CF 8.0.1 brought several updates and features, all of them useful. But to some of my projects there’s one that most people wouldn’t qualify as the must exciting one, for sure. It’s the ability to have CFPDF creating watermarks defined by HTML (or just text).
Previously I’ve used both DDX or iText to cover my needs but the possibility of doing it easily without having to rely on external resources it’s much appreciated both by myself and some of my customers.
And, like everything else in ColdFusion, it’s sooo easy.
For a quick example, on how you can easily create a PDF with text watermark and serve it to your users let’s create a simple a document:
<cfdocument format="pdf" name="somePDFDocument">
<!--- Lets create some paragraphs --->
<cfloop index="x" from="1" to="5">
<p>
Aliquam turpis libero, cursus eu, tristique sit amet, elementum sed, dolor. Duis convallis, velit ut hendrerit pellentesque, eros diam sagittis libero, nec sodales ligula libero sed nunc. Suspendisse tellus lorem, dapibus quis, iaculis et, pretium vel, odio. Vestibulum risus massa, porta non, suscipit et, ullamcorper eget, ligula.
</p>
</cfloop>
</cfdocument>
Ok, so now I can add a watermark saying: “Yay, I can do this now!”:
<cfpdf action="addWatermark" text="<b>Yay I can do this now!</b>" source="somePDFDocument" foreground="true">
And that’s pretty much it! If you feel like or have to you can format your watermark text using cfsavecontent and then just add the variable name to the CFPDF tag like this:
<cfsavecontent variable="myWatermark">
<b>Yay I can do this now!</b>
</cfsavecontent>
<cfpdf action="addWatermark" text="#myWatermark#" source="somePDFDocument" foreground="true">
Obviously you still need to serve the document to the user:
<cfheader name="content-disposition" value="attachment; filename=""newWatermark.pdf"""/>
<cfcontent type="application/pdf" variable="#toBinary(somePDFDocument)#">