简体   繁体   中英

Rails wicked_pdf (wkhtmltopdf) small images and bugs in production environment

For some reason my Rails app that uses wicked_pdf gem to generates PDF files (a gem that uses the software wkhtmltopdf) is generating different PDF in development and production.

This is how it outputs in development, is almost fine: Click here to view .
This is how outputs in production, with a lot of errors: Click here to view .

The real difference in the system is that in development I use the wkhtmltopdf 0.9.9 in OSX. In production I use wkhtmltopdf 0.9.9 in Ubuntu. Once the Ubuntu Server 14.04 don't use X Server (and the wkhtmltopdf need it) I installed the Xorg and make the follow wrapper:

/usr/bin/wkhtmltopdf.sh

xvfb-run -a -s "-screen 0 1024x768x32" wkhtmltopdf "$@"

And the follow code in Rails initializers:

/config/initializers/wicked_pdf.rb

if Rails.env.production?
  WickedPdf.config = {
    :exe_path => '/usr/bin/wkhtmltopdf.sh'
  }
else
  WickedPdf.config = {
    :exe_path => '/usr/local/bin/wkhtmltopdf'
  }
end

And that's the controller code that I render the PDF:

render :pdf => "curriculum",
        :template => 'kurrics/kurrics.pdf.erb',
        :margin => { :top => 4, :bottom => 4, :left => 0, :right => 0 },
        :encoding => "utf8",
        :print_media_type => true

And if its needed, the PDF template layout:
https://gist.github.com/fschuindt/e173c05d0cc7378df105

I am pretty lost, any tip?

Your problem might have already been solved. But this answer will helpful for other.

As your pdf samples, the problems are with external css and background image. I had the same problem and, I added internal css line as below in pdf template. (Please see this issue )

background-image: url('file://<%= Rails.root.join('app/assets/images/colorful-triangles.jpg') %>');

This method works in both development and production environments.

It is a best practice to use separate css files for pdf. This will speedup your pdf generation and it reduces the server cost. And, avoid loading unwanted css files and images to pdf layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM