What is PDFREP?

 

PDFREP is a Perl Package.  It was developed to allow the creation of a PDF file from a Perl program without using any third party or Adobe software.

 

PDFREP has been designed primarily to create business reports into a PDF file, although it will allow any information to be created by a Perl program as long as the Perl program can obtain the data.

 

It has been written Trevor Ward and is available for use by all parties free of charge.

 

What can it do?

 

PDFREP can be used to create a PDF file a line of data at a time.  You can specify the font, font size, text colour, column indent, page size (A4 or letter), page orientation (Landscape or Portrait) and font slope (Italic).

 

The page size and orientation is fixed across the whole document.

The font, font size, text colour, column indent and font slope is line specific.

 

What is a PDF File?

 

A PDF file is a special format file which has become the industry standard for documents on the Web.  The file has specific features and requires a special reader from Adobe to view and print.  The reader is available free of charge, however to create a PDF file requires special software.  Adobe provide the software to create PDF files which costs quite a lot of money and only runs on windows based computers.

 

The advantages of using a PDF file for documents are that it can be downloaded from the web quickly and easily, the standard format allows for easy viewing and printing, the document cannot be changed unless the appropriate software is available.

 

The disadvantages of using a PDF file for documents are that it is much larger than a conventional text file, the software to create and change the files is expensive and limited to only windows based computers Until now.

 

What is Perl?

 

Perl is a programming language, which was developed primarily for use on Unix based computers.  It has become the main language for creating CGI's or web programs and systems.  Although not confirmed Perl is believed to be behind over 90% of the Internet.  It is very powerful and allows for a great deal of flexibility in use and application.

 

To use Perl within a web development you are required to have Perl installed on the Web server.  As Perl is open source (FREE) most web servers come with Perl compatibility.  Perl can be obtained from www.perl.com

 

How to utilise PDFREP

 

As already mentioned PDFREP will create a PDF file using a Perl program.  PDFREP is not computer platform specific and will run on any computer, which is web configured and has Perl as the main programming language.

 

To utilise PDFREP you will require a copy of the PDFREP program, a Perl programmer, Data contained within a relational database like Oracle or in a text file and the technical part of this document, which explains how to write the Perl program to use PDFREP.

 

You can then decide how you want you document to look, fonts sizes colours etc.

 

PDFREP will not format a line of data into columns, so when choosing the fonts if you require the data lined up into columns a fixed width font like courier will have to be used.  Also PDFREP will not wrap the text over multiple lines if you go over the right hand edge the text is lost.

 

All this being said PDFREP is quick, efficient and easy to use from a programmer's viewpoint.  It is free and no special software has to be purchased to use it.  It is completely self-contained.

The Techie Bit

 

This part of the document explains how to use the PDFREP module and write the Perl program.

 

Overview

 

PDFREP has been written as a standard Perl module.  Including in the Perl program uses it by using the USE option.  It has various call options for new document and page etc passing required parameters with the data.  Two parameters are always returned from the module. Being the status code and a message. The module does not fall over it is left to the programmer to code in the error checking if required.

 

The following options have to be used in a specific order as detailed.

 

                PDFREP->heading()          Once only

                PDFREP->fontset()             One or more

                PDFREP->pagedata()        One or more NP first

                PDFREP->writepdf()           Once only

 

Four options each use various parameters as detailed below but the order in which you output them is vital.

 

The first steps

 

To include the PDFREP module firstly copy the PDFREP.pm program into an accessible directory which is in the Perl path. (Only if you don't already have it).

 

Then after the shebang line but before the rest of code include the package.

 

USE PDFREP;

 

Now you can start writing you Perl code.

 

File Naming

 

The first thing you have to do to initialise the PDFREP is to call the package heading function. This accepts two parameters, which is the file name and the directory you want it to go to.  The filename will automatically have the .PDF added so do not include an extension.

 

my $filenam = "pdftest1";

my $filedir = "../";

 

($status, $message) = PDFREP->heading($filenam, $filedir);

 

$status is true if the file was opened and created with the required heading information.

$message contains text like FILE OPENED Successfully.

 

$status is false if the package failed and the error message is set to reflect this.  The status and message are used for every call to the package to enable the programmer to code there own error routines.

 

Fonts

 

The PDFREP package allows for the specification of fonts.  The example here uses the Arial font but any can be used like Helvetica or Courier. To add Bold or Italic just put the font name Arial-bold this will give a bold font.

 

The font call accepts two parameters; the first is the name reference. This will be used as a reference when outputting the data and the physical fonts name.

 

($status, $message) = PDFREP->fontset('F1','Arial');

 

You can add as many fonts as you wish but they must be declared before use, and each have a unique name reference.

 

Page Data

 

Now the file is set-up and the fonts are ready for use, it's time to output the data. Take a look at the example line.

 

($status, $message) = PDFREP->pagedata('np','0','12','F2','12','0','0','0','0','This is the first bit of text','LE','LA');

 

This is the first line of data for a new page. Every time you want a new page you use the line as above.  There are various parameters passed as detailed below.

 

1              =              np or nl This tells the package if it is a new page (np) or a new line (nl).

2              =              '???'                        This is the column offset 0 is left hand side of page 10 is in 10 spaces.

3              =              '12'                          This is the font size of the text for that line.

4              =              'F1'                          This is the name reference of the font you declared earlier.

5              =              '12'                          This is the font size of the next line used to get correct line spacing.

6              =              '0'                            If set to one this will tilt the font to the right like italic.

7,8,9       =              'r','g','b'                    These three define the colour of the text in standard red green blue.

10           =              'TEXT'                     This is the text to be output can be perl scalar.

11           =              'LE'                          LE = Letter A4 = A4 this is the page size. Only needed on new page.

12           =              'LA'                          LA = Landscape PO = Portrait this is the page orientation Only needed on np.

 

OK so there you have the new page output. After you have sent one of these use the nl or new line to output further text to that line. Beware data is lost to the right and off the bottom of the page so make sure the letter and line counts are accurate.

 

Example of new line

 

($status, $message) = PDFREP->pagedata('nl','0','12','F1','10','0','0','0','0','This is the second bit of text');

 

Well that’s it all you need to know how to get the data out to a PDF file now you just need to say you have finished writing out data.

 

The Last Bit

 

($status, $message) = PDFREP->writepdf('LE','LA');

 

Pretty straight forward huh. No messing around after you have finished this line will complete the writing of the PDF file close it and make it available for viewing. Please note the page size and orientation parameters.  The document itself will only allow for one page size and orientation throughout.

 

Have fun any probs  there is a complete test system available.

 

Contact.

 

If you have any questions or suggestions please feel free to contact me at Trevor.r.ward@btinternet.com.

 

All documents programs etc are copyrighted to me with the PDF file layout copyrighted to Adobe.

 

Have fun hope this is useful.