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?
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)
- 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");
来源:https://stackoverflow.com/questions/20531079/adding-an-image-to-a-pdf-with-pdftk