For those who are in the middle of searching for a free tools to put digital signature on a PDF file, here are two options that I had ever tried to use :
- lsnepomuceno/laravel-a1-pdf-sign. It is a plugin for laravel and written in PHP. This library inherits methods from FPDI and FPDI inherits some methods from TCPDF. The documentation is quite easy to understand and there are several examples how to use it to sign PDF file. The drawbacks are, first it does not provide any function to validate the signature and data integrity. Second, this library does not let you to sign the PDF file incrementally. Well, I customized the code so I can check the validity of the signature and the PDF file's integrity. But I still failed to make this library to allow the PDF File being signed incrementally. At this point I stopped trying customizing this library and look for an alternatives with following requirements:
- it should be an executable script (Go, bash, python, etc...)
- it should be able put a custom QRcode image for the stamp
- Python Pyhanko. My search ended with PyHanko. It is an cli, so it matches with my first requirement. But...I was still not sure whether it can satisfy my second requirement.
I forget to mention, pyhanko is able to generate QRcode as well as the stamp. But I need to put a logo on the QRcode image and unfortunately pyhanko cannot do that. That's why I put "custom" QRcode on this post's title.
So...after several attempt, asking chatGPT and Gemini I still could not find any solution. And this is the lesson learned....if chatGPT and Gemini can not help you any further, try to read the documentation diligently hehehe....so the solution is right over there.
Again, for those who are looking for Digital Signature tool for PDF that allow you to sign it incrementally and put a custom QRcode for the Stamp.....here are the steps that might help you:
For your information, my server is ubuntu
1. install python (latest version):
sudo apt install python3
2. make a virtual environment for python
python3 -m venv /path/to/virtual-env
3. activate the virtual environment
source /path/to/virtual-env/bin/activate
4. install pyhanko latest version (0.26.0) and Pillow
pip install pyhanko==0.26.0
pip install Pillow
5. go to your project directory
cd /path/to/your/project/pyhanko
6. prepare the pdf file that you want to sign, the QRcode image and the pkcs12 file (.pfx)
7. I put my passphrase for .pfx file inside a text file. I named it "mypass"
8. make a config file here, for example pyhanko.yml. Put this config inside:
stamp-styles:
justqr:
type: text
stamp-text: ""
background: "/path/to/your/project/pyhanko/myqrcode.png"
background-opacity: 1
7. sign the PDF file using this command (I used pkcs12, if you use another method you can adjust the signing method)
PYHANKO_CONFIG=pyhanko.yml pyhanko sign addsig \
--no-strict-syntax \
--field 1/200,10,300,110/sig1 \
--style-name justqr \
pkcs12 \
--passfile mypass \
to_sign.pdf \
signed.pdf \
mypkcs12.pfx
--no-strict-syntax : well...it was a help from chatGPT hehehe...there was an error and this is the solution hehe
--field page/x1,y1,x2,y2/nameOfSigField: --field is to define the location where you want to put the stamp.
page: the page number
x1,y1 define the coordinate of the bottom left edge of the stamp
x2,y2 define the coordinate of the top right edge of the stamp
nameOfSigField: it will be used to put the coordinate in byte of the signature. I put sig1 for the first signature, sig2 for the second signature and so on.
I'm sure that the other options are self explained, right?
The stamp will looks like this, FYI the QRcode image was generated by another tools
Oh if you want to put QRcode and sometext beside the QRcode, for example the name of the signer and the timestamp when it is signed, you can change the config file into this.
then change the value of --style-name options to "qrandtext" on the cli command:
PYHANKO_CONFIG=pyhanko.yml pyhanko sign addsig \
--no-strict-syntax \
--field 1/200,10,500,110/sig1 \
--style-name qrandtext \
pkcs12 \
--passfile mypass \
to_sign.pdf \
signed.pdf \
mypkcs12.pfx
your stamp will look like this
From the config file I'm sure you can recognize that I use the background to display the custom QRcode. I set the opacity to 1 so it will be shown as a solid image.
I hope this help you somehow.