Watermark in existing PDF in Ruby
Asked Answered
E

5

21

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

Ez answered 26/4, 2010 at 19:13 Comment(0)
L
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.

Lerner answered 19/12, 2012 at 18:55 Comment(2)
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.Styria
Prawn no longer supports editing/combining two PDFs into one. Check the answer by @Myst for an up-to-date solution.Kettie
X
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.

Xanthous answered 19/12, 2012 at 17:12 Comment(0)
M
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"
Muss answered 7/12, 2014 at 11:12 Comment(2)
Content file text not visibing, i've noticed the font color is geting white when creating template using mac pagesInexpugnable
@Inexpugnable , 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.Muss
S
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

Shelter answered 3/6, 2010 at 5:35 Comment(3)
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.Ez
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.Shelter
Actually, it seems that Prawn can edit an existing PDF if you use that PDF as a template: groups.google.com/group/prawn-ruby/browse_thread/thread/… & github.com/yob/prawn/tree/templates_2010 . I have not personally used these, but the information seems somewhat recent?Fiord
K
-5

Use Ruport.

1st result for Googling ruby pdf watermark.

Knox answered 25/5, 2010 at 18:8 Comment(2)
Duh... just shows the limits of Google. I'm looking for the same thing, and Ruport is not it.Encomiastic
Ruport uses PDF::Writer. PDF::Writer only creates PDFs. No go.Falgoust

© 2022 - 2024 — McMap. All rights reserved.