用Prawn加水印(使用模板)

发布于 2024-11-09 08:49:34 字数 1670 浏览 3 评论 0原文

我需要能够为从模板创建的文档添加水印。我现在有以下代码:

# 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

由于某种我无法理解的原因,这会忽略模板化页面。现在,情节变得更加复杂了:如果服务器添加了水印,那么这段代码将按预期工作(例如,直接的 Ruby 代码 = 在非虾生成的页面上没有覆盖文本,但水印可以在预先加水印的模板上工作)。我唯一的猜测是有某种方法可以创建服务器正在执行的 z-index/layer,但 Prawn 本身不能。

这是来自服务器的执行 PDF 的部分代码 生成本身,这使用 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();

如果在我使用 Prawn 中的原始数据之前运行,我可以加水印,去 数字。

所以我的问题是:

  1. 任何人都知道我如何使用虾来达到相同的效果 混合体?我宁愿在本地处理水印。

  2. Prawn 中是否有与 GetOverContent() 基本等效的函数?

  3. 有没有更好的方法将一串原始PDF数据获取到Prawn中 不使用 :templateStringIO? (我看到了#add_content方法 但这没有用)


TL;DR:我需要将文本浮动在 Prawn 模板化文本上方 为文档添加水印。

我可以研究的任何见解或路径将不胜感激。如果这个 我无法澄清,这毫无意义。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2024-11-16 08:49:34

您可以尝试在文档上盖章。

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] 

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] 
素染倾城色 2024-11-16 08:49:34
        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
        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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文