As the importance of security grows, a huge number of products and businesses keep locks on their data. The first line of defense against data breaches could be to keep the PDFs password protected — especially in sensitive areas such as Healthcare and Fintech.
Statistics by Varonis showcase why every enterprise needs to have password-protected PDFs:
- 53% of companies surveyed had more than 1,000 sensitive files open to every employee
- 22% of all folders were accessible to all employees
- 15% of companies found more than 1,000,000 files were available to every employee
- 17% of all sensitive files were accessible by the entire staff
- Financial and manufacturing service providers have the highest percent of exposed sensitive files at 21%
- Financial services had an average of more than 3 million exposed sensitive files
- Healthcare, Pharma, and Biotech had an average of more than 1 million exposed sensitive files
Looking at the statistics above, it’s time to protect your PDFs with passwords irrespective of your industries when dealing with sensitive data using PDFs.
Having worked on such products — in this article, I will show how you can generate password-protected PDFs in Rails using the wicked PDF gem.
How does it work basically?
Before we begin, understand that PDFs are generated in essentially four simple steps:
- You first create the PDF you need in HTML
- Use wkhtmltopdf to convert the said HTML into PDF
- Convert the PDF into images
- Create a PDF from the images and password protect the PDF
This convoluted process is needed because of wicked pdf, which doesn’t provide the functionality to add a password while creating the PDF and the prawn gem doesn’t accept HTML to create password-protected PDFs :)
And, as you might have guessed — wicked PDF uses wkhtmltopdf, an open-source (LGPLv3) command-line tool to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require a display or display service.
Steps to generate password-protected PDFs
Before we begin, you will need to install the following gems in your Ruby on Rails application:
Step1: Create the PDF from an HTML string using the Wicked PDF gem and store it in a temp file.
In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked PDF take care of the hard stuff.
Wicked PDF provides a PDF file to the users converted from HTML.
Above we have generated pdf from string using WickedPdf and store it in a temp file. It’s easy a pie!
Now we move to how we can make it password protected :)
Step 2: Now, we have to generate images from the temp pdf
ImageMagick helps in generating images from the PDF file. You can learn how to install ImageMagick on your device from the below link.
For a Heroku server, use ImageMagick build pack.
If image generation gives unauthorized error then change below in the /etc/ImageMagick-6/policy.xml file
As mentioned before, we generate the images for the prawn gem as it doesn’t handle HTML to PDF conversion like wicked PDF does!
Step 3: Now, we will generate the password-protected PDF using images and a Prawn gem.
Prawn is a Ruby on Rails (RoR) library that generates PDFs for various functionalities.
You can store the encrypted file in the database with a password value — and Voila! you’re done.
I hope this article will help you to generate password protected PDF in Ruby On Rails. I hope someday wicked pdf allows us to generate password-protected PDFs so that someday my article would become irrelevant! :)
Want to make your startup journey smooth? YS Education brings a comprehensive Funding Course, where you also get a chance to pitch your business plan to top investors. Click here to know more.