Generate Password Protected PDF in Ruby on Rails with WickedPDF.

Here is how we can generate password-protected PDFs in Rails using Wicked PDF, ImageMagick, and Prawn gem in Ruby on Rails w/ code.

10th Feb 2020
  • +0
Share on
close
  • +0
Share on
close
Share on
close

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:

  1. You first create the PDF you need in HTML
  2. Use wkhtmltopdf to convert the said HTML into PDF
  3. Convert the PDF into images
  4. 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

Preparations!

Before we begin, you will need to install the following gems in your Ruby on Rails application:

  1. wicked_pdf
  2. rmagick
  3. Prawn

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.


Wicked PDF

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.


How to install ImageMagick 7 on Ubuntu 18.04 Linux


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


image generation


image

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.


Prawn

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! :)

This is a YourStory community post, written by one of our readers.The images and content in this post belong to their respective owners. If you feel that any content posted here is a violation of your copyright, please write to us at mystory@yourstory.com and we will take it down. There has been no commercial exchange by YourStory for the publication of this article.
  • +0
Share on
close
  • +0
Share on
close
Share on
close