Simplistic wrapper for the excellent pdf::api2 modules
use PDF::API2::Simple; my $pdf = PDF::API2::Simple->new( file => 'output.pdf' ); $pdf->add_font('VerdanaBold'); $pdf->add_font('Verdana'); $pdf->add_page(); $pdf->link( 'http://search.cpan.org', 'A Hyperlink', x => 350, y => $pdf->height - 150 ); for (my $i = 0; $i < 250; $i++) { my $text = "$i - All work and no play makes Jack a dull boy"; $pdf->text($text, autoflow => 'on'); } $pdf->save();
Take note that \s-1PDF\s0 coordinates are not quite what you're used to. The coordinate, (0, 0) for instance is at the lower-left hand corner. Thus, x still grows to the right, but y grows towards the top.
PDF::API2::Simple->new( 'file' => 'output.txt', 'width' => 612, 'height' => 792, 'line_height' => 10, 'margin_left' => 20, 'margin_top' => 20, 'margin_right' => 20, 'margin_bottom' => 50, 'width_right' => 0, 'height_bottom' => 0, 'effective_width' => 0, 'effective_height' => 0, 'header' => undef, 'footer' => undef, );
Creates a new PDF::API2::Simple instance. A good strategy is to create a new object for each pdf file you want to create. That is, of course, up to you.
file - The \s-1PDF\s0 file you want to write to. No default, parameter required
width - The width of the \s-1PDF\s0 file. Defaults to 612, the 8 1/2 x 11 \s-1US\s0 Letter width
height - The height of the \s-1PDF\s0 file. Defaults to 792, the 8 1/2 x 11 \s-1US\s0 Letter height
line_height - The standard height you want to define for lines. The default is 10
margin_left - The amount of margin space you want on the left side. Of course, you can specify whatever coordniates you want. Default is 20
margin_top - The amount of margin space you want on the top of each page. Default is 20
margin_right - The amount of margin space you want of the right side of each page. Default is 20
margin_bottom - The amount of margin space you want on the bottom of each page. Default is 50
width_right - A convenience property that contains the furthest x of the page, accounting for the margins specified
height_bottom - A convenience property that contains the largest \*(C`y\*(C' of the page, accounting for the bottom margin
effective_width - A convenience property that contains the width of the page, after the left and right margin have been accounted for
effective_height - A convenience property that contains the height of the page, after the top and bottom margin have been accounted for
header - This \*(C`CODE\*(C' reference will be called everytime a page is appended to the \s-1PDF\s0, allowing you to specifiy a header for your pages
footer - This \*(C`CODE\*(C' reference will be called everytime a page is ended, allowing you to specifiy a footer for your pages
PDF::API2::Simple->open( 'open_file' => 'my_pdf.pdf', 'open_page' => 1, # Default is 1. # Any other options to new. );
This method opens an existing \s-1PDF\s0 for editing. You can include any other arguments that are valid for \*(C`new\*(C' and they will be set in the resulting object.
The following methods help you to manage the space on each page of your \s-1PDF\s0.
next_line_would_extend_page
Returns a true value if the current \*(C`y\*(C' minus \*(C`line_height\*(C' would write past the bottom margin.
would_extend_page \*(C`theoretical_y\*(C'
Returns a true value if the \*(C`theoretical_y\*(C' would write past the bottom margin.
next_line
Reduces the current \*(C`y\*(C' by \*(C`line_height\*(C'.
add_page
Closes the current page, resets the \*(C`x\*(C' and \*(C`y\*(C' values to thier default coordinates, and calls the header callback, if specified. This method may be called for in, such as if your autoflow text wraps over a page.
end_page
Closes the current page, and calls the footer callback, if specified. This method is usually called for you.
These methods help you manage the \s-1PDF\s0 itself
add_font \*(C`font_name\*(C'
This method will add a font to be used throughout the \s-1PDF\s0. You \*(C`MUST\*(C' call this at least once before laying out your \s-1PDF\s0. \*(C`font_name\*(C' is a font name, such as 'Arial', or 'Verdana'. Appending 'Bold' seems to make the text bold, as in 'VerdanaBold'. Refer to \s-1PDF::API2\s0 for more details.
You can optionally pass a font object and font size if you have loaded an external font.
my $font_obj = $pdf->pdf->ttfont('new_font.ttf'); $pdf->add_font('NewFont', $font_obj, '12');
Refer to \s-1PDF::API2\s0 for the various font methods, like \*(C`ttfont\*(C', available.
set_font \*(C`font_name\*(C'[, \*(C`pt\*(C']
Set the current font by name, and optionally a font size.
as_string
An alias for the stringify method
stringify
Ends the current page, and returns the pdf as a string
save [\*(C`file\*(C']
End the current page, and save the document to the file argument, or the file specified when instaniating the object. If not suitable file can be found to save in, a \*(C`Carp::croak\*(C' is emitted. Aliases for this method are \*(C`saveas\*(C' and \*(C`save_as\*(C'.
These methods modify the actual layout of the \s-1PDF\s0. Note that most of these methods set some internal state. Most often, the last \*(C`x\*(C' and \*(C`y\*(C' are set after the rendered object.
Most times, underscores may be stripped and the arguments will still work, such as is the difference between \*(C`fill_color\*(C', and \*(C`fillcolor\*(C'.
popup \*(C`text\*(C'[, %opts]
Creates a 25x25 box at \*(C`opts{x}\*(C' (or the current \*(C`x\*(C'), \*(C`opts{y}\*(C' (or the current \*(C`y\*(C') to represent an annotation at that point. The user may then click that icon for a full annotation.
url
This is an alias for the \*(C`link\*(C' method.
link \*(C`url\*(C', \*(C`text\*(C'[, %opts]
Specifies a link to \*(C`url\*(C', having \*(C`text\*(C'. %opts may contain:
\*(C`x\*(C' - The x position of the link. Defaults to \*(C`x\*(C'.
\*(C`y\*(C' - The y position of the link. Defaults to \*(C`y\*(C'.
\*(C`limit\*(C' - The amount to \*(L"limit\*(R" the text. This will add an ellipis (...) a few characters from the end if the text length is greater than this numerical value. Defaults to 0, which is to mean \*(L"do not limit\*(R".
\*(C`align\*(C' - Which alignment you want for your text. The choices are 'left', 'center', and 'right'. Defaults to 'left'.
\*(C`font\*(C' - Specifies via font name, the font to use. This sets the current font.
\*(C`font_size\*(C' - Specifies the font size. This sets the current font size.
\*(C`fill_color\*(C' - Specifies the fill color. This sets the current fill color.
\*(C`stroke_color\*(C' - Specifies the stroke color. This sets the current stroke color.
This method returns the width of the text.
text \*(C`text\*(C'[, %opts]
Renders text onto the \s-1PDF\s0.
\*(C`x\*(C' - The x position of the link. Defaults to \*(C`x\*(C'.
\*(C`y\*(C' - The y position of the link. Defaults to \*(C`y\*(C'.
\*(C`limit\*(C' - The amount to \*(L"limit\*(R" the text. This will add an ellipis (...) a few characters from the end if the text length is greater than this numerical value. Defaults to 0, which is to mean \*(L"do not limit\*(R".
\*(C`align\*(C' - Which alignment you want for your text. The choices are 'left', 'center', and 'right'. Defaults to 'left'.
\*(C`autoflow\*(C' - Any value but 'off' will notify this method to use \*(L"autoflowing\*(R" heuristics to gracefully wrap your text over lines and pages. Useful for variable length text. Note that due to laziness, this option is mutually exclusive with \*(C`limit\*(C'.
\*(C`font\*(C' - Specifies via font name, the font to use. This sets the current font.
\*(C`font_size\*(C' - Specifies the font size. This sets the current font size.
\*(C`fill_color\*(C' - Specifies the fill color. This sets the current fill color.
\*(C`stroke_color\*(C' - Specifies the stroke color. This sets the current stroke color.
If \*(C`autoflow\*(C' was not specified, this method returns the width of the text in scalar context, the bounding box of the text in list context. In autoflow mode, this method returns nothing.
image \*(C`src\*(C'[, %opts]
Renders an image onto the \s-1PDF\s0. The following image types are supported: \s-1JPG\s0, \s-1TIFF\s0, \s-1PNM\s0, \s-1PNG\s0, \s-1GIF\s0, and \s-1PDF\s0. Note that the module determines the image type by file extension.
\*(C`x\*(C' - The x position of the link. Defaults to \*(C`x\*(C'.
\*(C`y\*(C' - The y position of the link. Defaults to \*(C`y\*(C'.
\*(C`width\*(C' - The width of the image. Defaults to 100.
\*(C`height\*(C' - The height of the image. Defaults to 100.
\*(C`scale\*(C' - Amount to scale the image. Defaults to 1. (only available for \s-1PDF\s0 types)
line [, %opts]
Renders a line onto the \s-1PDF\s0.
\*(C`x\*(C' - The x position of the line. Defaults to \*(C`x\*(C'.
\*(C`y\*(C' - The y position of the line. Defaults to \*(C`y\*(C'.
\*(C`to_x\*(C' - The x position where to draw the line to.
\*(C`to_y\*(C' - The y position where to draw the line to.
\*(C`fill_color\*(C' - Specifies the fill color. This sets the current fill color. Defaults to \*(C`current_fill_color\*(C'.
\*(C`stroke_color\*(C' - Specifies the stroke color. This sets the current stroke color. Defaults to \*(C`current_stroke_color\*(C'.
\*(C`width\*(C' - Specifies the width of the line. Defaults to 0.5.
rect [, %opts]
Renders a rectangle onto the \s-1PDF\s0. Rectangles are usually drawn specifying two coordinates diagonal from each other. (5, 5) to (30, 30) for example, would produce a 25 x 25 square.
\*(C`x\*(C' - The x position of the rect. Defaults to \*(C`x\*(C'.
\*(C`y\*(C' - The y position of the rect. Defaults to \*(C`y\*(C'.
\*(C`to_x\*(C' - The x position where to draw the rect to.
\*(C`to_y\*(C' - The y position where to draw the rect to.
\*(C`stroke\*(C' - A value of 'on', 'true', or 'yes' will enable the stroke. Defaults to 'on'.
\*(C`fill\*(C' - A value of 'on', 'true', or 'yes' will enable the fill. Defaults to 'off'.
\*(C`fill_color\*(C' - Specifies the fill color. This sets the current fill color. Defaults to \*(C`current_fill_color\*(C'.
\*(C`stroke_color\*(C' - Specifies the stroke color. This sets the current stroke color. Defaults to \*(C`current_stroke_color\*(C'.
\*(C`width\*(C' - Specifies the width of the line. Defaults to 0.5.
The following properties are read/write, and may be used as $pdf->prop to read, $pdf->prop('val') to set.
Gets/sets the header callback.
Gets/sets the footer callback.
Gets/sets the file used to save the \s-1PDF\s0. It's recommended that you do not set this.
Gets/sets the width of the current page. It's recommended that you do not set this.
Gets/sets the height of the current page. It's recommended that you do not set this.
Gets/sets the default line height. This is used in most space layout methods.
Gets/sets the left margin.
Gets/sets the top margin.
Gets/sets the right margin.
Gets/sets the bottom margin.
Gets/sets the calculated value, \*(C`width\*(C' - \*(C`margin_right\*(C'
Gets/sets the calculated value, \*(C`height\*(C' - \*(C`margin_bottom\*(C'
Gets/sets the calculated value, \*(C`width\*(C' - (\*(C`margin_left\*(C' + \*(C`margin_right\*(C')
Gets/sets the calculated value, \*(C`height\*(C' - (\*(C`margin_top\*(C' + \*(C`margin_bottom\*(C')
Gets/sets the current page object. It's hard to find a good reason to set this, but it is possible to do so.
Gets/sets the raw \s-1PDF::API2\s0 object. This allows you great flexibility in your applications.
Gets/sets the array of fonts we have been storing via the \*(C`add_font\*(C' method.
Gets/sets the current font.
Gets/sets the current font size.
Gets/sets the current stroke color. Aliases for this method are \*(C`stroke_color\*(C', and \*(C`strokecolor\*(C'
Gets/sets the current fill color. Aliases for this method are \*(C`fontcolor\*(C', \*(C`set_font_color\*(C', \*(C`font_color\*(C', \*(C`fillcolor\*(C', and \*(C`fill_color\*(C'.
Gets/sets the current x position.
Gets/sets the current y position.
Please mail all bug reports, fixes, improvements, and suggestions to bugs -at- redtreesystems -dot- com.
This project belongs to Red Tree Systems, \s-1LLC\s0 - <http://www.redtreesystems.com>, but is placed into the public domain in the hopes that it will be educational and/or useful. Red Tree Systems, \s-1LLC\s0 requests that this section be kept intact with any modification of this module, or derivitive work thereof.
Thanks to Jim Brandt for noting that the default values didn't do well with false values.
Thanks to Denis Evdokimov for submitting more margin-fixing goodness.
Thanks to Jonathan A. Marshall for fixing a long standing margin issue, and pointing out further documentation shortcomings.
Thanks to Jim Brandt for the open method, contributing code to add_font, and offering beer.
Thanks to Pradeep N Menon and Alfred Reibenschuh for pointing out optimizaiton issues, and helping to resolve them.
Thanks to Simon Wistow for uncovering several bugs and offering up code.
Thanks to Bryan Krone for pointing out our documentation shortcomings.
\s-1PDF::API2\s0
There is an examples folder with this dist that should help you get started. You may contact pdfapi2simple -AT- redtreesystems _dot_ com for support on an individual or commercial basis.