NAME
PDF::API2::Simple - Simplistic wrapper for the excellent PDF::API2 modules
SYNOPSIS
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();
Note that this module aims to be simple, not accurate or efficent, though it is mostly both. I like the PDF::API2 modules, but I find, and suspect others do also, that they are difficult to use and lack documentation. This module was written with PDF creation in mind, though it is certainly possible to update existing PDF files with this module.
Take note that PDF 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 left, but y grows towards the top.
METHODS
new
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 PDF file you want to write to. No default, parameter required
width - The width of the PDF file. Defaults to 612, the 8 1/2 x 11 US Letter width
height - The height of the PDF file. Defaults to 792, the 8 1/2 x 11 US 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
y
of the page, accounting for the bottom margineffective_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
CODE
reference will be called everytime a page is appended to the PDF, allowing you to specifiy a header for your pagesfooter - This
CODE
reference will be called everytime a page is ended, allowing you to specifiy a footer for your pages
Space Management
The following methods help you to manage the space on each page of your PDF.
next_line_would_extend_page
Returns a true value if the current y
minus line_height
would write past the bottom margin.
would_extend_page theoretical_y
Returns a true value if the current y
minus theoretical_y
would write past the bottom margin.
next_line
Reduces the current y
by line_height
.
add_page
Closes the current page, resets the x
and y
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.
General Management
These methods help you manage the PDF itself
add_font font_name
This method will add a font to be used throughout the PDF. You MUST
call this at least once before laying out your PDF. font_name
is a font name, such as 'Arial', or 'Verdana'. Appending 'Bold' seems to make the text bold, as in 'VerdanaBold'. Refer to PDF::API2 for more details.
set_font font_name
[, pt
]
Set the current font by name, and optionally a font size.
save
End the current page, and save the document to the file specified when instaniating the object.
Layout
These methods modify the actual layout of the PDF. Note that most of these methods set some internal state. Most often, the last x
and y
are set after the rendered object.
Most times, underscores may be stripped and the arguments will still work, such as is the difference between fill_color
, and fillcolor
.
popup text
[, %opts
]
Creates a 25x25 box at opts{x}
(or the current x
), opts{y}
(or the current y
) 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 link
method.
link url
, text
[, %opts
]
Specifies a link to url
, having text
. %opts
may contain:
x
- The x position of the link. Defaults tox
.y
- The y position of the link. Defaults toy
.limit
- The amount to "limit" 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 "do not limit".align
- Which alignment you want for your text. The choices are 'left', 'center', and 'right'. Defaults to 'left'.font
- Specifies via font name, the font to use. This sets the current font.font_size
- Specifies the font size. This sets the current font size.fill_color
- Specifies the fill color. This sets the current fill color.stroke_color
- Specifies the stroke color. This sets the current stroke color.
This method returns the width of the text.
text text
[, %opts
]
Renders text onto the PDF.
x
- The x position of the link. Defaults tox
.y
- The y position of the link. Defaults toy
.limit
- The amount to "limit" 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 "do not limit".align
- Which alignment you want for your text. The choices are 'left', 'center', and 'right'. Defaults to 'left'.autoflow
- Any value but 'off' will notify this method to use "autoflowing" 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 withlimit
.font
- Specifies via font name, the font to use. This sets the current font.font_size
- Specifies the font size. This sets the current font size.fill_color
- Specifies the fill color. This sets the current fill color.stroke_color
- Specifies the stroke color. This sets the current stroke color.
If autoflow
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 src
[, %opts
]
Renders an image onto the PDF. The following image types are supported: JPG, TIFF, PNM, PNG, GIF, and PDF. Note that the module determines the image type by file extension.
x
- The x position of the link. Defaults tox
.y
- The y position of the link. Defaults toy
.width
- The width of the image. Defaults to 100.height
- The height of the image. Defaults to 100.scale
- Amount to scale the image. Defaults to 1. (only available for PDF types)
line [, %opts
]
Renders a line onto the PDF.
x
- The x position of the line. Defaults tox
.y
- The y position of the line. Defaults toy
.to_x
- The x position where to draw the line to.to_y
- The y position where to draw the line to.fill_color
- Specifies the fill color. This sets the current fill color. Defaults tocurrent_fill_color
.stroke_color
- Specifies the stroke color. This sets the current stroke color. Defaults tocurrent_stroke_color
.width
- Specifies the width of the line. Defaults to 0.5.
rect [, %opts
]
Renders a rectangle onto the PDF. 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.
x
- The x position of the rect. Defaults tox
.y
- The y position of the rect. Defaults toy
.to_x
- The x position where to draw the rect to.to_y
- The y position where to draw the rect to.stroke
- A value of 'on', 'true', or 'yes' will enable the stroke. Defaults to 'on'.fill
- A value of 'on', 'true', or 'yes' will enable the fill. Defaults to 'off'.fill_color
- Specifies the fill color. This sets the current fill color. Defaults tocurrent_fill_color
.stroke_color
- Specifies the stroke color. This sets the current stroke color. Defaults tocurrent_stroke_color
.width
- Specifies the width of the line. Defaults to 0.5.
PROPERTIES
The following properties are read/write, and may be used as $pdf->prop to read, $pdf->prop('val') to set.
header
Gets/sets the header callback.
footer
Gets/sets the footer callback.
file
Gets/sets the file used to save the PDF. It's recommended that you do not set this.
width
Gets/sets the width of the current page. It's recommended that you do not set this.
height
Gets/sets the height of the current page. It's recommended that you do not set this.
line_height
Gets/sets the default line height. This is used in most space layout methods.
margin_left
Gets/sets the left margin.
margin_top
Gets/sets the top margin.
margin_right
Gets/sets the right margin.
margin_bottom
Gets/sets the bottom margin.
width_right
Gets/sets the calculated value, width
- margin_right
height_bottom
Gets/sets the calculated value, height
- margin_bottom
effective_width
Gets/sets the calculated value, width
- (margin_left
+ margin_right
)
effective_height
Gets/sets the calculated value, height
- (margin_top
+ margin_bottom
)
current_page
Gets/sets the current page number. It's hard to find a good reason to set this, but it is possible to do so.
Gets/sets the raw PDF::API2 object. This allows you great flexibility in your applications.
fonts
Gets/sets the array of fonts we have been storing via the add_font
method.
current_font
Gets/sets the current font.
current_font_size
Gets/sets the current font size.
current_stroke_color
Gets/sets the current stroke color. Aliases for this method are stroke_color
, and strokecolor
current_fill_color
Gets/sets the current fill color. Aliases for this method are fontcolor
, set_font_color
, font_color
, fillcolor
, and fill_color
.
x
Gets/sets the current x position.
y
Gets/sets the current y position.
BUGS / FIXES
Please mail all bug reports, fixes, improvements, and suggestions to bugs -at- redtreesystems -dot- com.
SPONSERS AND LICENSE
This project was sponsered in part by http://deviatemedia.com. This project belongs to http://redtreesystems.com, and is placed into the public domain in the hopes that it will be educational and/or useful. Each party asks only that they are referenced, or this section kept in any modification of this module, or derivitive work thereof.