Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stamp a pdf with an image

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

like image 353
Runar Avatar asked Jun 10 '17 11:06

Runar


People also ask

How do I add a picture to a stamp in PDF?

To add an image to a PDF one time only, simply paste the image into the document. Pasted images have the same characteristics as other stamp comments; each includes a pop-up note and editable properties. Open the Stamps Palette by doing one of the following: Choose Tools > Stamp > Stamps Palette.


1 Answers

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
like image 92
mihaimm Avatar answered Nov 26 '22 18:11

mihaimm