Watermark your PDFs


Tue 26 April 2016 By Thibaud Arguillere

The Nuxeo Platform already natively does quite a lot around PDFs. “Already” and “natively” translate to “you don’t have to do anything, the features are here”, which is always a cool way of working. It lets you focus on other features of your application. For PDF manipulation, here's a short (but not exhaustive) list of available features: Full text extraction/indexing, conversion/rendition to pdf, preview, template rendering, and digital signature.

Now let’s say you want to watermark a PDF. For example, you want to watermark this PDF (on the left) with that “Draft” image:

Watermark the PDF (on the left) with the “Draft” image (on the right)

For this we will use nuxeo-pdf-utils, a plug-in full of cool operations to manipulate your PDFs within Nuxeo (internally, it uses PDFBox, a widely use PDF tool). In this article, we will use its PDF:Watermark with Image operation, but you can do much more with this plug-in (for instance, watermark with a simple text or with another PDF, extract pages, encrypt, decrypt, etc.)

Also, it is available via our continuous integration platform. That means you don’t have to download the sources and build it. All you have to do is go its build page and download the .zip package. You can then install it on your server like any other Nuxeo Package.

To implement our watermarking example using Nuxeo Studio, we first have to add the JSON definition of the operation to the “Automation Operations” registry of your project. This will make it available in the list of operations when writing an automation chain. An easy way to get the JSON definition of an operation is to start the server and go to {yourserver}/nuxeo/site/automation/doc once the plug-in is installed. The page displays a list of each and every operation available in your server. You will notice that there are more operations than the ones exposed in Studio. They come from various plug-ins or are just not exposed by default in Studio. So, you have to find the PDF:Watermark with Image operation in the list and copy its JSON definition.

Operation - get the JSON definition

Then you have to add it to the “Automation Operations” registry in your project.

You can now build the operation that will add the watermark to a PDF. Look how easy it is:

03-TheChain

In this example, paths and values are hard-coded, and we downloaded the file. In your business app, you probably will have something more dynamic, and maybe expose the watermarked PDF as a rendition, or save it in another document and the process will be the same. The point is that it is that easy.

The resulting PDF is as expected, watermarked on every page:

The Result

PDFs are fun after all, aren’t they?


Tagged: Document Management, How to, Nuxeo Plugin, Nuxeo Studio