David Adress

Subscribe to David Adress: eMailAlertsEmail Alerts
Get David Adress: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: ColdFusion on Ulitzer

CFDJ: Article

It's Summer – Dive Into PDF!

Combining It with ColdFusion Is a Lot Easier Than You May Think

ColdFusion plus Adobe's Portable Document Format add up to a lot of graphical power — and they mix like mint & julep.

ColdFusion and PDF
Let's say we need to produce a customized handbook for each of our employees, who would download it from our web site. Additionally, the handbook also needs to contain some glitzy artwork that must be printed in a tightly controlled format. To do the job properly, we decide we need a to use a tool fancier than HTML. The right tool for this job, we decide, is PDF.

What is PDF? PDF stands for Portable Document Format. It lets a user view documents regardless of platform when Adobe Acrobat or an Acrobat plug-in for the user's browser is properly installed.

There seems to be a lot of confusion on how to get ColdFusion to work with PDF files. In this article we'll discuss two ways to accomplish this mystifying task.

Adobe FormsDocument
In our example we'll create a predefined PDF document that works hand-in-hand with a forms document. This is similar to using a Word merge file: You have a source document where the merge fields are placed and a secondary document that contains your data. The PDF file is the equivalent of the source document while the forms document is the data file.

Let's examine an example application, EmployeeManual.cfm

 WHERE       EMPID = 'A100'

<cfsetting enablecfoutputonly="Yes"
<cfcontent type="application/fdf">

<cfoutput query="SP">%FDF-1.2
1 0 obj <<
/FDF <<

<< /T (EMPID) /V (#EMPID#)>>
<< /T (Name) /V (#NAME#)>>
<< /T (Title) /V (#TITLE#)>>

/F (http://localhost/article/Emanual.pdf)
<</Root 1 0 R>>

As you can see, we start off with the usual <cfquery>, which will retrieve the employee's information. In this case, we are retrieving one record from our database. Next we have ColdFusion's <cfcontent> tag, which informs the browser of the file type to expect: We expect a document of type-FDF. An FDF document is an Adobe Acrobat Forms Document. (You can define and view what each document type means by inspecting the file types under My Computer.)

Now we use <cfoutput> to setup the actual FDF document. The first part of the FDF tells Adobe that this an FDF file. The /T /V tags assign the field values from our query to the field names of the PDF document. The /F tag tells the FDF which PDF document it is associated with. In this case it is Emanual.pdf. If you opened this file in Adobe PDF Writer you would see that this document has three form fields called EmpID, Name and Title, as are found in our EmployeeManual.cfm.

PDF on thefly
By now you're probably asking yourself: But how can I output my entire query and create it on the fly?

There are a couple of ways to create a PDF file. The method that I use is pretty fast and easy. I cheat.

Let me explain: I create a blank PDF document in Adobe PDF Writer. You can also create a little fancier PDF by placing logos in the actual document. Next, open up the PDF file in a browser and view the document's source code. I copy and paste the source code into my ColdFusion application. This saves me the time of becoming proficient with the many PDF program codes. These codes are very tedious and complex — one little mix-up can make the PDF document inoperable.

A good working knowledge of PDF programming would definitely help you. Adobe has good references for this information and a listing of these codes.

Back to our example: After pasting the initial code in my ColdFusion document I work my way out of the document, incorporating the data, then adding bits and pieces of graphics here and there.

Let's view the PDF code
A PDF file is made up of four sections: a one-line header, a body, a cross-reference table, and a trailer.

Figure 2, the header, which specifies the PDF version number

<cfoutput query="SP">%PDF-1.0

The header line of a PDF specifies the version number of the PDF, as in Figure 2

Figure 3, the code that places the data in the PDF

<CFSET LC = 650>
<cfloop query="SP­>
	/F1 14 Tf
	50 #EVALUATE(LC)# Td (#EMPID#) Tj

	/F1 14 Tf
	100 #EVALUATE(LC)# Td (#NAME#) Tj

	/F1 14 Tf
	200 #EVALUATE(LC)# Td (#TITLE#) Tj

	<CFSET LC = LC - 20>

The body consists of a sequence of indirect objects representing a document. I'm not going to go into all the objects that can be placed in the body of a PDF at this time. For a detailed list get a good reference manual. Here we'll just take a look at the code that places our data in the PDF. See Figure 3.

As you can see, we are cycling through our query to produce our result set.

The /F1 14 tells the PDF to use a font size of 14.

The F1 says to use that particular font, which might be Arial in our case.

The 50 #Evaluate(LC)# is the x and y coordinates of where the text will be placed in the document. You will have to keep track of all line counters.

Figure 4, the cross-reference table

0 8
0000000000 65535 f
0000000009 00000 n
0000000074 00000 n
0000000120 00000 n
0000000179 00000 n
0000000322 00000 n
0000000415 00000 n
0000000445 00000 n

The TD and TJ are for used for trailing and spacing.

The next section is the cross-reference table. As Figure 4 shows, it contains indexing information about the objects in our document.

Our document will end with the trailer and, appropriately enough, End Of File markers.

I hope this makes the use of PDF and FDF a little easier to understand. Used with ColdFusion, PDF can be a practical as well as powerful addition to your tool chest.

More Stories By David Adress

David Adress puts his strong knowledge in ColdFusion, Powerbuilder, Visual Basic and Oracle to work for Intrasphere Technologies, Inc. a leader in providing a full range of services for engineering enterprise-class web applications. David has acquired more than 10 years' experience in the computer field since receiving a B.S. in mathematics/computer science from Touro College (New York).

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.