Password protect a pdf file in linux using pdftk


You have a pdf file with sensitive information and you want to protect it with a password.


Install the pdftk if you haven’t already in your system:

sudo apt-get install pdftk

And then run the following to prompt for a password before opening the file:

pdftk original.pdf output pswd_protected.pdf user_pw PROMPT

Pdf with .doc templates using prawn in Ruby on Rails

You want to generate pdf documents in your Ruby on Rails app using .doc templates.

Open the .doc document in OpenOffice and save it as an Open Document Format (.odt).

Open the saved .odt document and export to pdf, making sure you use ‘Losless compression’ in the General Options section.

Use imagemagick’s convert to make the pdf file to an image (setting the density to your requirements – default is 72dpi):

convert -density 150 one.pdf one.png

Copy the image one.png to your rails image directory (public/images).

Use the image as a background to the pdf created by prawnto (install plugin if you don’t have it) with:

pdf.image(templ, {:position => :center, :vposition => :top, :scale => 0.215})

Put the database field(s) text on position (you’ll have to play around with the coordinates to get it right), like:

pdf.font “#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf”, :size => 9
pdf.text “#{@model_name.fielda_name}”, :at => [100,530]
pdf.text “#{@model_name.fieldb_name}”, :at => [100,510] …