4
votes

I tried the example in watermark existing pdf with ruby, but when I printed generated document, the "WATERMARK" was printed upside down, almost as if it got to the end of the paper, folded over and then printed on the back (but showed through the front). In fact, that is what happened because I added a few more lines of text with "pdf.text".

I also tried playing with the page size of the new document, with :page_size=>"LEGAL", but that didn't change anything. I also tried "LETTER", and "EXECUTIVE", but those didn't work either.

Is there a way to get it to print it without it folding over?

1

1 Answers

0
votes

It's very possible that the PDF you are trying to watermark has been rotated.

You can use the CombinePDF gem to both fix the rotation and add watermarks to your existing PDF like so:

require 'combine_pdf'
watermark = CombinePDF.load("watermark.pdf").pages[0]
pdf = CombinePDF.load "content_file.pdf"
pdf.pages.each {|page| page.fix_rotation << watermark}
pdf.save "content_with_watermark.pdf"

or, you can watermark using your own text:

require 'combine_pdf'
pdf = CombinePDF.load "content_file.pdf"
pdf.pages.each {|page| page.fix_rotation.textbox 'watermark', opacity: 0.3}
pdf.save "content_with_watermark.pdf"

Notice that #fix_rotation returns self - that is, the page object - which allows us to chain methods as I have done in this demo code.

As for PDF::Reader ... I have no idea how to move the 'Rotate: 180' into the actual page's content stream. But if you insist on using the pdf_reader gem, you might be able to find some data in it's documentation.

Good Luck!

P.S.

As far as I know, Prawn dropped it's template support... so the example you referenced shouldn't work with newer versions of Prawn.