1

I generate PDF with WickedPdf and it works like a charm locally...

I am facing trouble on Heroku. I can download the pdf but it has no style at all.... (nor in my orders nor in my stickers)

Here is my code:

The pdf.scssis in vendor/assets/stylesheets

orders_controller.rb

def show
  respond_to do |format|
    format.html { }
    format.pdf do
     html = render_to_string(template: "clients/orders/show.pdf.erb", layout: "layouts/application.pdf.erb", orientation: "Landscape", page_size: 'A4', encoding:"UTF-8" )
     pdf = WickedPdf.new.pdf_from_string(html)
     send_data(pdf, filename: "order.pdf", type: "application/pdf", disposition: 'attachment')     
    end
  end
end

stickers_controller.rb

def show
  respond_to do |format|
    format.html { }
    format.pdf do 
      html = render_to_string(template: "admin/stickers/show.pdf.erb", layout: "layouts/application.pdf.erb", orientation: "Landscape" )
      pdf = WickedPdf.new.pdf_from_string(html)
      send_data(pdf, filename: "sticker.pdf", type: "application/pdf", disposition: 'attachment')     
    end
  end
end

config/initializer/assets.rb

Rails.application.config.assets.precompile += %w( pdf.scss )

config/initializer/wicked_pdf.rb

if Rails.env.production?
  WickedPdf.config = {
   exe_path: Gem.bin_path('wkhtmltopdf-heroku', 'wkhtmltopdf-linux-amd64'),
   page_size: 'Letter',
   orientation: 'Portrait',
   margin: { top:    2,
           bottom: 2,
           left:   2,
           right:  2 }
 }

else

 WickedPdf.config = {
    exe_path: '/usr/local/bin/wkhtmltopdf',
    page_size: 'Letter',
    orientation: 'Portrait',
    margin: { top:    2,
             bottom: 2,
             left:   2,
             right:  2 }

  }
end

Gemfile

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary', '~> 0.12.3'
gem "wkhtmltopdf-heroku"

layouts/application.pdf.erb

<html lang="fr">
<head>
  <meta charset="utf-8" />
    <%= wicked_pdf_stylesheet_link_tag 'pdf' %>
</head>
<body>
  <div>
    <%= yield %>
  </div>
</body>
</html>

Also in both my orders and stickers show.pdf.erb I use bootstrap CDN at the top (I use bootstrap 4 for the rest of the app but it seems that Wickedpdf doesn't work with bootstrap 4). The Bootstrap CDN works locally

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<div id="container">
  <div class="row">
    <table class="table table-striped line-items">
    #the test of the code
    </table>
   </div>
</div>

I found this exemple but it's not working for me.. I am on rails 5.2 and that's maybe the difference... It is suggested to add this but it is actually in initializers/assets.rb

In config/environments/production.rb:

config.assets.precompile += ['documents.css.scss.erb']
johan
  • 721
  • 5
  • 21

1 Answers1

0

So the main style problem came from Bootstrap.

The Bootstrap CDN link didn't made the job, so I ended up with downloading the bootstap css. Basically I unchecked all and only kept what needed like the common css.

I minified the bootstrap css, pasted it in a new file bootstrap.min.css and placed it under vendor/assets/stylesheets/with pdf.scss

Then I've imported the boostrap file in my pdf.scss, like so:

@import "bootstrap.min";
johan
  • 721
  • 5
  • 21