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

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
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
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
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
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
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
 
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
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
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
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
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
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 

Kürzlich hochgeladen (20)

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
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
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
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
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
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!
 
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
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
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
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
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
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.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