ColdFusion 8.0.1 - Easier to add PDF Watermarks

ColdFusion 8.0.1 makes it much easier to add watermarks to PDF documents. In the past you had to either use an image or another PDF, but now you can simply pass in text. You can even pass in styled text. Here is a simple example.

First we generate a PDF dynamically. <cfdocument format="pdf" name="mypdf"> <cfloop index="x" from="1" to="9"> <p> Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. Lorem impsum delorem battlestar galactica begins tonight and it kicks butt. </p> </cfloop> </cfdocument>

Now let’s add the watermark:

<cfpdf action="addWatermark" text="<b>TOP SECRET!</b>" source="mypdf" foreground="true">

The foreground attribute is critical for PDFs made with cfdocument. If you don’t use it - your watermark will be behind the text.

Now I can serve the PDF to the user:

<cfheader name="content-disposition" value="attachment; filename=""test.pdf"""/> <cfcontent type="application/pdf" variable="#toBinary(mypdf)#">

Note the toBinary thing. This is an bug that was not fixed in CF8. Even though “mypdf” is a PDF document, when I performed the addWatermark action, I converted what was pure binary data into a PDF object recognized by ColdFusion. If I had used destinaiton= in the cfpdf tag, it would have worked fine, but I wanted to serve the document directly to the user, so I had to wrap it with toBinary.

Anyway - even with that little hitch at the end, it’s far easier now to add watermarks to PDFs!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments