我为此挣扎了一会儿,其他答案对我来说只是部分有用。我想为将来的任何人提供更多细节:
我为我们解决这个问题所做的 4 件主要事情是:
(1) 不使用中间件方法,而是使用基于您创建的 PDF 布局的一种 ruby 嵌入式 pdf
#Example layout file
#app/views/layout/pdf.pdf.rb
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%= wicked_pdf_stylesheet_link_tag "print" %> #print specific stylesheet
<%= yield :head %>
</head>
<body>
<h1>PDF Report</h1>
<%= yield %>
</body>
</html>
(2) 使用 wicked_pdf 的资产助手仅在那些页面上为我们存储在应用程序中的资产加载我们需要的 javascript(所有基于 CDN 的资产都可以使用普通的 javascript_include_tag 加载)
#Example page template for a PDF you're downloading
#app/views/users/profile.pdf.erb
<% content_for :head do %>
<%= javascript_include_tag "some_cdn.com" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
<% end %>
<p>All your pages content</p>
(3) 使用 javascript_delay 选项
#Use it universally from the initializer or on the specific PDF rendering in the controller
#config/initializers/wicked_pdf.rb
WickedPdf.config = {
javascript_delay: 3000,
other_options...
}
(4) 通过“离散”轴选项,否则我们只看到轴,没有时间图表的数据(线/区域/等)
#In the above template, wherever you render your chart
#app/views/users/profile.pdf.erb
<% content_for :head do %>
<%= javascript_include_tag "some_cdn.com" %>
<%= wicked_pdf_javascript_include_tag "chartkick" %>
<% end %>
<%= area_chart @data_retriever.time_based_data, discrete: true %>
<%= pie_chart @data_retriever.other_data %> # the default is discrete: false so no need for another option