Adding an image to a pdf with pdftk

北城余情 提交于 2019-12-02 21:02:04

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!

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.

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.

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");

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!