Adding an image to a pdf with pdftk
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 herehttp://www.pdflabs.com/tools/stamptk-the-pdf-stamp-maker/
I hope it helps!
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)
- 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");
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 PdfJinjapdf_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.