大虾: 静态头可以包含模型特定信息吗?

发布于 2024-11-09 16:45:42 字数 444 浏览 3 评论 0原文

我正在 Rails 和 Prawn 中构建一个 PDF 导出功能,我想在一个 PDF 中导出多个“公司”。一家公司通常有 2-3 个页面。现在,在导出单个公司时,可以使用 pdf.repeat(:all)。不过,我希望根据公司在标题中更改徽标。一个简单的代码示例是:

@companies.each do |c|
 pdf.repeat(:all) do
  pdf.image company.logo.url(:thumb), :at => [0,520]
 end
end

有办法做到这一点吗?我查看了相关主题,例如 Prawn PDF 中的页眉和页脚,但它对我没有帮助,因为我无法看到哪个公司在生成后属于哪个页面。

I'm building a PDF export function in Rails and Prawn where I want to export a number of "Companies" in one single PDF. A company usually flows over 2-3 pages. Right now when exporting a single Company this works with a pdf.repeat(:all). I want the logo to change in the header based on the Company though. A simple code example is:

@companies.each do |c|
 pdf.repeat(:all) do
  pdf.image company.logo.url(:thumb), :at => [0,520]
 end
end

Is there a way of doing this? I have looked at related topics, like header and footer in Prawn PDF but it won't help me since I can't see what Company belongs to which page after it's generated.

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

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

发布评论

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

评论(2

巷子口的你 2024-11-16 16:45:42

很棒的自文档手册(http://cloud.github.com/downloads/sandal/prawn/manual.pdf)包含此代码(第 105 页),这可能会有所帮助:

repeat(lambda { |pg| pg % 3 == 0 }) do
  draw_text "Every third page", :at => [250, 20]
end
repeat(:all, :dynamic => true) do
  draw_text page_number, :at => [500, 0]
end

lambda 或动态应该可以解决问题对于您来说,前提是您知道哪家公司从哪个页面开始。

如果您不知道每个公司的页数,请为每个公司制作一个 pdf 并合并它们。手册,第 109 页:

filename = "#{Prawn::BASEDIR}/data/pdfs/multipage_template.pdf"
Prawn::Example.generate("full_template.pdf", :template => filename) do
  go_to_page(page_count)
  start_new_page
  text "Previous pages and content imported.", :align => :center
  text "This page and content is brand new.", :align => :center
end

在最坏的情况下,您最终会一次合并两个。

您还可以检查带有 :filter 选项的 pdf.number_pages 是否适用于图像(如果您尚未尝试过)。我完全不知道它是否有效,而且我现在没有机会检查它。

The awesome self-documenting manual (http://cloud.github.com/downloads/sandal/prawn/manual.pdf) contains this code (page 105), which may be of help:

repeat(lambda { |pg| pg % 3 == 0 }) do
  draw_text "Every third page", :at => [250, 20]
end
repeat(:all, :dynamic => true) do
  draw_text page_number, :at => [500, 0]
end

Either lambda or the dynamic should do the trick for you, provided that you know what company starts at what page.

In case you do not know about the number of pages per company, make a pdf for each of them and merge them. Manual, page 109:

filename = "#{Prawn::BASEDIR}/data/pdfs/multipage_template.pdf"
Prawn::Example.generate("full_template.pdf", :template => filename) do
  go_to_page(page_count)
  start_new_page
  text "Previous pages and content imported.", :align => :center
  text "This page and content is brand new.", :align => :center
end

In the worst case you will end up with merging two at a time.

You might also check if pdf.number_pages with :filter option works with images (if you have not tried it yet). I have absolutely no idea if it works and I have no chance to check it right now.

离不开的别离 2024-11-16 16:45:42

不确定这是否有帮助,但 WickedPDF 是虾的良好替代品。

Not sure if this'll help but WickedPDF is a good alternative to Prawn.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文