<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manthan Andharia</title>
    <description>The latest articles on DEV Community by Manthan Andharia (@manthanandhari1).</description>
    <link>https://dev.to/manthanandhari1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F331242%2Fd1caa515-7a60-42e6-ab8a-b67e28f40787.png</url>
      <title>DEV Community: Manthan Andharia</title>
      <link>https://dev.to/manthanandhari1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manthanandhari1"/>
    <language>en</language>
    <item>
      <title>Generate Password Protected PDF in Ruby on Rails with WickedPDF.</title>
      <dc:creator>Manthan Andharia</dc:creator>
      <pubDate>Thu, 06 Feb 2020 11:54:17 +0000</pubDate>
      <link>https://dev.to/solutelabs/generate-password-protected-pdf-in-ruby-on-rails-with-wickedpdf-3nh8</link>
      <guid>https://dev.to/solutelabs/generate-password-protected-pdf-in-ruby-on-rails-with-wickedpdf-3nh8</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F6379%2F1%2ACRTosiUbi-Y-ifmob_gyxQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F6379%2F1%2ACRTosiUbi-Y-ifmob_gyxQ.png" alt="Password Protected PDF in Ruby on Rails with WickedPDF" width="800" height="853"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Statistics by &lt;a href="https://www.varonis.com/2019-data-risk-report/" rel="noopener noreferrer"&gt;Varonis&lt;/a&gt; showcase why every enterprise needs to have password-protected PDFs:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;53% of companies surveyed had more than 1,000 sensitive files open to every employee&lt;/li&gt;
&lt;li&gt;22% of all folders were accessible to all employees&lt;/li&gt;
&lt;li&gt;15% of companies found more than 1,000,000 files were available to every employee&lt;/li&gt;
&lt;li&gt;17% of all sensitive files were accessible by the entire staff&lt;/li&gt;
&lt;li&gt;Financial and manufacturing service providers have the highest percent of exposed sensitive files at 21%&lt;/li&gt;
&lt;li&gt;Financial services had an average of more than 3 million exposed sensitive files&lt;/li&gt;
&lt;li&gt;Healthcare, Pharma, and Biotech had an average of more than 1 million exposed sensitive files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;How does it work basically?&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Before we begin, understand that PDFs are generated in essentially four simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You first create the PDF you need in HTML&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://wkhtmltopdf.org/" rel="noopener noreferrer"&gt;wkhtmltopdf&lt;/a&gt; to convert the said HTML into PDF&lt;/li&gt;
&lt;li&gt;Convert the PDF into images&lt;/li&gt;
&lt;li&gt;Create a PDF from the images and password protect the PDF&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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 :)&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Steps to generate password-protected PDFs&lt;/strong&gt;
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Preparations!&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before we begin, you will need to install the following gems in your &lt;a href="https://www.solutelabs.com/ruby-on-rails-development-services" rel="noopener noreferrer"&gt;ruby on rails application&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;wicked_pdf&lt;/li&gt;
&lt;li&gt;rmagick&lt;/li&gt;
&lt;li&gt;Prawn&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step1: Create the PDF from an HTML string using the &lt;a href="https://github.com/mileszs/wicked_pdf" rel="noopener noreferrer"&gt;Wicked PDF&lt;/a&gt; gem and store it in a temp file.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Wicked PDF provides a PDF file to the users converted from HTML.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ARWWlwquZCEvAuV4bo_WL3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ARWWlwquZCEvAuV4bo_WL3g.png" alt="-------" width="572" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above we have generated pdf from string using WickedPdf and store it in a temp file. It's easy a pie!&lt;/p&gt;

&lt;p&gt;Now we move to how we can make it password protected :)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 2: Now, we have to generate images from the temp pdf&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;ImageMagick helps in generating images from the PDF file. You can learn how to install ImageMagick on your device from the below link.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://linuxconfig.org/how-to-install-imagemagick-7-on-ubuntu-18-04-linux" rel="noopener noreferrer"&gt;How to install ImageMagick 7 on Ubuntu 18.04 Linux&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a Heroku server, use ImageMagick build pack.&lt;/p&gt;

&lt;p&gt;If image generation gives unauthorized error then change below in the /etc/ImageMagick-6/policy.xml file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;policy &lt;span class="nv"&gt;domain&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coder"&lt;/span&gt; &lt;span class="nv"&gt;rights&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"read|write"&lt;/span&gt; &lt;span class="nv"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"PS"&lt;/span&gt; /&amp;gt;
&amp;lt;policy &lt;span class="nv"&gt;domain&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"coder"&lt;/span&gt; &lt;span class="nv"&gt;rights&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"read|write"&lt;/span&gt; &lt;span class="nv"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"PDF"&lt;/span&gt; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AFat5xwDlacVYaO37qh5OaA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AFat5xwDlacVYaO37qh5OaA.png" alt="-------" width="800" height="97"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As mentioned before, we generate the images for the prawn gem as it doesn't handle HTML to PDF conversion like wicked PDF does!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Step 3: Now, we will generate the password-protected PDF using images and a Prawn gem.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Prawn is a &lt;a href="https://blog.solutelabs.com/rails-6-updates-6a69c6b3d5c2" rel="noopener noreferrer"&gt;Ruby on Rails (RoR) library&lt;/a&gt; that generates PDFs for various functionalities.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ASedANZ5SENp1TUjIikaqPA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ASedANZ5SENp1TUjIikaqPA.png" alt="-------" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can store the encrypted file in the database with a password value - and Voila! you're done.&lt;/p&gt;

&lt;p&gt;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! :)&lt;/p&gt;

&lt;p&gt;I'm Manthan -a Software Developer at &lt;a href="https://www.solutelabs.com/" rel="noopener noreferrer"&gt;Solutelabs&lt;/a&gt;. An ordinary boy with extraordinary dreams. I am enthusiastic about writing clean, efficient and accessible code in a functional framework.&lt;/p&gt;

&lt;p&gt;Apart from work, I like to play cricket, watch movies and spend time with family.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>pdf</category>
      <category>security</category>
    </item>
  </channel>
</rss>
