CtrlO::PDF - high level PDF creator


use CtrlO::PDF;
use Text::Lorem;

my $pdf = CtrlO::PDF->new(
    logo        => "sample/logo.png", # optional
    orientation => "portrait", # Default
    footer      => "My PDF document footer",
    PDFlib      => "Builder",  # Default

# Add a page

# Add headings
$pdf->heading('This is the main heading');
$pdf->heading('This is a sub-heading', size => 12);

# Add paragraph text
my $lorem = Text::Lorem->new();
my $paras = $lorem->paragraphs(30);

# Add a table
my $data =[
    ['Fruit', 'Quantity'], # Table header
    ['Apples', 120],
    ['Pears', 90],
    ['Oranges', 30],

my $hdr_props = {
    repeat     => 1,
    justify    => 'center',
    font_size  => 8,

    data => $data,
    header_props => $hdr_props,

my $file = $pdf->content;

# output the file
open my $pdf_out, '>', 'out.pdf';
binmode $pdf_out;
print $pdf_out $file;
close $pdf_out;


This module tries to make it easy to create PDFs by providing a high level interface to a number of existing PDF modules. It aims to "do the right thing" by default, allowing minimal coding to create long PDFs. It includes pagination, headings, paragraph text, images and tables. Although there are a number of other modules to create PDFs with a high-level interface, I found that these each lack certain features (e.g. image insertion, paragraph text). This module tries to include each of those features through another existing module. Also, it is built on your choice of PDF::Builder or PDF::API2, and provides access to that object, so content can also be added directly using that, thereby providing any powerful features required.



Returns the PDF::Builder or PDF::API2 object used to create the PDF.


Returns the current PDF page.


Adds a PDF page and returns it.

Note that when a PDF page is added (either via this method or automatically) the is_new_page flag records that a new page is in use with no content. See that method for more details.


Whether the current page is new with no content. When the heading or text methods are called and this is true, additional top margin is added to account for the height of the text being added. Any other content manually added will not include this margin and will leave the internal new page flag as true.


Manually clears the is_new_page flag.


Sets or returns the page orientation (portrait or landscape). Portrait is default.


Sets or returns the width. Default is A4.


Sets or returns the height. Default is A4.


Sets or returns the page margin. Default 40 pixels.


Sets or returns the top padding (additional to the margin). Default 0.

Sets or returns the footer text. Page numbers are added automatically.


Sets or returns the font. This is based on PDF::Builder or PDF::API2 ttfont, which returns a TrueType or OpenType font object. By default, it assumes the font is available in the exact path truetype/liberation/LiberationSans-Regular.ttf. A future version may make this more flexible.


As font, but a bold font.

The path to a logo to include in the top-right corner of every page (optional).


The scaling of the logo. For best results a setting of 0.5 is recommended (the default).


The padding below the logo before the text. Defaults to 10 pixels.


Returns the current y position on the page. This value updates as the page is written to, and is the location that content will be positioned at the next write. Note that the value is measured from the bottom of the page.


Sets the current Y position. See y_position.


Moves the current Y position, relative to its current value. Positive values will move the cursor up the page, negative values down. See y_position.

heading($text, %options)

Add a heading. If called on a new page, will automatically move the cursor down to account for the heading's height (based on the assumption that one pixel equals one point). Options available are:

size _n_

    `n` is the font size in points, **default 16**

indent _n_

    `n` is the amount (in points) to indent the text, **default 0**

topmargin _n_

    `n` is the amount (in points) of vertical skip for the margin 
    _above_ the heading, **default 0**

bottommargin _n_

    `n` is the amount (in points) of vertical skip for the margin 
    _below_ the heading, **default 10**

text($text, %options)

Add paragraph text. This will automatically paginate. Options available are:

size _n_

    `n` is the font size in points, **default 10**

indent _n_

    `n` is the amount (in points) to indent the paragraph first line, 
    **default 0**

color _name_

    `name` is the string giving the text color, **default 'black'**


Add a table, based on PDF::Table. Options available are data to pass in the data for the table; all other options are passed to the table method of PDF::Table.

image($file, %options)

Add an image. Options available are:

scaling _n_

    `n` is the scaling factor for the image, **default 0.5** (50%)


Return the PDF content.


Copyright 2018-2020 Ctrl O Ltd

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License (GPL) as published by the Free Software Foundation; or the Perl Artistic License (PAL).

See for more information.