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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9185942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:46:39  来源:igfitidea点击:

How to edit or write on existing PDF with Ruby?

ruby-on-railsrubypdf-generationfpdi

提问by Alex Kovshovik

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,但它需要购买 Adob​​e Acrobat 才能为现有 PDF 文档添加可替换属性。

The PHP world is blessed with FPDIthat 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:

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

采纳答案by Said Kaldybaev

you have to definitely check out Prawngem, 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.

您一定要查看Prawngem,通过它您可以生成任何自定义 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.majesticseaacreature.com/manual.pdf

回答by Marc L

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

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

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

Rough Example:

粗略示例:

require 'prawn'
require 'pdf/toolkit'

template_filename = 'some/dir/Awesome-Graphics.pdf'
prawn_filename = 'temp.pdf'
output_filename = 'output.pdf'

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

PDF::Toolkit.pdftk(prawn_filename, "background", template_filename, "output", output_filename)

回答by Pawe? Go?cicki

I recommend prawnfor generating PDFs and then using combine_pdfto combine two generated PDFs together into one. I use it like this and it works just fine.

我推荐使用prawn生成 PDF,然后使用 combine_pdf将两个生成的 PDF 合并为一个。我像这样使用它,它工作得很好。

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

如何组合两个 PDF 的简短示例(来自自述文件):

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"

回答by Sami Birnbaum

You don't need to use a combination of gems you can use just one gem!

你不需要使用宝石的组合,你可以只使用一颗宝石!

Working with PDF's is really challenging in Ruby/Rails (so I have found out!)

在 Ruby/Rails 中使用 PDF 真的很有挑战性(所以我发现了!)

This is the way I was able to add text dynamically to a PDF in rails.

这是我能够在 rails 中将文本动态添加到 PDF 的方式。

add this gem to your gem file gem combine_pdf

将此 gem 添加到您的 gem 文件 gem combine_pdf

and then you can use code like this:

然后你可以使用这样的代码:

# get the record from the database to add dynamically to the pdf
user = User.last

# get the existing pdf
pdf = CombinePDF.load "#{Rails.root}/public/pdf/existing_pdf.pdf"

# create a textbox and add it to the existing pdf on page 2
pdf.pages[1].textbox "#{user.first_name} #{user.last_name}", height: 20, width: 70, y: 596, x: 72

# output the new pdf which now contains your dynamic data
pdf.save "#{Rails.root}/public/pdf/output#{Time.now.to_s}.pdf"

You can find details of the textbox method here: https://www.rubydoc.info/gems/combine_pdf/0.2.5/CombinePDF/Page_Methods#textbox-instance_method

您可以在此处找到文本框方法的详细信息:https: //www.rubydoc.info/gems/combine_pdf/0.2.5/CombinePDF/Page_Methods#textbox-instance_method

I spent days on this working through a number of different gems: prawnwicked_pdfpdfkitfillable_pdf

我花了几天的时间研究了许多不同的宝石: prawnwicked_pdfpdfkitfillable_pdf

But this was by far the most smooth solution for me as of 2019.

但这对我来说是 2019 年迄今为止最顺利的解决方案。

I hope this saves someone a lot of time so they don't have to go through all the trial and error I had to with PDF's!!

我希望这可以为某人节省大量时间,这样他们就不必经历我对 PDF 所做的所有试验和错误!!

回答by Joel Meador

PDFLibseems to do the thing you want and has ruby bindings.

PDFLib似乎可以做您想做的事情并且具有 ruby​​ 绑定。

回答by rudolph9

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 和其他一些)。

回答by SajithP

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
  • 该库可免费供个人使用,但如果用于生产则需要许可证
  • 这是一个非 ruby​​ 命令行工具

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/