21

I would like to add a dynamically generated text. Is there a way to watermark an existing PDF in Ruby?

oxfist
  • 749
  • 6
  • 22
Mike
  • 5,165
  • 6
  • 35
  • 50

5 Answers5

11

This will do it:

PDF::Reader to count the number of pages in the file.

Prawn to create a new PDF document using each page of the input pdf as a template.

require 'prawn'
require 'pdf-reader'

input_filename = 'input.pdf'
output_filename = 'output.pdf'

page_count = PDF::Reader.new(input_filename).page_count

Prawn::Document.generate(output_filename, :skip_page_creation => true) do |pdf|

  page_count.times do |num|
    pdf.start_new_page(:template => input_filename, :template_page => num+1)
    pdf.text('WATERMARK')
  end

end

However, in my testing the output file size was huge with the latest Gem version of Prawn (0.12), but after pointing my Gemfile at the master branch on github, all worked fine.

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • You can pass the `:template` option to `Prawn::Document` and then just iterate through the pages that way. That will keep the output file size much closer to the input file size. – siannopollo Aug 20 '13 at 22:45
  • Prawn no longer supports editing/combining two PDFs into one. Check the answer by @Myst for an up-to-date solution. – Paweł Gościcki Jun 01 '15 at 09:55
5

Another option is to use PDFTK. It can be used to add a watermark and create a new PDF. Maybe prawn will do the same thing with it's templating.

pdftk in.pdf background arecibo.pdf output wmark1.pdf

Some more info: http://rhodesmill.org/brandon/2009/pdf-watermark-margins/

There is a ruby wrapper gem called active_pdftk which supports backgrounding, so you don't have to do the shell commands yourself.

Amala
  • 1,718
  • 1
  • 17
  • 29
5

Prawn doesn't support templates anymore...

Try the combine_pdf gem.

You can combine, watermark, page-number and add simple text to existing PDF files (including the creation of a simple table of contents without PDF links).

It's a very simple and basic PDF library, written in pure Ruby with no dependencies.

This example can fit your situation (it's from the readme file):

# load the logo as a pdf page
company_logo = CombinePDF.load("company_logo.pdf").pages[0]

# load the content file
pdf = CombinePDF.load "content_file.pdf"

# inject the logo to each page in the content
pdf.pages.each {|page| page << company_logo}

# save to a new file, with the logo.
pdf.save "content_with_logo.pdf"
Myst
  • 18,516
  • 2
  • 45
  • 67
  • 1
    Content file text not visibing, i've noticed the font color is geting white when creating template using mac pages – Subha Sep 10 '18 at 07:45
  • @Subha , this might be an issue (or your PDF's background might be an opaque white rather than transparent), but this is definitely not the gem's normal behavior. – Myst Sep 10 '18 at 16:44
1

Check out Prawn(http://github.com/sandal/prawn) for just ruby and Prawnto(http://github.com/thorny-sun/prawnto) for Ruby on Rails.

You are probably going to want to either use the image embedding functionality or the background image functionality.

Here's an example of using a background image http://cracklabs.com/prawnto/code/prawn_demos/source/general/background

Nick Hammond
  • 11,228
  • 1
  • 19
  • 9
  • Prawn is only able to embed images to a fresh pdf created with prawn not an existing one ... so it doesn't work for me. – Mike Jun 04 '10 at 08:06
  • ah, missed the *existing* part in your post. Sorry about that. I don't believe there is, unless you were to convert it to an image and then watermark it. – Nick Hammond Jun 05 '10 at 22:53
  • Actually, it seems that Prawn can edit an existing PDF if you use that PDF as a template: http://groups.google.com/group/prawn-ruby/browse_thread/thread/a8dd9bb83d2c2abd?pli=1 & https://github.com/yob/prawn/tree/templates_2010 . I have not personally used these, but the information seems somewhat recent? – Riley Dutton Mar 07 '11 at 14:56
-5

Use Ruport.

1st result for Googling ruby pdf watermark.

supremebeing7
  • 1,073
  • 9
  • 26
mcandre
  • 22,868
  • 20
  • 88
  • 147