Did you ever had the need to attach a digital certificate to a pdf? No? Goodbye then!
Naah, just kidding 😅. In today’s study case we are going to find out what digital certificates are, why you should care about them and when you should put them to good use.
Background story
Let’s say I was a contractor, providing services to your customers through a web application. If I am going to sell my services online, then there will surely be documents involved, for example contracts. In a legal matter I need to be able to prove that it was my application through which I was selling my services and not some other malicious website. So I need to prove my application’s authenticity when sending my contracts as pdfs to the customers. That’s where we come to the next point, the digital certificates.
Digital certificates
You can think about digital certificates as passports. A passport contains a unique identification number, information about the person and it is issued by a legal authority.
The difference is that when digital certificates are verified, the issuing authority or the certificate authority, is the one who is verified.
Solution
Now 77.5% of all web servers are running on php and you must be thinking “How in the world can I attach that digital certificate to my documents?”.
I am happy to introduce you to two packages which are going to help us do the job.
We will combine FPDI, the documents importer and TCPDF, the documents signer in order to reach our goal. Let’s move on to the next step, the installation.
Instalation
To install FPDI and TCPDF we are going to need 2 commands:
In applyDigitalSignaturewe create a new Fpdi instance.
We open a stream resource to our Contract.pdf and set Fpdi to use this resource.
We will iterate through all pdf pages, import them, set the signature using the digital certificate and finally store the new content to a new SignedContract.pdf file.
But we don’t have the certificate yet, so let’s create it.
Creating the certificate
First we will create a new certs folder under storage/app/ and then we will create the digital certificate using the following command
I am going to test this using Laravel’s php artisan tinker command
Done, now let’s have a look at the new SignedContract.pdf using LibreOffice Draw since it can detect such digital signatures
We have confirmation! Our certificate exists! Don’t worry about the warning, it is displayed because we created a self signed certificate. For the purpose of this demo this certificate is going to be enough. Now let’s display the certificate details. You can do this in LibreOffice by clicking File>Digital Signatures and voila
Our certificate details, as we expected. Awesome! It works 🎉.
Conclusion
Digital certificates and CA’s play a very important role when verifying the identity of an application (server) and not just only that, there are also other types like client and email certificates. They show if an application is trustworthy.
That is it for today, in our next blog, we are going to explore how to make fillable documents using the Google Docs API.
Now make a break, go outside, enjoy life and as always, happy coding!