将外部PDF插入到Prawn生成的文档中

时间:2022-07-22 21:16:27

How can I insert an existing PDF into a Prawn generated document? I am generating a pdf for a bill (as a view), and that bill can have many attachments (png, jpg, or pdf). How can I insert/embed/include those external pdf attachments in my generated document? I've read the manual, looked over the source code, and searched online, but no luck so far.

如何将现有PDF插入到Prawn生成的文档中?我正在为账单生成pdf(作为视图),该账单可以包含许多附件(png,jpg或pdf)。如何在生成的文档中插入/嵌入/包含这些外部pdf附件?我已经阅读了手册,查看了源代码,并在线搜索,但到目前为止还没有运气。

The closest hint I've found is to use ImageMagick or something similar to convert the pdf to another format, but since I don't need to resize/manipulate the document, that seems wasteful. The old way to do it seems to be through templates, but my understanding is that the code for templating is unstable.

我发现最接近的提示是使用ImageMagick或类似的东西将pdf转换为另一种格式,但由于我不需要调整/操作文档,这似乎很浪费。旧方法似乎是通过模板,但我的理解是模板的代码是不稳定的。

Does anyone know how to include PDF pages in a Prawn generated PDF? If Prawn won't do this, do you know of any supplementary gems that will? If someone can point me towards something like prawn-templates but more reliable, that would be awesome.

有谁知道如何在Prawn生成的PDF中包含PDF页面?如果Prawn不会这样做,你知道任何补充宝石吗?如果有人能指出我喜欢虾模板但更可靠的东西,那就太棒了。

Edit: I am using prawnto and prawn to render PDF views in Rails 4.2.0 with Ruby 2.2.0.

编辑:我正在使用prawnto和prawn在Rails 4.2.0中使用Ruby 2.2.0呈现PDF视图。


Strategies that I've found but that seem inapplicable/too messy:

我发现但似乎不适用/太乱的策略:

  • Create a jpg preview of a PDF on upload, include that in the generated document (downsides: no text selection/searching, expensive). This is currently my favorite option, but I don't like it.

    在上传时创建PDF的jpg预览,包括生成的文档中的内容(缺点:没有文本选择/搜索,昂贵)。这是目前我最喜欢的选择,但我不喜欢它。

  • prawn-templates (downside: unstable, unmaintained codebase; this is a business-critical application)

    prawn-templates(缺点:不稳定,未维护的代码库;这是一个关键业务应用程序)

  • Merge PDFs through a gem like 'combine-pdf'–I can't figure out how to make this work for rendering a view with the external PDFs inserted at specific places (the generated pdf is a collection of bills, and I need them to follow the bill they're attached to)

    通过像'combine-pdf'这样的宝石合并PDF文件 - 我无法弄清楚如何通过在特定位置插入外部PDF来渲染视图(生成的pdf是一个帐单集合,我需要它们按照他们附加的法案)

1 个解决方案

#1


10  

You're right about the lack of existing documentation for this - I found only this issue from 2010 which uses the outdated methods you describe. I also found this SO answer which does not work now since Prawn dropped support for templates.

你对此缺乏现有文档是正确的 - 我发现2010年只有这个问题使用了你描述的过时方法。我也发现这个SO答案现在不起作用,因为Prawn放弃了对模板的支持。

However, the good news is that there is a way to do what you want with Ruby! What you will be doing is merging the PDFs together, not "inserting" PDFs into the original PDF.

然而,好消息是有一种方法可以用Ruby做你想做的事!您将要做的是将PDF合并在一起,而不是将PDF插入到原始PDF中。

I would recommend this library, combine_pdf, to do so. The documentation is good, so doing what you want would be as simple as:

我建议这个库,combine_pdf,这样做。文档很好,所以做你想做的事情就像这样简单:

my_prawn_pdf = CombinePDF.new
my_prawn_pdf << CombinePDF.new("my_bill_pdf.pdf")
my_prawn_pdf << CombinePDF.new("attachment.pdf")
my_prawn_pdf.save "combined.pdf"

Edit

In response to your questions:

回答你的问题:

I'm using Prawn to render a pdf view in Rails, which means that I don't think I get that kind of post-processing

我正在使用Prawn在Rails中渲染pdf视图,这意味着我不认为我会得到那种后期处理

You do! If you look at the documentation for combine_pdf, you'll see that loading from memory is the fastest way to use the gem - the documentation even explicitly says that Prawn can be used as input.

你做!如果你查看combine_pdf的文档,你会发现从内存加载是使用gem的最快方法 - 文档甚至明确地说Prawn可以用作输入。

I'm not just tacking the PDFs to the end: a bill attachment must directly follow the generated page(s) for a bill

我不只是将PDF格式化到最后:账单附件必须直接跟在生成的账单上

The combine_pdf gem isn't just for adding pages on the end. As the documentation shows, you can cycle through a PDF adding pages when you want to, for example:

combine_pdf gem不只是用于在末尾添加页面。如文档所示,您可以根据需要循环浏览PDF添加页面,例如:

my_pdf # previously defined
new_pdf = CombinePDF.new
my_pdf.pages.each.do |page| 
  i += 1
  new_pdf << my_pdf if i == bill_number # or however you want to handle this logic
end
new_pdf.save "new_pdf.pdf"

#1


10  

You're right about the lack of existing documentation for this - I found only this issue from 2010 which uses the outdated methods you describe. I also found this SO answer which does not work now since Prawn dropped support for templates.

你对此缺乏现有文档是正确的 - 我发现2010年只有这个问题使用了你描述的过时方法。我也发现这个SO答案现在不起作用,因为Prawn放弃了对模板的支持。

However, the good news is that there is a way to do what you want with Ruby! What you will be doing is merging the PDFs together, not "inserting" PDFs into the original PDF.

然而,好消息是有一种方法可以用Ruby做你想做的事!您将要做的是将PDF合并在一起,而不是将PDF插入到原始PDF中。

I would recommend this library, combine_pdf, to do so. The documentation is good, so doing what you want would be as simple as:

我建议这个库,combine_pdf,这样做。文档很好,所以做你想做的事情就像这样简单:

my_prawn_pdf = CombinePDF.new
my_prawn_pdf << CombinePDF.new("my_bill_pdf.pdf")
my_prawn_pdf << CombinePDF.new("attachment.pdf")
my_prawn_pdf.save "combined.pdf"

Edit

In response to your questions:

回答你的问题:

I'm using Prawn to render a pdf view in Rails, which means that I don't think I get that kind of post-processing

我正在使用Prawn在Rails中渲染pdf视图,这意味着我不认为我会得到那种后期处理

You do! If you look at the documentation for combine_pdf, you'll see that loading from memory is the fastest way to use the gem - the documentation even explicitly says that Prawn can be used as input.

你做!如果你查看combine_pdf的文档,你会发现从内存加载是使用gem的最快方法 - 文档甚至明确地说Prawn可以用作输入。

I'm not just tacking the PDFs to the end: a bill attachment must directly follow the generated page(s) for a bill

我不只是将PDF格式化到最后:账单附件必须直接跟在生成的账单上

The combine_pdf gem isn't just for adding pages on the end. As the documentation shows, you can cycle through a PDF adding pages when you want to, for example:

combine_pdf gem不只是用于在末尾添加页面。如文档所示,您可以根据需要循环浏览PDF添加页面,例如:

my_pdf # previously defined
new_pdf = CombinePDF.new
my_pdf.pages.each.do |page| 
  i += 1
  new_pdf << my_pdf if i == bill_number # or however you want to handle this logic
end
new_pdf.save "new_pdf.pdf"