Rails 4 pdfkit 无法将 chartkick 图转换为 pdf [英] Rails 4 pdfkit failed converting chartkick graph to pdf
问题描述
我使用的是 Rails 4
I am using Rails 4
我遵循了 https://github.com/pdfkit/pdfkit
在控制器中我有
respond_to do |format|
format.html {render layout:'application'}
format.csv {send_data @testdatares.to_csv}
format.pdf {
html = render_to_string(:layout => 'application' , :action => "testpage.html.erb");
kit = PDFKit.new(html);
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.css";
send_data(kit.to_pdf, :filename => "some_name.pdf", :type => 'application/pdf')
}
end
在 application.html.erb 我有
In application.html.erb I have
<!DOCTYPE html>
<html>
<head>
<title>STNEW</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "http://www.google.com/jsapi", "/usr/local/lib/ruby/gems/2.1.0/gems/chartkick-1.3.2/app/assets/javascripts/chartkick",media:'all' %>
<%= csrf_meta_tags %>
</head>
请注意,我已经使用了 chartkick.js 的绝对路径
Please note that I already used absolute path to chartkick.js
现在在 html 中,chartkick 内容完美显示.
Now in html the chartkick contents were perfectly displayed.
但在 pdfkit 生成的 pdf 文件中,所有图表内容都显示为正在加载",而文本显示正确
But in the pdf file generated by pdfkit all chartkick contents were shown as "loading", while text was correctly shown
谷歌搜索但没有找到答案
Googled around but found no answer
推荐答案
终于,经过 3 天的搜索,路径和错误,我找到了解决方案
Finally, after 3 days of search, trails and error, I found solution for this
首先,我必须使用wicked_pdf而不是pdfkit,因为前者提供了两个重要的辅助函数
first of all, I have to use wicked_pdf rather than pdfkit because the former provides two important helper functions
wicked_pdf_stylesheet_link_tag
wicked_pdf_javascript_include_tag
那么我必须使用这两个助手来包含样式表和javascript,就像这样
then I must use these two helper to include stylesheet and javascript, like this
<%= wicked_pdf_stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= wicked_pdf_javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "http://www.google.com/jsapi"%>
<%= wicked_pdf_javascript_include_tag 'chartkick'%>
javascript_include_tag 和 wicked_pdf_javascript_include_tag 的区别在于后者会将所有原来的java代码写入到html文件中.这很重要,因为 wicked_pdf 的核心 wkhtmltopdf 不知道在哪里可以找到 js 和 css 资产.这些资产位于/usr/local/lib/ruby/gems/2.1.0/gems/之类的地方(您的可能不同),但只有您的 rails 应用程序知道它在哪里
The difference between javascript_include_tag and wicked_pdf_javascript_include_tag is that the latter will write all original java code into the html file. This is important because wkhtmltopdf, the core of wicked_pdf, does not know where to find the js and css assets. These assets are located at someplace like /usr/local/lib/ruby/gems/2.1.0/gems/ (yours might be different), but only your rails app know where it is
注意对于
<%= javascript_include_tag "http://www.google.com/jsapi"%>
你不能也不需要 wicked_pdf_javascript_include_tag.
you can't and don't need wicked_pdf_javascript_include_tag.
最重要的事情来了.wicked_pdf(以及 pdfkit)将指示您安装 gem wkhtmltopdf-binary ,它具有最新版本 0.9.9,然后只需调用这样的东西
Here the most most important thing come. wicked_pdf (and also pdfkit) will instruct you to install gem wkhtmltopdf-binary , which has the latest version 0.9.9 and then simply call something like this
format.pdf {render pdf :"test",layout:'application',template:'stinfos/testpage.html.erb'}
生成pdf.这仅适用于没有 chartkick 或 highchart 组件的 html.在生成的 PDF 文件中,chartkick 或 highchart 数字只会显示为正在加载"
to generate pdf. This ONLY works for html without chartkick or highchart component. In the PDF file generated, the chartkick or highchart figures will only show as "loading"
要解决这个问题,您需要通过运行删除 wkhtmltopdf-binary gem
To solve this, you need to remove the wkhtmltopdf-binary gem by running
gem uninstall wkhtmltopdf-binary
在您的 Rails 应用根目录中
in your rails app root
然后从 http://wkhtmltopdf.org/downloads.html
对我来说预编译版本有效http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_linux-trusty-amd64.deb
for me the precompiled version work http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_linux-trusty-amd64.deb
并安装
dpkg -i wkhtmltox-0.12.1_linux-trusty-amd64.deb
它很可能位于这里/usr/local/bin/wkhtmltopdf
most likely it will be located here /usr/local/bin/wkhtmltopdf
如果没有,请更改 config/initializers/wicked_pdf.rb
if not, change the config/initializers/wicked_pdf.rb
:exe_path => '/yourpath/wkhtmltopdf'
然后将 javascript_delay:2000 添加到 format.pdf
Then add javascript_delay:2000 to the format.pdf
format.pdf {render pdf:"test",javascript_delay:2000,
layout:'application',template:'stinfos/testpage.html.erb'}
然后您应该会看到图表显示在生成的 pdf 中.如果不是,请使用更大的延迟时间.
Then you should see the chartkick figures show up in the pdf generated. If not, use a larger delay time.
请注意,javascript_delay 选项不适用于 0.10 以下版本的 wkhtmlpdf
Note that javascript_delay option is not available for wkhtmlpdf below version 0.10
这篇关于Rails 4 pdfkit 无法将 chartkick 图转换为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!