Multipage PDF watermarking

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
miragium
Posts: 3
Joined: 2019-04-15T05:09:17-07:00
Authentication code: 1152

Multipage PDF watermarking

Post by miragium »

Hello IM people,

I'm on linux and I want to put together a command that can take a pdf from the standard input, watermark it, and write it on the standard output. I turned to ImageMagick because of all the features and also because the rasterisation of pdfs was perfect for my use case.

After skimming this forum and some other ressources, I managed to put together this command :

Code: Select all

convert pdf:- -write mpr:base \
\( mask.png -shade 30x30 -blur 0x1 +level 45%,55% -write mpr:water +delete \) \ # create the watermark logo from a black and white image
\( +clone -tile mpr:water -draw "color 0,0 reset" -write mpr:tile +delete \) \ # tile it to the input image
+delete \
mpr:tile mpr:base \ # the order is important here, so that the compose effect can work the way I want
-compose Overlay -composite \
pdf:-
This does what I want, but only work for single page document. I can't find how to make it treat all the pages of a document. I know about the use of bracket to select pages, but I don't know how to apply it here.

Is it possible to do this with ImageMagick (I'm using 6.8, but could deploy 7 if necessary), or should I just split and merge the pdfs with something like pdftk ?

The latter solution would be suboptimal for me, as this would mean I would need to create temporary files, and because the pdfs are coming from and going to another machine through a http service, this would slow down the process.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multipage PDF watermarking

Post by snibgo »

"-composite" works with two input files: a destination and a source.

You want multiple destinations and a single source, or single destination and multiple sources. "-layers composite" does that. It needs a "null:" image between the destination(s) and source(s). See http://www.imagemagick.org/script/comma ... php#layers

The command could be something like this (untested):

Code: Select all

convert pdf:- -write mpr:base -delete 0--1 \
 \( mask.png -shade 30x30 -blur 0x1 +level 45%,55% -write mpr:water +delete \) \ # create the watermark logo from a black and white image 
\( +clone -tile mpr:water -draw "color 0,0 reset" -write mpr:tile +delete \) \ # tile it to the input image 
+delete \
 mpr:tile mull: mpr:base \ # the order is important here, so that the compose effect can work the way I want 
-compose Overlay -layers composite \
 pdf:-
snibgo's IM pages: im.snibgo.com
miragium
Posts: 3
Joined: 2019-04-15T05:09:17-07:00
Authentication code: 1152

Re: Multipage PDF watermarking

Post by miragium »

Thanks. This was not enough to do it, but put me on the right track, and I think I'm starting to grasp how this all work.

To correctly handle multiple pages, I also had to change some of the other delete and the clone like you did with the first delete, to correctly manipulated all pages.

Here is the final command :

Code: Select all

convert pdf:- -write mpr:base \
\( mask.png -shade 30x30 -blur 0x1 +level 45%,55% -write mpr:water +delete \) \
\( -clone 0--1 -tile mpr:water -draw "color 0,0 reset" -write mpr:tile -delete 0--1 \) \
-delete 0--1 \
mpr:tile null: mpr:base \
-compose Overlay -layers composite \
pdf:-
I'm wondering if I could make it faster (the pdfs are sometime very large), but at least it's working.

If anyone has tips I'm listening, but anyway my main issue is resolved. Thanks again.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multipage PDF watermarking

Post by snibgo »

Are all the PDF pages the same size? Your mpr:tile will contain one image per page. If the pages are the same size, then all the mpr:tile images will be identical, so effort has been duplicated. You can reduce this duplication by cloning just one page.
snibgo's IM pages: im.snibgo.com
miragium
Posts: 3
Joined: 2019-04-15T05:09:17-07:00
Authentication code: 1152

Re: Multipage PDF watermarking

Post by miragium »

Sometimes not, so I will leave it like this.

I tried to avoid the unnecessary writes and deletes, but it didn't made any noticeable difference.

Also, will doing that, I noticed that the Overlay effect that I'm using doesn't have the same result between version 6 and 7. Is that normal ? Should I open an new topic about this ? If this is normal, where can I find updated explanation of the different compose algorithm (I'm currently using this ressource which use v6.6 apparently, and I find the dest / src grayscale graphs very useful).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Multipage PDF watermarking

Post by snibgo »

Cloning, and writing to "mpr:", create new image structures with a pointer to the original pixels so don't actually copy pixels, so they are fast.

"-compose overlay" should have the same effect between v6 and v7. If they don't, please post a simple example in bugs, with the exact command, and input and output images.
snibgo's IM pages: im.snibgo.com
Post Reply