2

I need to be able to watermark a document that was created from a template. I have the following code right now:

# Note: the raw PDF text (body variable below) is sent from a remote server.
Prawn::Document.new(:template => StringIO.new(body), :page_size =>
'A4') do |document|
  # ... including other pages and sections to the template here ...

  # watermark
  d.page_count.times do |i|
    d.go_to_page i
    d.stroke_line [d.bounds.left, d.bounds.bottom], [d.bounds.right, d.bounds.top]
    d.draw_text "Watermark", :rotate => 45, :at => [100,100], :size => 100
  end
end

This is ignoring the templated pages for some reason that I can't comprehend. Now here's where the plot thickens: if the server adds a watermark, then this code will work as expected (e.g. straight Ruby code = no overlaying text on the non-prawn-generated pages, yet watermarking works on a pre-watermarked template). My only guess is there is some way to create a z-index/layer that the server is doing but Prawn itself cannot.

Here is a portion of the code from the server that does the PDF generation itself, this does the watermarking using iText:

PdfStamper stamper = new PdfStamper(...);
PdfContentByte over = stamper.GetOverContent(i + 1);
over.BeginText();
over.SetTextMatrix(20, 40);
over.SetFontAndSize(bf, 20);
over.SetColorFill(new Color(97, 150, 58));
over.ShowTextAligned(Element.ALIGN_CENTER,
                     watermarkText,
                     document.PageSize.Width / 2,
                     document.PageSize.Height / 2,
                     55);
over.EndText();
over.Stroke();

If that runs before I use the raw data in Prawn I can watermark, go figure.

So my questions are:

  1. Anyone know how I can achieve the same effect using prawn instead of a hybrid? I'd rather handle the watermarking locally.

  2. Is there a basic equivalent to GetOverContent() in Prawn?

  3. Is there a better way to get a string of raw PDF data into Prawn without using :template and StringIO? (I saw the #add_content method but that didn't work)


TL;DR: I need to float text above the Prawn templated text a la watermarking the document.

Any insight or paths I can research will be appreciated. If this makes no sense I can clarify.

oxfist
  • 749
  • 6
  • 22
RobH
  • 568
  • 5
  • 13
  • **note: i know i have "d" instead of "document" -- in the actual code this is in a helper method, so ignore that :) – RobH May 25 '11 at 13:55
  • How are you applying the template? Is this an image? – user596916 May 25 '11 at 17:43
  • Nope. it is a raw string of PDF data -- which is why I had to use StringIO to avoid writing it to a file. – RobH May 25 '11 at 17:46
  • Prawn has removed templating. See answer to a similar question: https://stackoverflow.com/a/25044631/5307177 – drweird Feb 12 '21 at 19:09

2 Answers2

1

You could try stamping the document.

create_stamp("watermark") do
 rotate(30, :origin => [-5, -5]) do
   stroke_color "FF3333"
   stroke_ellipse [0, 0], 29, 15
   stroke_color "000000"
   fill_color "993333"
   font("Times-Roman") do
     draw_text "Watermark", :at => [-23, -3]
   end
   fill_color "000000"
 end
end

stamp_at "watermark", [210, 210] 
Aaron Henderson
  • 1,840
  • 21
  • 20
1
        create_stamp("stamp") do
            fill_color "cc0000"
            text_rendering_mode(:fill_stroke) do
                transparent(0.5){
                text_box "WATERMARK",
                :size   => 50,
                :width  => bounds.width,
                :height => bounds.height,
                :align  => :center,
                :valign => :center,
                :at     => [0, bounds.height],
                :rotate => 45,
                :rotate_around => :center
            }
            end
          end

        repeat (:all) do
            stamp("stamp")
        end