# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
# .ebextensions/install-wkhtmltopdf.config
container_commands:
01_install_wkhtmltopdf:
ignoreErrors: true
command: yum -y install fontconfig libXrender libXext xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi freetype libpng zlib libjpeg-turbo openssl icu
02_install_wkhtmltopdf:
# see: https://wkhtmltopdf.org/downloads.html for updates
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: wget https://downloads.wkhtmltopdf.org/0.12/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz --dns-timeout=5 --connect-timeout=5
03_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: tar -xJf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
04_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: cp wkhtmltox/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
05_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: cp wkhtmltox/bin/wkhtmltoimage /usr/local/bin/wkhtmltoimage
06_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: mkdir /home/webapp
07_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: chown -R webapp:webapp /home/webapp
08_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: ln -s /usr/local/bin/wkhtmltopdf /home/webapp/wkhtmltopdf
09_install_wkhtmltopdf:
ignoreErrors: true
test: test ! -f .wkhtmltopdf
command: chown webapp:webapp /home/webapp/wkhtmltopdf
10_install_wkhtmltopdf:
command: touch .wkhtmltopdf
# products_controller.rb
def show
respond_to do |format|
format.html {}
format.pdf do
html = render_to_string(template: 'products/show.pdf.erb', layout: 'layouts/application.pdf.erb')
pdf = WickedPdf.new.pdf_from_string(html)
send_data(pdf, filename: 'product.pdf', type: 'application/pdf', disposition: :attachment)
end
end
end
# layouts/application.pdf.erb
<!doctype html>
<html>
<head>
<meta charset='utf-8' />
<%= wicked_pdf_stylesheet_link_tag 'pdf' %>
</head>
<body>
<div>
<%= yield %>
</div>
</body>
</html>
# vendor/assets/stylesheets/pdf.css
/* pdf.css */
# initializers/wicked_pdf.rb
Rails.application.config.assets.precompile += %w( pdf.css )
# products/show.pdf.erb
<p>
<strong>Name:</strong>
<%= @product.name %>
</p>
<p>
<strong>Price:</strong>
<%= @product.price %>
</p>
<p>
<strong>Description:</strong>
<%= @product.description %>
</p>
<p class='image'>
<%= wicked_pdf_image_tag rails_blob_url(@product.image), class: 'img-responsive' %>
</p>
# products/show.html.erb
<%= link_to 'PDF', [@product, format: :pdf] %>
# initializers/wicked_pdf.rb
WickedPdf.config = {
# exe_path: '/usr/local/bin/wkhtmltopdf'
}