SlideShare uma empresa Scribd logo
1 de 12
Baixar para ler offline
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

Mais conteúdo relacionado

Mais de Tomasz Borowski

Rozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na światRozszerzona rzeczywistość - nowe spojrzenie na świat
Rozszerzona rzeczywistość - nowe spojrzenie na świat
Tomasz Borowski
 

Mais de 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
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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