NAME
Mojo::PDF - Generate PDFs with the goodness of Mojo!
SYNOPSIS
# Just render text. Be sure to call ->end to save your document
Mojo::PDF->new('mypdf.pdf')->text('Viva la Mojo!', 306, 396)->end;
# Let's get fancy pants:
Mojo::PDF->new('myawesome.pdf')
->mixin('_template.pdf') # add a pre-made PDF page from a template
# Render text with standard fonts
->font('Times-Bold')->size(24)->color(0, 0, .7)
->text('Mojo loves PDFs', 612/2, 500, 'center')
# Render text with custom TTF fonts
->add_fonts(
galaxie => 'fonts/GalaxiePolaris-Book.ttf',
galaxie_it => 'fonts/GalaxiePolaris-BookItalic.ttf',
)
->font('galaxie')->size(24)->color('#353C8C')
->text('Weeee', 20.4, 75 )
->text('eeee continuing same line!')
->text('Started a new line!', 20.4 )
# Render a table
->font('galaxie_it')->size(8)->color
->table(
at => [20.4, 268],
data => [
[ qw{Product Description Qty Price U/M} ],
@data,
],
)
->end;
DESCRIPTION
Mojotastic, no-nonsense PDF generation.
WARNING
This module is currently experimental. Things will change.
METHODS
Unless otherwise indicated, all methods return their invocant.
new
my $pdf = Mojo::PDF->new('myawesome.pdf');
Creates a new Mojo::PDF
object. Takes one mandatory argument: the filename of the PDF you want to generate.
end
$p->end;
Finish rendering your PDF and save it.
add_fonts
$pdf->add_fonts(
galaxie => 'fonts/GalaxiePolaris-Book.ttf',
galaxie_it => 'fonts/GalaxiePolaris-BookItalic.ttf',
);
Adds TTF fonts to the document. Key/value pairs specify the arbitrary name of the font (for you to use with "font") and the path to the TTF file.
You cannot use any of the names of the "DEFAULT FONTS" for your custom fonts.
color
$pdf->color(.5, .5, .3);
$pdf->color('#abcdef');
$pdf->color('#abc'); # same as #aabbcc
$pdf->color; # same as #000
Specifies active color. Takes either an RGB tuple or a hex colour. Defaults to black.
font
$pdf->font('Times-Bold');
$pdf->font('galaxie');
Sets active font family. Takes the name of either one of the "DEFAULT FONTS" or one of the custom fonts included with "add_fonts"
mixin
$pdf->mixin('template.pdf');
$pdf->mixin('template.pdf', 3);
Adds a page from an existing PDF to your currently active page, so you can use it as a template and render additional things on it. Takes one mandatory argument, the filename of the PDF to include. An optional second argument specifies the page number to include (starting from 1), which defaults to the first page.
page
$pdf->page;
Add a new blank page to your document and sets it as the currently active page.
rule
->rule(
bold => { re => qr/\*\*(.?)\*\*/, font => 'galaxie_bold' },
shiny => {
re => qr/!!(.?)!!/,
font => 'galaxie_bold',
color => '#FBBC05',
size => 30,
},
)
->text('Normal **bold text** lalalala !!LOOK SHINY!!')
->rule( shiny => undef )
->text('!!no longer shiny!!')
Sets rules for bits of text when rendering with "text" or "table". Available overrides are "font", "color", and /size
. To disable a rule, set its value to undef
.
size
$pdf->size(24);
$pdf->size; # set to 12
Specifies active font size in points. Defaults to 12
points.
table
$pdf->table(
at => [20.4, 268],
data => [
[ qw{Product Description Qty Price U/M} ],
@$data,
],
#Optional:
border => [.5, '#CFE3EF'],
header => 'galaxie_bold',
max_height => [ 744, sub {
my ( $data, $conf, $pdf ) = @_;
$conf->{at}[1] = 50;
$pdf->page;
$data;
},
min_width => 571.2,
row_height => 24,
str_width_mult => 1.1,
);
Render a table on the current page. Takes these arguments:
at
at => [20.4, 268],
An arrayref with X and Y point values of the table's top, left corner.
data
data => [
[ qw{Product Description Qty Price U/M} ],
@$data,
],
An arrayref of rows, each of which is an arrayref of strings representing table cell values. Setting "header" will render first row as a table header. Cells that are undef
/empty string will not be rendered.
border
border => [.5, '#CFE3EF'],
Optional. Takes an arrayref with the width (in points) and colour of the table's borders. Color allows the same values as "color" method. Defaults to: [.5, '#ccc']
header
header => 'galaxie_bold',
Optional. Takes the same value as "font". If set, the first row of /data
will be used as table header, rendered centered using header
font. Not set by default.
max_height
$pdf->table(
at => [20.4, 300],
data => $data,
max_height => [ 744, sub {
my ( $data, $conf, $pdf ) = @_;
$conf->{at}[1] = 50; # start table higher on subsequent pages
$pdf->page; # start a new page
$data; # render remaining rows
},
);
Optional. Takes an arrayref with two arguments: the maximum height (in points) the table should reach and the callback to use when not all rows could fit. The return value of the callback will be used as the new collection of rows to render. The @_
will contain remaining rows to render, hashref of the options you've passed to table
method, and the Mojo::PDF
object.
min_width
min_width => 571.2,
Optional. Table's minimum width in points (zero by default). The largest column will be widened to make the table at least this wide.
row_height
row_height => 24,
Optional. Specifies the height of a row, in points. Defaults to 1.4 times the current font size.
str_width_mult
str_width_mult => 1.1,
Optional. Cell widths will be automatically computed based on the width of the strings they contain. On some fonts, the detection is a bit imperfect. For those cases, use str_width_mult
as a multiplier for the detected character width.
text
$p->text($text_string, $x, $y, $alignment, $rotation);
$p->text('Mojo loves PDFs', 612/2, 500, 'center', 90);
$p->text('Lorem ipsum dolor sit amet, ', 20 );
$p->text('consectetur adipiscing elit!');
Render text with the currently active "font", "size", and "color". $alignment
specifies how to align the string horizontally on the $x
point; valid values are left
(default), center
, and right
. $rotation
is the rotation of the text in degrees.
Subsequent calls to text
can omit $x
and $y
values with these effects: omit both to continue rendering where previous text
finished; omit just $y
, to render on the next line from previous call to text
.
DEFAULT FONTS
These fonts are available by default:
Times-Roman
Times-Bold
Times-Italic
Times-BoldItalic
Courier
Courier-Bold
Courier-Oblique
Courier-BoldOblique
Helvetica
Helvetica-Bold
Helvetica-Oblique
Helvetica-BoldOblique
Symbol
ZapfDingbats
You can use their abbreviated names:
TR
TB
TI
TBI
C
CB
CO
CBO
H
HB
HO
HBO
S
Z
SEE ALSO
PDF::Reuse, PDF::Create, and PDF::WebKit
REPOSITORY
Fork this module on GitHub: https://github.com/zoffixznet/Mojo-PDF
BUGS
To report bugs or request features, please use https://github.com/zoffixznet/Mojo-PDF/issues
If you can't access GitHub, you can email your request to bug-Mojo-PDF at rt.cpan.org
AUTHOR
LICENSE
You can use and distribute this module under the same terms as Perl itself. See the LICENSE
file included in this distribution for complete details.