Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用ruby向现有pdf添加文本_Ruby On Rails_Ruby_Pdf - Fatal编程技术网

Ruby on rails 使用ruby向现有pdf添加文本

Ruby on rails 使用ruby向现有pdf添加文本,ruby-on-rails,ruby,pdf,Ruby On Rails,Ruby,Pdf,我有一个rails应用程序,可以连接任意数量的pdf文件。现在我需要使用ruby为连接的pdf添加编号 有没有一种最先进的方法可以使用ruby将文本或其他内容添加到现有的pdf文件中?这个解决方案对我来说效果很好 Prawn::Document.generate("output.pdf", :template => "/path/to/template.pdf") do text "This is a text in a copied pdf.", :align => :cent

我有一个rails应用程序,可以连接任意数量的pdf文件。现在我需要使用ruby为连接的pdf添加编号


有没有一种最先进的方法可以使用ruby将文本或其他内容添加到现有的pdf文件中?

这个解决方案对我来说效果很好

Prawn::Document.generate("output.pdf", :template => "/path/to/template.pdf") do
  text "This is a text in a copied pdf.", :align => :center
end
你可以用它

我写这篇文章是因为Prawn放弃了他们的模板支持,我需要一个本地的替代品

您的代码可能如下所示:

pdf = CombinePDF.new
pdf << CombinePDF.new("file1.pdf")
pdf << CombinePDF.new("file2.pdf")
pdf.number_pages
pdf.save "output.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"
pdf=CombinePDF.new

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

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

将此gem添加到您的gem文件
gem“combine\u pdf”

然后您可以使用如下代码:

pdf = CombinePDF.new
pdf << CombinePDF.new("file1.pdf")
pdf << CombinePDF.new("file2.pdf")
pdf.number_pages
pdf.save "output.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"
您可以在此处找到textbox方法的详细信息:

我花了几天的时间研究了许多不同的宝石:
对虾
wicked\u pdf
pdfkit
filled\u pdf

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


我希望这能节省一些人很多时间,这样他们就不必经历我在PDF上的所有尝试和错误

明虾是一种可能性-谢谢,这对我来说很有用!请注意,自2013年12月以来,Prawn不支持模板: