用Prawn加水印(使用模板)
我需要能够为从模板创建的文档添加水印。我现在有以下代码:
# 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 中的原始数据之前运行,我可以加水印,去 数字。
所以我的问题是:
任何人都知道我如何使用虾来达到相同的效果 混合体?我宁愿在本地处理水印。
Prawn 中是否有与
GetOverContent()
基本等效的函数?有没有更好的方法将一串原始PDF数据获取到Prawn中 不使用
:template
和StringIO
? (我看到了#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:
Anyone know how I can achieve the same effect using prawn instead
of a hybrid? I'd rather handle the watermarking locally.Is there a basic equivalent to
GetOverContent()
in Prawn?Is there a better way to get a string of raw PDF data into Prawn
without using:template
andStringIO
? (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试在文档上盖章。
You could try stamping the document.