Wednesday, September 2, 2009

PDF's 101

I wanted to share my first experience with pdf's and how simple ColdFusion makes working with. Previously, when I had to do something with a PDF it was changing some styles on a .cfm that's content was wrapped in cfdocument tags, so I didn't really learn much. I got put on a project that had us generate pdfs and learned a lot from the experience. When I first started working on the project I was nervous because I didn't know XML or XSLT, but quickly realized they are not hard at all. Here is a small example of my process for creating a pdf. My goal was no files, which means I save the pdf in the database, but in this example I will show where you can either create a file or save it to the database.

First we have some data that we want to display on a PDF. I like snowboarding so I made an array of structs with data regarding each boarder. Mostly we would have a whole bunch of queries who's data we would to display but I just want to keep it simple.

<cfset boarders = arrayNew(1)/>

<cfset boarder = structNew()/>
<cfset boarder.id = 1/>
<cfset boarder.name = "Joe"/>
<cfset boarder.board = "Forum"/>
<cfset boarder.gender = "M"/>
<cfset arrayAppend(boarders,boarder)/>

<cfset boarder = structNew()/>
<cfset boarder.id = 2/>
<cfset boarder.name = "Ben"/>
<cfset boarder.board = "Burton"/>
<cfset boarder.gender = "M"/>
<cfset arrayAppend(boarders,boarder)/>

<cfset boarder = structNew()/>
<cfset boarder.id = 3/>
<cfset boarder.name = "Jake"/>
<cfset boarder.board = "Santa Cruz"/>
<cfset boarder.gender = "M"/>
<cfset arrayAppend(boarders,boarder)/>

<cfset boarder = structNew()/>
<cfset boarder.id = 4/>
<cfset boarder.name = "Jamie"/>
<cfset boarder.board = "Nitro"/>
<cfset boarder.gender = "F"/>
<cfset arrayAppend(boarders,boarder)/>

<!---dump the data--->
<cfdump var="#boarders#" label="boarders"/>

After we have our data we need make some xml. If you haven't work with xml, think of it like html, but you can make your own tag names. Here I create a node called "boarder" which I store properties about the boarder like name, board, and gender. The xml will act as our data when working with the pdf. Later the xslt will be our styles for the pdf.

<cfset xml = arrayNew(1)/>

<cfloop from="1" to="#arrayLen(boarders)#" index="i">

<cfsavecontent variable="boarder">
<cfoutput>

<boarder>
<id>#boarders[i].id#</id>
<name>#boarders[i].name#</name>
<board>#boarders[i].board#</board>
<gender>#boarders[i].gender#</gender>
</boarder>

</cfoutput>
</cfsavecontent>

<cfset arrayAppend(xml,boarder)/>

</cfloop>

<!---dump the xml--->
<cfloop from="1" to="#arrayLen(xml)#" index="i">
<cfdump var="#xmlParse(xml[i])#" label="#i#">
</cfloop>

After we have the data the next piece is to create some xslt. Remember xslt is the styles of the xml. If you haven't worked with xslt, the basic idea is you loop through your xml nodes "the custom tags you made" and output the data. You loop through a node by doing <xsl:for-each select="boarder">. If you want the value of node you do this <xsl:value-of select="name"/>. This should get you by, you will have to search W3schools for the rest.

<cfsavecontent variable="xslt">
<cfoutput>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:for-each select="boarder">

<table border="1">

<tr>
<td>Name:</td>
<td><xsl:value-of select="name"/></td>
</tr>

<tr>
<td>Gender:</td>
<td><xsl:value-of select="gender"/></td>
</tr>

<tr>
<td>Board:</td>
<td><xsl:value-of select="board"/></td>
</tr>

</table>

</xsl:for-each>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

</cfoutput>
</cfsavecontent>

<---dump the xslt--->
<cfdump var="#xslt#">


The next piece is where we actually merge the xml and xslt together to create html. Once we have the html we can run it through cfdocument tags and get it in pdf format.

I start an array to hold the info regarding each document.

<cfset documents = arrayNew(1)/>

<cfloop from="1" to="#arrayLen(xml)#" index="i">

<cfset document = structNew()/>


I get the xml which we create earlier.

<cfset document.xml = xml[i]/>

I get the xslt which we create earlier.

<cfset document.xslt = xslt/>

I merge the xml and xslt together using xmlTransform(), a built in ColdFusion function and it will return my html.

<cfset document.html = xmlTransform(xml[i],document.xslt)/>

I wrap my html in cfdocument tags with a format of pdf. This will return me a binary object of the pdf. Earlier I mentioned about creating a file or saving it to the database...this is that point.

<cfdocument name="document.binary" format="pdf">
<cfoutput>#document.html#</cfoutput>
</cfdocument>

Since I didn't want to create any files I convert the binary object to base64 so I can save it in my MSSQL database. I can always pull the base64 from the databased and use toBinary() to convert it back to a binary object.

<cfset document.base64 = toBase64(document.binary)/>

<cfset arrayAppend(documents,document)/>

</cfloop>

<!---dump the documents array--->
<cfdump var="#documents#" label="documents">


At this point you can be done or if you want, since we have the all the pdfs in the array called documents, we can loop through them and merge them into one pdf so we can print 1 document instead of each document individually.

Pretty basic here. The only thing crazy is I loop through the documents again an apply them as source to the main pdf, named "final", I want to merge. The pointer is important because you can't directly throw the variable into the pdfparam source attribute...it requires a variable name, this took me along time to figure out.

<!--- merge documents --->
<cfpdf action="merge" name="final">

<cfloop from="1" to="#arrayLen(documents)#" index="i">
<cfset pointer = documents[i].binary/>

<cfpdfparam source="pointer"/>
</cfloop>

</cfpdf>

That's it, all the documents we created early are all merged into 1 single pdf, we can print them once. Now we just can the content tag to display the pdf to the browser.


<cfcontent type="application/pdf" variable="#toBinary(final)#" reset="no" />

Pretty harmless huh.

1 comment:

  1. Nice syntax highlighting, thief.

    You should try cfxml. Works just like cfsavecontent but automatically parses the string as XML for you and might save you some code. Also, looping the array rather than looping the length of the array reads much cleaner in my opinion.

    <cfxml variable="xml">
    <cfloop array="#boarders#" index="boarder">
    <boarder>
    <id>#boarder.id#</id>
    <name>#boarder.name#</name>
    <board>#boarder.board#</board>
    <gender>#boarder.gender#</gender>
    </boarder>
    </cfloop>
    </cfxml>

    ReplyDelete