24

Hello I am using pdftk to generate a PDF based on a form that is submitted.

I have everything working fine until here. Which is adding an image of a signature. I am using signature pad which works great to generate the image file of the signature. Now i am trying to add that signature image to the PDF? Does anyone know if this is even possible to do with pdftk? i don't see the option to do that. Or if its even possible to attach to the image to a form filed that i have in the PDF?

Brad Hazelnut
  • 1,603
  • 5
  • 21
  • 33
  • 1
    Isn't this closely related to http://stackoverflow.com/q/20235541/2870069 ? – Jakob Dec 12 '13 at 21:32
  • Thank you, that looks very promising i will try and test it out, but the only thing, is i want to be able to put the image in a certain spot in the middle of the page, i don't know how well this is going to work – Brad Hazelnut Dec 12 '13 at 22:09
  • If the image position is fixed it doesn't matter where to put it. Simply use the `--offset` part to place it at the desired position. – Jakob Dec 12 '13 at 22:39
  • i've been messing with this, and i have a 3 page pdf and when i run it, for some reason it displays it on all 3 pages, i only want to display it on the last page, how can i do that? – Brad Hazelnut Dec 13 '13 at 17:37
  • If you only want the image on one page, I'd split your 3 page document into two documents, run the stamp only on one document, then recombine the two pages. PDFtk can do that also. – Suzanne Feb 14 '18 at 15:28

4 Answers4

52

First convert the image to PDF

convert image.png image.pdf

Then scale up and offset the image using pdfjam (another free tool)

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

Then combine both PDFs using pdftk

pdftk text.pdf stamp image.pdf output combined.pdf

You may need to download STAMPtk if you need to position the image and add it to only one page in the general PDF, but this one you have to pay for it.

You can download STAMPtk from here http://www.pdflabs.com/tools/stamptk-the-pdf-stamp-maker/

I hope it helps!

Salvi Pascual
  • 1,788
  • 17
  • 22
  • 5
    Just adding installation notes for Linux (it is quite straightforward). PDFjam: `apt-get install texlive-extra-utils`, PDFtk: `apt-get install pdftk`. – exmaxx Apr 19 '16 at 18:39
  • 3
    And installation notes for **Mac**. ***PDFjam***: `brew install homebrew/tex/pdfjam`, ***PDFtk***: Homebrew is currently not supported (however you can find guides on SO on how to install it through Terminal) so use the official download page https://www.pdflabs.com/tools/pdftk-server/ or this link if you are on OSX 10.11.4 (and PDFtk just freezes and does nothing) https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg – exmaxx Apr 19 '16 at 18:45
  • For those in the US, you can replace `a4paper` with `letterpaper` to get 8.5"x11" which comes from the [latex geometry](http://texdoc.net/texmf-dist/doc/latex/geometry/geometry.pdf) package – gene_wood Oct 08 '18 at 22:40
  • 2
    I see this type of answer a lot, but it seems more straightforward to me to divide the PDF into several pages, sign the page you want, then re-combine? – lmat - Reinstate Monica Oct 30 '19 at 14:24
  • 1
    indeed, after the pdfjam thing, use `pdftk text.pdf cat 5 output page5.pdf` to extract page 5, then `pdftk page5.pdf stamp signature.pdf output signed.pdf`, then `pdftk A=text.pdf B=signed-page5.pdf cat A1-4 B A6-end output signed-text.pdf`. Otherwise, see comment below by @Tanupryia to add blank pages signature.pdf – Christophe Priieur Jan 07 '21 at 15:37
8

In our case, we have to add dynamic signature in pdf. I have implemented it using pdftk and imagemagick libraries. (Require to install pdftk and imagemagick)

  1. First convert the image to pdf as: (Use magick instead of convert for windows)

exec("convert signature-image -resize 26% -transparent white -page a4+25+102 -quality 75 outputs/stamp.pdf");

Descriptions:

  • resize : adjust the size of image.
  • transparent makes image background transparent
  • page : set page to a4 and (25,102) sets the position of image pdf from left and top.

2.Then execute following command:

exec("pdftk main.pdf multistamp stamp.pdf output outputs/final.pdf");

Tanupriya
  • 159
  • 1
  • 9
7

pdfjinja for Python

https://github.com/rammie/pdfjinja

This library will allow you to to add images to a signature or button object in your PDF, without the need for merging or vector location information.

1. Add signature element to your PDF template

Adobe Pro allows creation and modification of PDF fillable forms. Go to Tools>Forms>Edit, then from the Add New Field dropdown, choose Digital Signature.

After placement, go to the properties of the Digital Signature element.

Property Settings

In the Tooltip property, add

{{ Sig | paste }}

Save and close.

2. Save your signature image as a jpg or png

You may need a separate method to retrieve signatures as images, and place in an accessible folder.

3. Add method to your Python script

from pdfjinja import PdfJinja

pdf_jinja_object = PdfJinja("path_to_pdf_template")

filled_out_pdf = pdf_jinja_object({
    'firstName': 'John',
    'lastName': 'Smith',
    'sig': 'path_to_signature_image',
})

filled_out_pdf.write(open("output_file.pdf", 'wb'))

This should give you a form with your signature image placed in the location created in your template.

goWithSwagger
  • 1,557
  • 1
  • 17
  • 19
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18892890) – blurfus Feb 22 '18 at 00:40
  • Thanks, I added a 3 step walkthrough to help. – goWithSwagger Feb 22 '18 at 17:03
  • This should be the right answer. Generate a signature.pdf is more a ridiculous joke than a real answer. And paying for some script to do that? That's just hilarious. – pmourelle Apr 27 '18 at 20:33
  • Tiny correction: on 'sig' the value should be the path to the image: `os.path.join(dirname, './sig.png')` although I'm not able to make it work. – pmourelle May 01 '18 at 20:15
  • Good catch, I updated that part. See if you can set a breakpoint to look at the "Sig" property. It should have the full path to the exact image you want. – goWithSwagger May 02 '18 at 14:50
3

First convert the image to pdf as: (Use magick instead of convert for windows) exec("convert signature-image -resize 26% -transparent white -page a4+25+102 -quality 75 outputs/stamp.pdf");

Descriptions:

resize : adjust the size of image. transparent makes image background transparent page : set page to a4 and (25,102) sets the position of image pdf from left and top. 2.Then execute following command:

exec("pdftk main.pdf multistamp stamp.pdf output outputs/final.pdf");

BUT TO LOCATE SIGNATURE TO SPECIFIC POSITION ADD SOME BLANK PDF BEFORE SIGN PDF AS SHOWN BELOW:

exec("convert signature-image -resize 26% -transparent white -page a4+25+102 -quality 75 outputs/stamp.pdf");
            exec("convert xc:none -page A4 outputs/blank1.pdf");
            exec("convert xc:none -page A4 outputs/blank2.pdf");
exec("convert xc:none -page A4 outputs/blank3.pdf");
            exec("pdftk outputs/blank1.pdf outputs/stamp.pdf outputs/blank2.pdf  outputs/blank3.pdf cat output outputs/sign.pdf");
          exec("pdftk main.pdf multistamp outputs/sign.pdf output outputs/final.pdf");
Tanupriya
  • 159
  • 1
  • 9