Help language development. Donate to The Perl Foundation
PDF::Document - Provides high-level classes and routines to create original documents in Portable Document Format (PDF)
This module is currently functioning as a laboratory to create routines and classes to support other PDF modules. As such, its API is subject to change until version 1.0.0+.
In the meantime, users are encouraged to use it, report issues, and submit feature requests.
dev directory in the source repository for examples of use. The example in the SYNOPSIS is program
#!/usr/bin/env raku use PDF::Document; # We change only three of the many defaults for this # example: (1) output file name, (2) force option to # allow overwriting that file if it exists, and (3) # turn page numbering on: my \d = Doc.new: :pdf-name<example-letter>, :force, :page-numbering, :$debug; #=========== THE LETTER ================= # starts with a new page, current position top baseline, left margin # put the date at the top-right corner d.print: "2021-03-04", :tr, :align<right>, :valign<top>; d.nl; # adds the newline, resets x to left margin, moves y down one line d.say: "Dear Mom,"; # SHOULD automatically add a newline d.nl: 1; # moves y down one line, resets x=0 (left margin) d.say: "I am fine."; d.nl: 1; d.say: "How are you?"; # simple graphics: circle, etc. d.nl: 30; d.say: "circle: radius = 36 pts, linewidth = 4 points"; d.save; # save the current position and graphics state d.setlinewidth: 4; # points d.circle: :x<5in>, :y<3in>, :radius(36); # default points (or in, cm) d.restore; # don't forget to go back to normal! d.np; # new page, current position top baseline, left margin d.say: q:to/PARA/; Pretend this is a VERY long para that extends at least more than one line length in the current font so we can observe the effect of paragraph wrapping. Isn't this swell! PARA d.nl: 3; d.say: "Thats all, folks, but see following pages for other bells and whistles!"; d.nl: 2; d.say: "Love,"; d.nl: 2; d.say: "Isaiah"; d.np; # for some graphics examples d.say: "ellipse: a = 1 in, b = 0.5 in", :y<8in>; d.ellipse: :x<5in>, :y<8in>, :a<1in>, :b<.5in>; d.say: "ellipse: a = 0.3 in, b = 2 cm", :y<6in>; d.ellipse: :x<5in>, :y<6in>, :a<.3in>, :b<2cm>; d.say: "circle: radius = 24 mm", :y<4in>; d.circle: :x<5in>, :y<4in>, :radius<24mm>; d.say: "rectangle: width = 2 in, height = 2 cm", :y<2in>; d.rectangle: :llx<5in>, :lly<2in>, :width<2in>, :height<2cm>; d.np; # for some more graphics examples d.say: "polyline:", :y<7.5in>; my @pts = 1*i2p, 7*i2p, 4*i2p, 6.5*i2p, 3*i2p, 5*i2p; d.polyline: @pts; d.say: "blue polygon:", :y<4.5in>; @pts = 1*i2p, 4*i2p, 4*i2p, 3.5*i2p, 3*i2p, 2*i2p; d.polygon: @pts, :fill, :color[0,0,1]; # rgb, 0-1 values d.end-doc; # renders the pdf and saves the output # also numbers the pages if you requested it #=========== END THE LETTER =================
PDF::Document leverages the power of lower-level modules
Font::AFM and encapsulates some of its classes, routines, and variables into higher-level contructs to ease PDF document creation.
This module is designed around the document generation process used by those who use PostScript (PS) code to create PS documents which are then transformed into PDF by the GNU program
ps2pdf. That process is described in great detail in the classic PS books by Adobe (see REFERENCES).
The same sequence is also followed in the PDF document creation process:
my $pdf = PDF::Lite;
Select the fonts (with size) to be used with the
FontFactory. (There are 72 PS points per inch.)
my $ff = FontFactory.new: :$pdf; my $t12d1 = $ff.get-font: 't12d1'; # Times-Roman at 12.1 points my $c10 = $ff.get-font: 'c10'; # Courier at 10 points
Define each page
my $page = $pdf.add-page; #...add text and graphics... #...add a new page... my $page = $pdf.add-page; #...add text and graphics...
Create the document and exit
As you can see the document steps are equivalent, but the steps in PDF page creation are much easier because common low-level code required in PS creation is available under the covers in
PDF::Lite and accessed more easily by this module.
Currently the the module provides routines and constants as used in the example program shown in the SYNOPSIS. In addition, other graphics and text examples are shown in the
/dev directory including showing phases of the Moon, creating grids, using landscape orientation, and using A4 paper.
There is also a font factory which eases selection and use of multiple fonts. Fonts included are all the standard PostScript fonts plus a font used to create bank checks: MICREncoding. The PS fonts are free for any use, but the MICR font is only free for non-commercial use. See its license.txt file in the
More work is planned including:
This module is being used during the development of the author's other PDF modules:
This module will be updated with more items as the user modules are updated and published.
NOTE: The asterisk (
*) indicates the module has been published, albeit of minimal use. Two asterisks means the published module is not even minimally useful, but it is exposed to issues or feature requests from interested parties.
The author is indebted to the tremendous amount of work done by his Raku friend, David Warring. David's voluminous project, hosted at https://github.com/PDF-Raku, provides all the tools needed to manipulate PDF files using our wonderful Raku language. Thank you David!
Tom Browder [email protected]
Copyright © 2021-2023 Tom Browder
This library is free software; you may redistribute it or modify it under the Artistic License 2.0.