Perl Pdf Create

Posted on 2010-07-22 09:35-07 by kodo Hi, now forgive me for being new, I have PDF-Create working just wonderfully, but I cannot find an example of an image being added to a pdf anywhere, and I've search all day. PDF-Create (1.03) The example code worked out of the box on Win32 and was very fast. PDF::Create seems more straightforward (at least from the docs) and may meet your 'simple formatting' requirement more adequately. However if you want to know what the 'community' thinks then only PDF::API2 gets a rating on CPAN Ratings coming in with 4 out of 5 stars overall score.

Perl is widely known as 'the duct-tape of the Internet'. Perl can handle encrypted Web data, including e-commerce transactions. Perl can be embedded into web servers to speed up processing by as much as 2000%. Perl's modperl allows the Apache web server to embed a Perl interpreter.

Create or modify PDF with a Perl script.

Many web sites use Perl for creating dynamic content. You can also use Perl to script Acrobat on your local machine [Hack #95] . Given the great number of packages that extend Perl, it is no surprise that packages exist for creating and manipulating PDF. Let's take a look.

6.14.1 Install Perl and the PDF::API2 Package on Windows

[Hack #95] explains how to install Perl on Windows. After installing Perl, use the Perl Package Manager to easily install the PDF::API2 package.

Launch the Programmer's Package Manager (PPM, formerly called Perl Package Manager) by selecting Start Programs ActiveState ActivePerl 5.8 Perl Package Manager. A command prompt will open with its ppm> prompt awaiting your command. Type help to see a list of commands. Type search pdf to see a list of available packages. To install PDF::API2, enter installpdf-api2. The Package Manager will fetch the package from the Internet and install it on your machine. The entire session looks something like this:


The PDF::API2 package is used widely to create and manipulate PDF. You can download documentation and examples from http://pdfapi2.sourceforge.net/dl/.

6.14.2 Hello World in Perl

This Perl script creates a PDF named HelloWorld.pdf, adds a page, and then adds text to that page. It gives you an idea of how easily you can create PDF. Figure 6-16 shows the PDF document created by this script.

Figure 6-16. Creating PDF content using Perl

6.14.3 Discover Perl Packages with CPAN

CPAN (http://www.cpan.org) is the Comprehensive Perl Archive Network, where you will find 'All Things Perl.' Visit http://search.cpan.org to discover several other PDF packages. Drill down to find details, documentation, and downloads. For example, PDF::Extract (http://search.cpan.org/~nsharrock/) creates a new PDF from the pages of a larger, input PDF.

I’ve long been a user of PDF::API2, a module available for Perl. I use it everyday at work and along with a few other modules, it has made Perl an invaluable tool for me. Being able to generate PDF’s for reports and documents allow for a bevy of automation shortcuts where traditional Data Merging, or “variable imaging,” would be impractical and at times impossible to integrate into existing e-commerce or ERP platforms.

To run this code on your own computer, you will need to download a Perl interpreter. If you’re on Windows, I would recommend http://strawberryperl.com/.

Simple Letter sized PDF

Add a picture and text

Perl

JPEGs are supported as well as PNGs with transparency.

The example below has no image formatting, but the back-end Perl script for this example does center the image and size it down to fit, but that’s outside the scope of this simple example.

Perl Pdf Create

Add content to an existing PDF document

This is great for automating online forms for customers to fill out on your website that can then be used for printing, record-keeping, or emailing to other parties.

In this example, Page 1 of the IRS 1040 form is opened and a persons first and last names are filled in and saved as a new document.

A custom font file, Raleway, is also used.

Drawing lines

Perl

Basic shapes and lines are possible through the ‘gfx’ object. Here, the first four lines define the line, and the last line actually draws the line.

Barcodes

This really isn’t an example of PDF::API2, but rather an example of how existing Perl tools can be used to create custom documents. Here, the module ‘GD::Barcode::Code39’ is used to encode a custom string into a standard ‘3 of 9’ type barcode.

GD::Barcode::Code39 itself will only produce the 1’s and 0’s necessary for the barcode. A ‘foreach’ loop then iterates through the binary string and PDF::API2 will be used to draw either a black line (#000000) or a white line (#ffffff), producing a barcode.

You can download an app for your phone to scan the barcode. Read the CODE39 Wikipedia page for valid characters and more information. Regular show torrent.

2D Datamatrix Barcode

QR Code

Perl Pdf Reader

This one comes in handy a lot. Be careful with your “Version=>?” numbers. QR Codes will encode up to a certain number of bits depending on this version number. The example below is “Version=>4” which will hold up to 512 bits.

Exceeding the number of bits will error out, but the “GD::Barcode::QRcode” module has a bug that prevents handling the error cleanly. Read more.

Data Driven PDF

The below program will load a spreadsheet with the contact information for US Senators. The program will then open a ‘template’ business card and use the spreadsheet data to populate the business card and save it as a new file.

Note that in several places I am calling $text->advancewidth() and storing the result. Snip it for mac download. This variable is used for text justification.

Perl Pdf Create Rectangle

Download all the files here (540KB)