9

I am currently writing an application in which wone of the processes is to stamp an existing 1-page pdf-document with an image provided by the user. The stamp needs to be scaled and position correctly onto the pdf.

I've successfully followed the incstructions in Kurt Pfeifle's answer to Stamp PDF file with control for position of stamp file .

In the answer, Kurt

  1. Creates a stamp on the fly using ghostscript.
  2. Creates an empty A4-sized-pdf, with the stamp position in.
  3. He then merges the newly created pdf, with the original pdf using pdftk

As I said, this all works great. However, if I do the same process with my own image-file(converted to pdf), something goes wrong in the second step with the sizing in the second step. The sizing in the command seems to be ignored, and instead, the pdf gets the same size as the image. Se output below for a comparison of original command with original stamp as pdf and my modified command using a converted image.

Original working command:

gs \
  -o A4-stamp.pdf \
  -sDEVICE=pdfwrite \
  -g5950x8420 \
  -c "<</PageOffset [280 790]>> setpagedevice" \
  -f stamp-small.pdf

Output of original command

Modified command with image

 gs \
  -o A4-image.pdf \
  -sDEVICE=pdfwrite \
  -g5950x8420 \
  -c "<</PageOffset [280 790]>> setpagedevice" \
  -f image.pdf

enter image description here

As can be seen, the size and ratio is all wrong, and should match the original.

The original stamp-small.pdf (from original answer) can be generated like this:

gs \
  -o stamp-small.pdf \
  -sDEVICE=pdfwrite \
  -g3200x500 \
  -c "/Helvetica-Bold findfont 36 scalefont setfont" \
  -c "0 .8 0 0 setcmykcolor" \
  -c "12 12 moveto" \
  -c "(This is my stamp) show" \
  -c "showpage"

The image I used in the command is the following, but the same thing happens with any image I have tried, after converting the image to pdf:

enter image description here

convert image.png image.pdf

Runar
  • 245
  • 3
  • 17
  • The image on Dropbox cannot be accessed. – mkl Jun 11 '17 at 14:30
  • @mkl Thank you for clearing that up. I have edited the question. It seems to give the same result with any image. – Runar Jun 11 '17 at 19:05
  • If I am correct in reading through the example provided by Kurt: the file after the flag -f is used as "input". However, your issue is with the canvas size/ratio being off. What are the dimensions of your input file? Did you scale it to its appropriate dimensions beforehand? – Uvar Jun 20 '17 at 11:44
  • @Uvar I've tried multiple image-files, with various sizes, even tiny icons of 10x10 pixels, all with the same result. _mihaimm_ seems to be right about this being related to transparency. – Runar Jun 20 '17 at 14:52

2 Answers2

4

There seem to be some issues related to:

  • transparency in your png image (transparency is not supported by PDF)
  • convert output from jpg to pdf (some kind of bug in convert?)

In short, without going into the details of the problems, you can use

  • convert image.png -size 640x562 xc:white +swap -compose over -composite image.jpg - this removes png transparency to white (as background) and converts image to jpg (note the -size, this is the same as the image you added in this post, but should be stated to be the correct one for your stamp)
  • img2pdf image.jpg -o image.pdf - properly add jpg image to pdf
  • gs -o A4-image.pdf -sDEVICE=pdfwrite -g5950x8420 -c "<</PageOffset [100 500]>> setpagedevice" -f image.pdf
mihaimm
  • 521
  • 2
  • 6
  • You are referring to `img2pdf`, but which one of them? I found numerous results. Could it be this: https://gitlab.mister-muffin.de/josch/img2pdf ? – Runar Jun 20 '17 at 14:36
  • I tried this route, and it does add the image as I want. However, without transparency, the result is not too good. Anyway, I am closer to a solution. – Runar Jun 20 '17 at 14:50
  • Still sounds like it should be the accepted answer, even though transparency isn't supported. – zanlok Jun 20 '17 at 15:07
  • @zanlok Yes, I agree. I'll just hang on a bit more, to see if anybody else has an answer to this, or if mihaimm wants to elaborate. – Runar Jun 20 '17 at 15:25
  • Don't forget to add resolution, example 300dpi: `gs -o A4-image.pdf -sDEVICE=pdfwrite -g5950x8420 -r300x300 -c "<> setpagedevice" -f image.pdf` . Additional info PageOffset is left bottom, units in points – fearis Nov 22 '22 at 18:22
2

The best way currently I found is:

1, Convert your image to pdf, e.g.

rsvg-convert -f pdf -o stamp.pdf in.svg

2, Change ratio and position of the image, e.g. to the right top (7cm, 12cm)

pdfjam --paper 'a4paper' --scale 0.3 --offset '7cm 12cm' stamp.pdf

3, Overylay stamp to some page, e.g. page 4

qpdf in.pdf --overlay --to=4 stamp.pdf --out.pdf
John Chen
  • 173
  • 10