Resources

WickedPDF - https://github.com/mileszs/wicked_pdf

7/5/2020 Update - I've noticed that the link to install wkhtmltopdf is no longer working properly for downloading the binary. I've confirmed that this config works instead.

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://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox-0.12.5-1.centos6.x86_64.rpm --dns-timeout=5 --connect-timeout=5

    03_install_wkhtmltopdf:
        ignoreErrors: true
        test: test ! -f .wkhtmltopdf
        command: sudo rpm -i --force wkhtmltox-0.12.5-1.centos6.x86_64.rpm

    04_install_wkhtmltopdf:
        ignoreErrors: true
        test: test ! -f .wkhtmltopdf
        command: mkdir /home/webapp

    05_install_wkhtmltopdf:
        ignoreErrors: true
        test: test ! -f .wkhtmltopdf
        command: sudo chown -R webapp:webapp /home/webapp

    06_install_wkhtmltopdf:
        ignoreErrors: true
        test: test ! -f .wkhtmltopdf
        command: sudo ln -s /usr/local/bin/wkhtmltopdf /home/webapp/wkhtmltopdf

    07_install_wkhtmltopdf:
        ignoreErrors: true
        test: test ! -f .wkhtmltopdf
        command: sudo chown webapp:webapp /home/webapp/wkhtmltopdf

    08_install_wkhtmltopdf:
        command: touch .wkhtmltopdf
Download Source Code

Summary

# 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'
}