gpt4 book ai didi

ruby - 用 Prawn 加水印(使用模板)

转载 作者:太空宇宙 更新时间:2023-11-03 16:09:55 25 4
gpt4 key购买 nike

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

# 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 代码 = 非 Prawn 生成的页面上没有覆盖文本,但水印在预先加水印的模板上工作)。我唯一的猜测是有一些方法可以创建服务器正在执行但 Prawn 本身不能创建的 z-index/layer。

这是来自服务器的执行 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. 任何人都知道我如何使用 Prawn 来达到同样的效果混合动力车?我宁愿在本地处理水印。

  2. Prawn 中是否有基本等同于 GetOverContent() 的方法?

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


TL;DR:我需要将文本 float 到 Prawn 模板化文本上方给文档加水印。

我可以研究的任何见解或路径将不胜感激。如果这没有意义我可以澄清。

最佳答案

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

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]

关于ruby - 用 Prawn 加水印(使用模板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com