如何使用Ruby编辑或编写现有PDF?

时间:2021-08-04 21:12:39

I have a couple of PDF template files with complex content and several blank regions/areas in them. I need to be able to write text in those blank regions and save the resulting PDFs in a folder.

我有几个包含复杂内容的PDF模板文件以及其中的几个空白区域/区域。我需要能够在这些空白区域中写入文本并将生成的PDF保存在文件夹中。

I googled for answers on this question quite intensively, but I didn't find definite answers. One of the better solutions is PDF::Toolkit, but it would require the purchase of Adobe Acrobat to add replaceable attributes to existing PDF documents.

我搜索了这个问题的答案非常密集,但我没有找到明确的答案。其中一个更好的解决方案是PDF :: Toolkit,但它需要购买Adobe Acrobat才能为现有PDF文档添加可替换属性。

The PHP world is blessed with FPDI that can be used to simply open a PDF file and write/draw on it over the existing content. There is a Ruby port of this library, but the last commit for it happened at the beginning of 2009. Also that project doesn't look like it is widely used and supported.

PHP世界拥有FPDI,可用于简单地打开PDF文件并在现有内容上书写/绘制。这个库有一个Ruby端口,但它的最后一次提交发生在2009年初。此项目看起来并不像它被广泛使用和支持。

The question is: What is the better Ruby way of editing, writing or drawing on existing PDFs?

问题是:在现有PDF上编辑,编写或绘图的Ruby方式有哪些?

This question also doesn't seem to be answered on here. These questions are related, but not really the same:

这个问题似乎也没有在这里回答。这些问题是相关的,但并不完全相同:

6 个解决方案

#1


27  

you have to definitely check out Prawn gem, by which you can generate any custom pdf files. You can actually use prawn to write in text into existing pdfs by treating the existing PDF as a template for your new Prawn document.

你必须要看看Prawn gem,你可以通过它生成任何自定义的pdf文件。通过将现有PDF作为新Prawn文档的模板处理,您实际上可以使用prawn将文本写入现有pdf。

For example:

例如:

filename = "#{Prawn::DATADIR}/pdfs/multipage_template.pdf"
Prawn::Document.generate("full_template.pdf", :template => filename) do
  text "THis content is written on the first page of the template", :align => :center
end

This will write text onto the first page of the old pdf.

这会将文本写入旧pdf的第一页。

See more here: http://prawn.majesticseacreature.com/manual.pdf

在此处查看更多信息:http://prawn.majesticseacreature.com/manual.pdf

#2


15  

Since Prawn has removed the template feature (it was full of bugs) the easiest way I've found is the following:

由于Prawn删除了模板功能(它充满了bug),我发现的最简单方法如下:

  1. Use Prawn to generate a PDF with ONLY the dynamic parts you want to add.
  2. 使用Prawn生成只包含要添加的动态部分的PDF。
  3. Use PDF::Toolkit (which wraps PDFtk) to combine the Prawn PDF with the original.
  4. 使用PDF :: Toolkit(包装PDFtk)将Prawn PDF与原始PDF结合起来。

Rough Example:

粗略的例子:

require 'prawn'
require 'pdf/toolkit'

template_file = 'some/dir/Awesome-Graphics.pdf'
prawn_file = 'temp.pdf'
output_file = 'output.pdf'

Prawn::Document.generate(prawn_file) do
  # Generate whatever you want here.
  text_box "This is some new text!", :at => [100, 300]
end

PDF::Toolkit.pdftk(prawn_file, "background", template_file, "output", output_file)

#3


12  

I recommend prawn for generating PDFs and then using combine_pdf to combine two generated PDFs together into one. I use it like this and it works just fine.

我建议大虾用于生成PDF,然后使用combine_pdf将两个生成的PDF组合成一个。我像这样使用它,它工作得很好。

Short example (from the README) of how to combine two PDFs:

关于如何组合两个PDF的简短示例(来自README):

company_logo = CombinePDF.load("company_logo.pdf").pages[0]
pdf = CombinePDF.load "content_file.pdf"
pdf.pages.each { |page| page << company_logo } # notice the << operator is on a page and not a PDF object.
pdf.save "content_with_logo.pdf"

#4


1  

The best I can think of is Rails-latex, it doesn't allow you to edit existing PDF files but it would allow you to set up template *.tex.erb which you may dynamically modify and compile them into PDF format (along with dvi and a number of others).

我能想到的最好的是Rails-latex,它不允许你编辑现有的PDF文件,但它允许你设置模板* .tex.erb,你可以动态修改它们并将它们编译成PDF格式(以及dvi和其他一些人)。

#5


1  

PDFLib seems to do the thing you want and has ruby bindings.

PDFLib似乎做你想要的东西,并有ruby绑定。

#6


0  

According to my research, Prawn is one of the free and best gems I found. The template functionality isn't working in later version. The latest version I could find to work with templates is 1.0.0.rc2 - March 1, 2013. Couldn't find any later version which works with templates. So be mindful if you are using later versions than this. Check below thread for more info.

根据我的研究,Prawn是我发现的免费和最好的宝石之一。模板功能在以后的版本中不起作用。我可以找到使用模板的最新版本是1.0.0.rc2 - 2013年3月1日。找不到任何适用于模板的更新版本。如果您使用的是更高版本,请注意。查看以下主题以获取更多信息。

https://groups.google.com/forum/#!searchin/prawn-ruby/prawn$20templates/prawn-ruby/RYGPImNcR0I/7mxtnrEDHeQJ

https://groups.google.com/forum/#!searchin/prawn-ruby/prawn$20templates/prawn-ruby/RYGPImNcR0I/7mxtnrEDHeQJ

PDFtk is another capable tool for PDF manipulation and to work with templates. But it mentions following points,

PDFtk是另一种用于PDF操作和使用模板的功能强大的工具。但它提到了以下几点,

  • This library is free for personal use, but requires a license if used in production
  • 该库可供个人免费使用,但如果在生产中使用,则需要许可证
  • This is a non-ruby command line tool
  • 这是一个非ru​​by命令行工具

For more information please refer the below link http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/

欲了解更多信息,请参阅以下链接http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/

#1


27  

you have to definitely check out Prawn gem, by which you can generate any custom pdf files. You can actually use prawn to write in text into existing pdfs by treating the existing PDF as a template for your new Prawn document.

你必须要看看Prawn gem,你可以通过它生成任何自定义的pdf文件。通过将现有PDF作为新Prawn文档的模板处理,您实际上可以使用prawn将文本写入现有pdf。

For example:

例如:

filename = "#{Prawn::DATADIR}/pdfs/multipage_template.pdf"
Prawn::Document.generate("full_template.pdf", :template => filename) do
  text "THis content is written on the first page of the template", :align => :center
end

This will write text onto the first page of the old pdf.

这会将文本写入旧pdf的第一页。

See more here: http://prawn.majesticseacreature.com/manual.pdf

在此处查看更多信息:http://prawn.majesticseacreature.com/manual.pdf

#2


15  

Since Prawn has removed the template feature (it was full of bugs) the easiest way I've found is the following:

由于Prawn删除了模板功能(它充满了bug),我发现的最简单方法如下:

  1. Use Prawn to generate a PDF with ONLY the dynamic parts you want to add.
  2. 使用Prawn生成只包含要添加的动态部分的PDF。
  3. Use PDF::Toolkit (which wraps PDFtk) to combine the Prawn PDF with the original.
  4. 使用PDF :: Toolkit(包装PDFtk)将Prawn PDF与原始PDF结合起来。

Rough Example:

粗略的例子:

require 'prawn'
require 'pdf/toolkit'

template_file = 'some/dir/Awesome-Graphics.pdf'
prawn_file = 'temp.pdf'
output_file = 'output.pdf'

Prawn::Document.generate(prawn_file) do
  # Generate whatever you want here.
  text_box "This is some new text!", :at => [100, 300]
end

PDF::Toolkit.pdftk(prawn_file, "background", template_file, "output", output_file)

#3


12  

I recommend prawn for generating PDFs and then using combine_pdf to combine two generated PDFs together into one. I use it like this and it works just fine.

我建议大虾用于生成PDF,然后使用combine_pdf将两个生成的PDF组合成一个。我像这样使用它,它工作得很好。

Short example (from the README) of how to combine two PDFs:

关于如何组合两个PDF的简短示例(来自README):

company_logo = CombinePDF.load("company_logo.pdf").pages[0]
pdf = CombinePDF.load "content_file.pdf"
pdf.pages.each { |page| page << company_logo } # notice the << operator is on a page and not a PDF object.
pdf.save "content_with_logo.pdf"

#4


1  

The best I can think of is Rails-latex, it doesn't allow you to edit existing PDF files but it would allow you to set up template *.tex.erb which you may dynamically modify and compile them into PDF format (along with dvi and a number of others).

我能想到的最好的是Rails-latex,它不允许你编辑现有的PDF文件,但它允许你设置模板* .tex.erb,你可以动态修改它们并将它们编译成PDF格式(以及dvi和其他一些人)。

#5


1  

PDFLib seems to do the thing you want and has ruby bindings.

PDFLib似乎做你想要的东西,并有ruby绑定。

#6


0  

According to my research, Prawn is one of the free and best gems I found. The template functionality isn't working in later version. The latest version I could find to work with templates is 1.0.0.rc2 - March 1, 2013. Couldn't find any later version which works with templates. So be mindful if you are using later versions than this. Check below thread for more info.

根据我的研究,Prawn是我发现的免费和最好的宝石之一。模板功能在以后的版本中不起作用。我可以找到使用模板的最新版本是1.0.0.rc2 - 2013年3月1日。找不到任何适用于模板的更新版本。如果您使用的是更高版本,请注意。查看以下主题以获取更多信息。

https://groups.google.com/forum/#!searchin/prawn-ruby/prawn$20templates/prawn-ruby/RYGPImNcR0I/7mxtnrEDHeQJ

https://groups.google.com/forum/#!searchin/prawn-ruby/prawn$20templates/prawn-ruby/RYGPImNcR0I/7mxtnrEDHeQJ

PDFtk is another capable tool for PDF manipulation and to work with templates. But it mentions following points,

PDFtk是另一种用于PDF操作和使用模板的功能强大的工具。但它提到了以下几点,

  • This library is free for personal use, but requires a license if used in production
  • 该库可供个人免费使用,但如果在生产中使用,则需要许可证
  • This is a non-ruby command line tool
  • 这是一个非ru​​by命令行工具

For more information please refer the below link http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/

欲了解更多信息,请参阅以下链接http://adamalbrecht.com/2014/01/31/pre-filling-pdf-form-templates-in-ruby-on-rails-with-pdftk/