Convert HTML to PDF in Ruby on Rails Applications – Part-2

Siva KB

1 min read

This blog is continuation of Convert HTML to PDF in Ruby on Rails Applications – Part-1

Bootstrap css load

We used bootstrap css for our pdf design, it worked perfectly in our development environment, but when we deployed it in server and while testing with some real large data, pdf page crashed unexpectedly. while checking logs we found request time out errors.
Then we googled and found many people raised issue in github saying that they are facing performance issue while using bootstrap (PDF generation very slow with bootstrap css #2642), So we removed bootstrap and wrote our own css for pdf and it worked perfectly.
Solution : Write your own css or just copy the necessary bootstrap css into a file and use. It will save lot of time.

Displaying chart

The next major challenge we faced was displaying charts in pdf, In our application we had 2 charts and we needed both in the pdf. Pdf printing starts before the chart renders, then we found javascript_delay option in the wicked_pdf gem.
We tried that in our application by giving 5 sec delay and it worked perfectly for the first chart.

respond_to do |format|
    format.html
    format.pdf do
        render :pdf => 'user_report',
               :javascript_delay => 5000,
               :disposition => 'attachment',
               :template => 'user_report.html.erb'
end

But the second chart din’t load, it is purely based on size of the data, some times it loads faster and some time it takes long time. So we decided that javascript_delay won’t help us.
Then we decided to save the chart as an image while previewing pdf page and embed that image in the pdf page, while searching for that we came across this solution html2canvas.
Image_embedded
I will share the remaining challenges and resolutions in my next blog. Share if you found this useful. Follow @spritlesoftware for more updates.

Related posts:

One Reply to “Convert HTML to PDF in Ruby on Rails Applications…”

  1. Thanks. Can you blog on how to convert HTML to PDF in Open Source Rhomobile 5.5.x ?

Leave a Reply

Your email address will not be published. Required fields are marked *