Generate Password Protected PDF in Ruby on Rails with WickedPDF.
Tags: ruby, rails, pdf, security
Published: 2020-02-06 11:54:17

![Password Protected PDF in Ruby on Rails with WickedPDF](https://miro.medium.com/max/6379/1*CRTosiUbi-Y-ifmob_gyxQ.png)

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](https://www.varonis.com/2019-data-risk-report/) 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](https://wkhtmltopdf.org/) 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](https://www.solutelabs.com/ruby-on-rails-development-services):

1. wicked_pdf
2. rmagick
3. Prawn

###**Step1: Create the PDF from an HTML string using the [Wicked PDF](https://github.com/mileszs/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.

![-------](https://cdn-images-1.medium.com/max/800/1*RWWlwquZCEvAuV4bo_WL3g.png)

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](https://linuxconfig.org/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

```sh
<policy domain="coder" rights="read|write" pattern="PS" />
<policy domain="coder" rights="read|write" pattern="PDF" />
```

![-------](https://cdn-images-1.medium.com/max/800/1*Fat5xwDlacVYaO37qh5OaA.png)

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](https://blog.solutelabs.com/rails-6-updates-6a69c6b3d5c2) that generates PDFs for various functionalities.

![-------](https://cdn-images-1.medium.com/max/800/1*SedANZ5SENp1TUjIikaqPA.png)

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


I'm Manthan -a Software Developer at [Solutelabs](https://www.solutelabs.com/). An ordinary boy with extraordinary dreams. I am enthusiastic about writing clean, efficient and accessible code in a functional framework.

Apart from work, I like to play cricket, watch movies and spend time with family.