Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
#gem 'wkhtmltopdf-binary-edge' # fork of wkhtmltopdf-binary,不需要
install libxrender1
error while loading shared libraries: libXrender.so.1
sudo apt-get install libxrender1
in config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
define template in app/views/layouts/pdf.html.erb
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<%= javascript_include_tag "https://code.jquery.com/jquery-3.3.1.slim.min.js" %>
<%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
<%= javascript_include_tag "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" %>
<%= javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" %>
</head>
<body onload='number_pages'>
<div id="header">
</div>
<div id="content">
<%= yield %>
</div>
</body>
</html>
<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
<%= wicked_pdf_javascript_include_tag "number_pages" %>
specify template
config/initlializers/wicked_pdf.rb
layout: 'pdf.html'
Use it in controllers
in app/controller/records_controller.rb
format.pdf do
render pdf: "records",
template: "records/index.html.erb"
# layout: "pdf.html"
end
If you use assets, pre-compile it for production. in config/initializers/assets.rb
. e.g.
Rails.application.config.assets.precompile += ['blueprint/screen.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js']
Chinese character problem.
<meta charset='utf-8' />
sudo apt-get install fonts-wqy-zenhei