Rails 4 pdfkit failed to convert chart diagram to pdf

I am using Rails 4

I followed the instructions https://github.com/pdfkit/pdfkit

In the controller I have

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 

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> 

Note that I have already used the absolute path to chartkick.js

Now in html the contents of the diagram were perfectly displayed.

But in the pdf file generated by pdfkit, the entire contents of the diagrams were shown as β€œloading”, while the text was correctly displayed

Included in Google but could not find an answer.

+1
source share
2 answers

Finally, after 3 days of searching, traces and errors, I found a solution for this

First of all, I have to use wicked_pdf, not pdfkit, because the first provides two important helper functions

 wicked_pdf_stylesheet_link_tag wicked_pdf_javascript_include_tag 

then I have to use these two helpers to include a stylesheet and javascript like

 <%= 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'%> 

The difference between javascript_include_tag and wicked_pdf_javascript_include_tag is that the latter will write all the original java code to the html file. This is important because wkhtmltopdf, the core of wicked_pdf, does not know where to find js and css assets. These assets are located in some place, for example / usr / local / lib / ruby ​​/ gems / 2.1.0 / gems / (you may have something else), but only your rails application knows where it is

note that for

 <%= javascript_include_tag "http://www.google.com/jsapi"%> 

you cannot and do not need wicked_pdf_javascript_include_tag.

The most important thing here. wicked_pdf (as well as pdfkit) will instruct you to install the gem wkhtmltopdf-binary, which has the latest version 0.9.9, and then just call something like this

 format.pdf {render pdf :"test",layout:'application',template:'stinfos/testpage.html.erb'} 

to create pdf. This ONLY works for html without a chart or high performance component. In the created PDF file, the chart or high-risk figures will be displayed only as "download"

To solve this problem, you need to remove the wkhtmltopdf binary pearls by running

 gem uninstall wkhtmltopdf-binary 

in rails root directory

Then download the latest version of wkhtmltopdf from http://wkhtmltopdf.org/downloads.html

the precompiled version http://downloads.sourceforge.net/project/wkhtmltopdf/0.12.1/wkhtmltox-0.12.1_linux-trusty-amd64.deb works for me

and install

 dpkg -i wkhtmltox-0.12.1_linux-trusty-amd64.deb 

Most likely it will be located here / usr / local / bin / wkhtmltopdf

If not, change config / initializers / wicked_pdf.rb

 :exe_path => '/yourpath/wkhtmltopdf' 

Then add javascript_delay: 2000 to .pdf format

  format.pdf {render pdf:"test",javascript_delay:2000, layout:'application',template:'stinfos/testpage.html.erb'} 

Then you should see that the chart numbers are displayed in pdf format. If not, use a longer delay time.

Please note that the javascript_delay option is not available for wkhtmlpdf below version 0.10

+6
source

While WickedPDF probably works very well, there is a workaround for creating PDFKit. You just need to point wkhtmltopdf to the absolute path of the .js file, similar to the approach used to include CSS files:

 html = render_to_string(:layout => 'application' , :action => "testpage.html.erb") html.gsub!("/assets", "#{Rails.root}/public/assets") 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') 

The precompiled chartkick.js will be located in the /public/assets folder, and wkhtmltopdf will be able to find it there.

0
source

Source: https://habr.com/ru/post/1496400/


All Articles