Rails 结合操作流中的 pdf

问题描述 投票:0回答:2

我的控制器中有一个操作

#applications
,它呈现我的网站应用程序页面的 html 页面。当我使用 :pdf 格式指定此操作的路径时,它会将页面呈现为 pdf(可以正常工作)。

我正在编写另一个操作

#applications_print_version
,它需要包含来自
#applications
的渲染视图以及一些其他 pdf(来自 url 列表)。使用combine_pdf,我已经能够通过工作的url获取pdf列表,但我无法将
#applications
视图添加到
#applications_print_version
工作中的组合pdf中。

这是我到目前为止所拥有的。

def applications_print_version
    html = render_to_string(action: :applications) 
    pdf = WickedPdf.new.pdf_from_string(html) 

    new_pdf = CombinePDF.new
    new_pdf << CombinePDF.parse(pdf)

    #List of pdfs I got from somewhere else
    @pdf_attachments.each { |att| new_pdf << CombinePDF.parse( Net::HTTP.get( URI.parse( att.url ) ) ) }

    send_data new_pdf.to_pdf, :disposition => 'inline', :type => "application/pdf"
end

此解决方案确实拥有所有数据,但

pdf
变量没有样式。我似乎无法让它发挥作用。

ruby-on-rails-4 pdf-generation
2个回答
0
投票

感谢当地社区的支持,我成功使其发挥作用。有一些事情我需要解决。

我用来渲染

#applications
的布局使用的是我用于 PDFKit 的标准 pdf 布局。我复制了布局并替换了

<%= stylesheet_link_tag 'application' %>

<%= wicked_pdf_stylesheet_link_tag 'application' %>

我所做的一个可以使用 WickedPDF 所需的布局渲染

#applications
操作。

html = render_to_string(action: :applications, layout: 'wicked_pdf') 

我遇到了另一个问题。 WickedPDF 存在一个已知问题 https://github.com/mileszs/wicked_pdf/issues/470

所以我必须删除样式表中的任何

@import "bootstrap"
实例,这并不理想,但我无法解决上述问题。

现在

#applications_print_version
可以正常工作了!

如果有人可以做得更好,请告诉我,我很想知道:)


0
投票

您遇到的问题是 pdf_from_string 方法期望传递 String 类,但 render_to_string 方法不返回字符串类。

尝试使用 File.read 或其他方法获取 HTML 内容。

© www.soinside.com 2019 - 2024. All rights reserved.