我使用WickedPdf生成PDF,它就像当地的魅力......
我在Heroku面临麻烦。我可以下载pdf但它根本没有风格....(也不是我的订单也不是我的贴纸)
这是我的代码:
在pdf.scss
的vendor/assets/stylesheets
is
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
配置/初始化/ assets.rb
Rails.application.config.assets.precompile += %w( pdf.scss )
配置/初始化/ 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"
布局/ 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我在顶部使用bootstrap CDN(我使用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,但它不适合我..我在轨道5.2上,这可能是差异...建议添加这个但它实际上是在initializers / assets.rb
在config/environments/production.rb
:
config.assets.precompile += ['documents.css.scss.erb']
所以主要的风格问题来自Bootstrap。
Bootstrap CDN链接没有成功,所以我最终下载了bootstap css。基本上我没有检查所有,只保留了所需的像常见的CSS。
我缩小了bootstrap css,将其粘贴到一个新的文件bootstrap.min.css
中并将其放在vendor/assets/stylesheets/
with pdf.scss
下
然后我在我的pdf.scss中导入bootstrap文件,如下所示:
@import "bootstrap.min";