- Perl ›
- Form creation ›
- here
Create PDF file - Create form with PDF::API2
To create a PDF file with PDF::API2:
Use the new method to create empty information in the PDF.
To add an empty page, use the page method.
Finally, use the saveas method to save the file.
By following this procedure, you can create a PDF file with one empty page.
use strict; use warnings; use PDF::API2; # Create empty PDF information my $pdf = PDF::API2->new; # Add an empty page my $page = $pdf->page; # Save PDF to file my $pdf_file = 'empty.pdf'; $pdf->saveas($pdf_file);
If you can create an empty PDF file, you are successful.
Open an existing PDF file
To open an existing PDF file with PDF::API2, use the open method.
# Open an existing PDF file my $pdf = PDF::API2->open('some.pdf');
Add a new page
Use the page method to add a new page in PDF::API2. The page created by page is added to the end of the PDF document.
# Add page to end of PDF document my $page = $pdf->page;
If you specify an argument, you can insert it on the specified page. Existing pages will be moved backwards.
# Add page to specified page of PDF document my $page = $pdf->page(5);
Get an existing page
To get the page that already exists in PDF::API2, specify the page number in the openpage method. Page numbers start at 1. Note that it does not start at 0.
# Get pages that already exist my $page = $pdf->openpage($page_number);
Get the number of pages
In PDF::API2, use the pages method to get the number of existing pages.
# Get the number of pages in a PDF document my $pages_count = $pdf->pages;
Set the paper size
To set the page paper size in PDF::API2, use the mediabox method.
# Specified by name $pdf->mediabox($name); # Specified by width and height $pdf->mediabox($width, $height)
The name that can be specified is the value defined in PDF. Here are some examples that you can specify: This is a global setting, and if you create a new page, the setting will be inherited by the page.
To specify the paper size for each page, use the page object's mediabox method.
# Specified by name $page->mediabox($name); # Specified by width and height $page->mediabox($width, $height)
- Letter
- A3
- A4
- B4
- A5
- B5
- A6
- B6
I will link to a page with a brief explanation about the PDF coordinate system.
Note that the width and height must be specified in PDF units. One unit is 1/72 inch (about 0.35 mm).
A typical PDF document has a page width of 612 units and a height of 792 units. The inch size is 8.5 "x 11". Obtained by dividing the width and height by 72.
When a PDF document is given to the screen, it usually needs physical values such as pixels. Resolution must be provided in order to convert from logical to physical units. Resolution is the value of a point(pixel) per inch used when logically converting to a physical value. In the above example, using 150 as the resolution, the page size in pixels would be 1275 x 1650. This is obtained by multiplying 8.5 x 11 inches by 150.
(Reference) PDF coordinate system
This is an example to specify the paper size in PDF::API2.
use PDF::API2; # Specified by A4 size { my $pdf = PDF::API2->new(); $pdf->mediabox('A4'); my $mediabox = $pdf->mediabox; my $page = $pdf->page; my $pdf_file = 'size_a4.pdf'; $pdf->saveas($pdf_file); } # Specify by B5 size { my $pdf = PDF::API2->new(); $pdf->mediabox('B5'); my $page = $pdf->page; my $pdf_file = 'size_b5.pdf'; $pdf->saveas($pdf_file); } # PDF unit Width 595, height 842 { my $pdf = PDF::API2->new(); $pdf->mediabox(595, 842); my $page = $pdf->page; my $pdf_file = 'size_595_842.pdf'; $pdf->saveas($pdf_file); }
Output result A4 size, Output result B5 size, Output result 595 × 842 size
Get the paper size
To get the paper size, use the page object's mediabox method with no arguments.
my @page_size_infos = $page->mediabox;
Note that you must call the page object's mediabox method, not the PDF object, to get information about the actual page paper size.
Please note that the paper size information is obtained in 4 PDF units.
(Margin X from lower left, Margin Y from lower left, Coordinate X on upper right, Coordinate Y on upper right)
For example, the following values are returned.
(0, 0, 612, 792)
Generally, the margin X from the lower left is 0 and the margin Y from the lower left is 0, so you can think of the upper right coordinate X as the width and the upper right coordinate Y as the height.
Combine multiple PDF files
Here are the steps to combine multiple PDF files. Create a new PDF::API2 object. Open an existing PDF file.
Insert an existing PDF file into a new PDF using the import_page method.
Finally, save the new PDF and you're done.
my $pdf = PDF::API2->new(); my $old = PDF::API2->open('our/old.pdf'); # Add page 2 from the old PDF as page 1 of the new PDF $pdf->import_page($old, 2); $pdf->saveas('our/new.pdf');