42

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

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

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

问题是:在现有 PDF 上编辑、书写或绘图的更好的 Ruby 方式是什么?

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

4

8 回答 8

35

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

例如:

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

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

在这里查看更多信息:http: //prawn.majesticseaacreature.com/manual.pdf

于 2012-02-09T05:47:37.053 回答
24

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

  1. 使用Prawn生成仅包含您要添加的动态部分的 PDF。
  2. 使用PDF::Toolkit(包装PDFtk)将 Prawn PDF 与原始文件结合起来。

粗略的例子:

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)
于 2015-05-18T23:39:26.807 回答
23

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

如何组合两个 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"
于 2015-06-01T09:58:01.353 回答
11

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

在 Ruby/Rails 中使用 PDF 确实具有挑战性(所以我发现了!)

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

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

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

# 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"

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

我花了几天时间研究了许多不同的宝石:prawn wicked_pdf pdfkit fillable_pdf

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

我希望这可以为某人节省很多时间,这样他们就不必经历我对 PDF 的所有反复试验!

于 2019-07-29T16:38:41.090 回答
1

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

于 2012-02-09T05:32:16.143 回答
1

PDFLib似乎可以做您想做的事情并且具有红宝石绑定。

于 2012-02-09T07:02:52.247 回答
0

根据我的研究,Prawn 是我发现的免费且最好的宝石之一。模板功能在更高版本中不起作用。我能找到与模板一起使用的最新版本是 1.0.0.rc2 - 2013 年 3 月 1 日。找不到任何与模板一起使用的更高版本。因此,如果您使用的是比这更高的版本,请注意。检查下面的线程以获取更多信息。

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

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

  • 此库可供个人免费使用,但如果在生产中使用则需要许可证
  • 这是一个非 ruby​​ 命令行工具

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

于 2015-10-12T05:17:19.940 回答
0

您可以使用Origami gem 为现有的 pdf 添加密码或对其进行编辑。

pdf = WickedPdf.new.pdf_from_url(pdf_params[:url])
temp_file = Tempfile.new('temp', encoding: 'ascii-8bit')
temp_file.write(pdf)

# Creates an encrypted document with AES256 and passwords.
pdf = PDF.read(temp_file.path).encrypt(cipher: 'aes', key_size: 256,user_passwd: pdf_params[:user_password], owner_passwd: pdf_params[:owner_password])
save_path = "#{File.basename(__FILE__, ".rb")}.pdf"

pdf.save(save_path)
temp_file.close
于 2021-03-23T07:38:11.603 回答