SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
Tomasz Borowski
                  programmer at Selleo
                  t.borowski@selleo.com




        Generating beautiful PDF files
        wkhtmltopdf and PDFKit in action




Bielsko-Biała, 2011-01-10
What is the problem
Generating PDF files may be a problem in web applications:


    ●   tool is not based on HTML tags
    ●   tool does not interpret css definitions
    ●   tool interpret only few css definitions
    ●   output PDF document looks just awful
    ●   ...
    ●   tool is excellent but... it is not free :)
Solution




Use wkhtmltopdf utility
Why this tool
Several reasons to use wkhtmltopdf


   ●   It is based on Webkit engine
   ●   It interprets JavaScript, which may be used to deliver some
       content
   ●   It has number of useful options for PDF generation process
   ●   It outputs nice looking PDFs!
   ●   ...
   ●   It is free (under GNU General Public License v3)
Simple Examples
wkhtmltopdf http://cnn.com cnn_page.pdf
wkhtmltopdf http://en.wikipedia.org wiki_page.pdf
What If I need more
    wkhtmltopdf allows you to generate pdf:


●   from multiple resources
●   with customized headers and footers (ex. with page numbers)
●   with automatically generated table of contents
●   in grayscale, economic way ready to print
●   In custom size, zoom, orientation, with custom margins and...

    much more :)
Ruby on Rails usage


You can use wkhtmltopdf through PDFKit gem!
  ●   customize default PDF settings in initializer
  ●   use middleware to get PDF view of any page in application
  ●   save PDF on server or render to browser
Ruby on Rails usage: installation
PDFKit:
gem install pdfkit

wkhtmltopdf:
# download: http://code.google.com/p/wkhtmltopdf/downloads/list

xz -d wkhtmltopdf-xxx.tar.lzma
tar -xf wkhtmltopdf-xxx.tar
sudo mv wkhtmltopdf-xxx /usr/local/bin/wkhtmltopdf
Ruby on Rails: usage
 You can use wkhtmltopdf through PDFKit gem!
    ●   customize default PDF settings in initializer
    ●   use middleware to get PDF view of any page in application
    ●   save PDF on server or render to browser

kit = PDFKit.new(html, :page_size => 'Letter')
pdf = kit.to_pdf # Get an inline PDF
file = kit.to_file('/path/to/pdf') # Save the PDF to a file
Ruby on Rails: config
You can customize default settings of PDFKit


# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  config.wkhtmltopdf = '/path/to/wkhtmltopdf'
  config.default_options = {
      :page_size => 'Legal',
      :print_media_type => true
  }
end
Ruby on Rails: middleware
You can create PDF view of any page of your application.
It will be accessible by using .pdf format, ex. /path/to/index.pdf


All pages:
# in application.rb
require 'pdfkit'
config.middleware.use PDFKit::Middleware


Certain action example:
# in environment.rb
Mime::Type.register "application/pdf", :pdf
# in action
respond_to do |format|
       format.html # index.html.erb
       format.pdf { render :text => PDFKit.new( post_url(@post) ).to_pdf }
end
Sources
●   wkhtmltopdf homepage
    http://code.google.com/p/wkhtmltopdf/
●   wkhtmltopdf manual
    http://madalgo.au.dk/~jakobt/wkhtmltopdf-0.10.0_beta4-doc.html
●   PDFKit homepage
    https://github.com/jdpace/PDFKit

Weitere ähnliche Inhalte

Mehr von Tomasz Borowski

Produkcja aplikacji internetowych
Produkcja aplikacji internetowychProdukcja aplikacji internetowych
Produkcja aplikacji internetowychTomasz Borowski
 
How I Became a Spaceship Commander
How I Became a Spaceship CommanderHow I Became a Spaceship Commander
How I Became a Spaceship CommanderTomasz Borowski
 
Grywalizacja w trosce o zdrowie
Grywalizacja w trosce o zdrowieGrywalizacja w trosce o zdrowie
Grywalizacja w trosce o zdrowieTomasz Borowski
 
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowychTomasz Borowski
 
Rozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na światRozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na światTomasz Borowski
 

Mehr von Tomasz Borowski (7)

Produkcja aplikacji internetowych
Produkcja aplikacji internetowychProdukcja aplikacji internetowych
Produkcja aplikacji internetowych
 
How I Became a Spaceship Commander
How I Became a Spaceship CommanderHow I Became a Spaceship Commander
How I Became a Spaceship Commander
 
Grywalizacja w trosce o zdrowie
Grywalizacja w trosce o zdrowieGrywalizacja w trosce o zdrowie
Grywalizacja w trosce o zdrowie
 
Haxorz day 7
Haxorz day 7Haxorz day 7
Haxorz day 7
 
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
7 rzeczy, które zespoły IT mogą nauczyć się ze sportów drużynowych
 
Rozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na światRozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na świat
 
Psychologia w grach
Psychologia w grachPsychologia w grach
Psychologia w grach
 

Kürzlich hochgeladen

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 

Generating beautiful PDF files

  • 1. Tomasz Borowski programmer at Selleo t.borowski@selleo.com Generating beautiful PDF files wkhtmltopdf and PDFKit in action Bielsko-Biała, 2011-01-10
  • 2. What is the problem Generating PDF files may be a problem in web applications: ● tool is not based on HTML tags ● tool does not interpret css definitions ● tool interpret only few css definitions ● output PDF document looks just awful ● ... ● tool is excellent but... it is not free :)
  • 4. Why this tool Several reasons to use wkhtmltopdf ● It is based on Webkit engine ● It interprets JavaScript, which may be used to deliver some content ● It has number of useful options for PDF generation process ● It outputs nice looking PDFs! ● ... ● It is free (under GNU General Public License v3)
  • 5. Simple Examples wkhtmltopdf http://cnn.com cnn_page.pdf wkhtmltopdf http://en.wikipedia.org wiki_page.pdf
  • 6. What If I need more wkhtmltopdf allows you to generate pdf: ● from multiple resources ● with customized headers and footers (ex. with page numbers) ● with automatically generated table of contents ● in grayscale, economic way ready to print ● In custom size, zoom, orientation, with custom margins and... much more :)
  • 7. Ruby on Rails usage You can use wkhtmltopdf through PDFKit gem! ● customize default PDF settings in initializer ● use middleware to get PDF view of any page in application ● save PDF on server or render to browser
  • 8. Ruby on Rails usage: installation PDFKit: gem install pdfkit wkhtmltopdf: # download: http://code.google.com/p/wkhtmltopdf/downloads/list xz -d wkhtmltopdf-xxx.tar.lzma tar -xf wkhtmltopdf-xxx.tar sudo mv wkhtmltopdf-xxx /usr/local/bin/wkhtmltopdf
  • 9. Ruby on Rails: usage You can use wkhtmltopdf through PDFKit gem! ● customize default PDF settings in initializer ● use middleware to get PDF view of any page in application ● save PDF on server or render to browser kit = PDFKit.new(html, :page_size => 'Letter') pdf = kit.to_pdf # Get an inline PDF file = kit.to_file('/path/to/pdf') # Save the PDF to a file
  • 10. Ruby on Rails: config You can customize default settings of PDFKit # config/initializers/pdfkit.rb PDFKit.configure do |config| config.wkhtmltopdf = '/path/to/wkhtmltopdf' config.default_options = { :page_size => 'Legal', :print_media_type => true } end
  • 11. Ruby on Rails: middleware You can create PDF view of any page of your application. It will be accessible by using .pdf format, ex. /path/to/index.pdf All pages: # in application.rb require 'pdfkit' config.middleware.use PDFKit::Middleware Certain action example: # in environment.rb Mime::Type.register "application/pdf", :pdf # in action respond_to do |format| format.html # index.html.erb format.pdf { render :text => PDFKit.new( post_url(@post) ).to_pdf } end
  • 12. Sources ● wkhtmltopdf homepage http://code.google.com/p/wkhtmltopdf/ ● wkhtmltopdf manual http://madalgo.au.dk/~jakobt/wkhtmltopdf-0.10.0_beta4-doc.html ● PDFKit homepage https://github.com/jdpace/PDFKit