WickedPDF在尝试呈现多个pdf时生成空白pdf



我在Rails 6项目中使用WickedPDF从HTML生成PDF,然后将它们与预填充的PDF表单结合起来。wicwickdpdf生成的pdf被分成两个部分,每个部分都必须与一个表单相结合。表单必须出现在各自部分的开头。

我尝试使用WickedPDF生成两个pdf,然后使用combine_pdf以适当的顺序将它们与预填充的表单组合在一起。

render_ar_docsrender_gl_docs当我单独访问它们的路由时,它们都像预期的那样工作:它们生成并保存预期的PDF。但是,当我在print_complete_docs操作中依次调用它们时,得到的pdf是一个空白页。

如何从一个操作生成多个pdf ?

谢谢你的帮助。

def print_complete_docs
@policy.fill_and_save_acord28
@policy.fill_and_save_acord25
render_ar_docs
render_gl_docs
pdf = CombinePDF.new
pdf << CombinePDF.load("tmp/acord28.pdf")
pdf << CombinePDF.load("tmp/ar_docs.pdf")
pdf << CombinePDF.load("tmp/acord25.pdf")
pdf << CombinePDF.load("tmp/gl_docs.pdf")
pdf.save "tmp/complete_docs.pdf"
send_file("#{Rails.root}/tmp/complete_docs.pdf", filename: "tmp/#{@policy.legal_vesting} Complete Docs.pdf", type: 'application/pdf')
end
def render_ar_docs
render pdf: 'ar_docs', 
layout: 'document',
save_to_file: Rails.root.join('tmp', "ar_docs.pdf"),
save_only: true
end

def render_gl_docs
render pdf: 'gl_docs', 
layout: 'document',
save_to_file: Rails.root.join('tmp', "gl_docs.pdf"),
save_only: true
end

我的问题似乎与试图在单个请求中渲染多次有关,这是Rails不允许的。相反,我应该使用WickedPDF的pdf_from_string方法。

我的新方法(过于冗长和未重构,但功能良好)是这样的:

def print_complete_docs
@policy.fill_and_save_acord28
@policy.fill_and_save_acord25
render_ar_docs
render_gl_docs

pdf = CombinePDF.new
pdf << CombinePDF.load("tmp/acord28.pdf")
pdf << CombinePDF.load("tmp/ar_docs.pdf")
pdf << CombinePDF.load("tmp/acord25.pdf")
pdf << CombinePDF.load("tmp/gl_docs.pdf")
pdf.save "tmp/complete_docs.pdf"
send_file("#{Rails.root}/tmp/complete_docs.pdf", filename: "tmp/#{@policy.legal_vesting} Complete Docs.pdf", type: 'application/pdf')
end

def render_ar_docs
ar_docs = WickedPdf.new.pdf_from_string(
render_to_string(
'policies/ar_docs.html.erb',
layout:'document.html.erb',
locals: { policy: @policy }
),
layout: 'document.html.erb'
)
save_path = Rails.root.join('tmp','ar_docs.pdf')
File.open(save_path, 'wb') do |file|
file << ar_docs
end
end
def render_gl_docs
gl_docs = WickedPdf.new.pdf_from_string(
render_to_string(
'policies/gl_docs.html.erb',
layout:'document.html.erb',
locals: { policy: @policy }
),
layout: 'document.html.erb'
)
save_path = Rails.root.join('tmp','gl_docs.pdf')
File.open(save_path, 'wb') do |file|
file << gl_docs
end
end

PS:感谢@Unixmonkey的帮助,感谢他对WickedPDF的贡献!