Heroku 上的 WickedPDF 宽松风格

WickedPDF loose style on Heroku

我用 WickedPdf 生成 PDF,它在本地运行得非常好...

我在 Heroku 上遇到了麻烦。我可以下载 pdf 但它根本没有样式....(我的订单和贴纸中也没有)

这是我的代码:

pdf.scssvendor/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>

还有我的订单和贴纸show.pdf.erb我用的是bootstrapCDN 在顶部(我对应用程序的其余部分使用 bootstrap 4,但 Wickedpdf 似乎不适用于 bootstrap 4)。 Bootstrap CDN 在本地工作

<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>

我找到了 this exemple 但它对我不起作用.. 我在 rails 5.2 上,这可能是不同之处...建议添加它,但实际上它在 initializers/assets.rb

config/environments/production.rb中:

config.assets.precompile += ['documents.css.scss.erb']

所以主要的风格问题来自Bootstrap。

Bootstrap CDN link 没有成功,所以我最终下载了 bootstap css。 基本上我取消了所有检查,只保留了需要的东西,比如常见的 css.

我缩小了 bootstrap css,将其粘贴到一个新文件中 bootstrap.min.css 并将其放在 vendor/assets/stylesheets/pdf.scss

然后我在 pdf.scss 中导入了 boostrap 文件,如下所示:

@import "bootstrap.min";