#220 PDFKit
Generating PDFs in plain Ruby can be a lot of work. Instead, consider generating PDFs from HTML using PDFKit.
- Download:
- source codeProject Files in Zip (109 KB)
- mp4Full Size H.264 Video (19.4 MB)
- m4vSmaller H.264 Video (10.9 MB)
- webmFull Size VP8 Video (24.7 MB)
- ogvFull Size Theora Video (26.8 MB)
Resources
- Episode 153: PDFs with Prawn
- Rethinking PDF Creation in Ruby
- PDFKit
- wicked_pdf
- Full episode source code
Gemfile
gem 'pdfkit'
gem 'pdfkit'
bash
bundle install
rake middleware
bundle install rake middleware
config/application.rb
config.middleware.use "PDFKit::Middleware", :print_media_type => true
config.middleware.use "PDFKit::Middleware", :print_media_type => true
layouts/application.html.erb
<%= stylesheet_link_tag 'application', :media => "all" %>
<%= stylesheet_link_tag 'application', :media => "all" %>
orders/show.html.erb
<p id="pdf_link"><%= link_to "Download Invoice (PDF)", order_path(@order, :format => "pdf") %></p>
<p id="pdf_link"><%= link_to "Download Invoice (PDF)", order_path(@order, :format => "pdf") %></p>
application.css
@media print {
body {
background-color: #FFF;
}
#container {
width: auto;
margin: 0;
padding: 0;
border: none;
}
#line_items {
page-break-before: always;
}
#pdf_link {
display: none;
}
}
@media print { body { background-color: #FFF; } #container { width: auto; margin: 0; padding: 0; border: none; } #line_items { page-break-before: always; } #pdf_link { display: none; } }

