• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey
 

PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

on

  • 42,775 views

breakdown of the most commonly used pdf libraries in rails projects,and an in depth review of prawn ...

breakdown of the most commonly used pdf libraries in rails projects,and an in depth review of prawn

example pdfs and code can be seen at prawn.heroku.com
More info at www.RailsPerformance.com

Statistics

Views

Total Views
42,775
Views on SlideShare
25,789
Embed Views
16,986

Actions

Likes
13
Downloads
247
Comments
3

32 Embeds 16,986

http://prawn.heroku.com 11447
http://prawn.herokuapp.com 3435
http://www.akitaonrails.com 1014
http://akitaonrails.com 728
http://www.slideshare.net 191
http://www.infoblogs.com.br 42
http://www.plugmasters.com.br 22
http://translate.googleusercontent.com 15
http://webcache.googleusercontent.com 13
http://lanyrd.com 11
http://www.e-presentations.us 10
http://www.akitaonrails.com.br 10
http://demo.family.dev4.sibers.com 7
http://www.rubyonrails.pro.br 7
https://prawn.herokuapp.com 6
http://www.linkedin.com 5
http://marislasol.blogspot.com 3
http://static.slidesharecdn.com 2
http://localhost:3000 2
http://akitaonrails.com.br 2
http://www.rails.com.br 2
https://prawn.heroku.com 2
http://74.125.113.132 1
http://forum.rubyonbr.org 1
http://family.local 1
http://matematicaetudomais.blogspot.com.br 1
http://prawn-cedar.herokuapp.com 1
http://localhost:3001 1
http://marislasol.blogspot.com.ar 1
http://209.85.129.132 1
http://coderwall.com 1
http://marislasol.blogspot.com.es 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

13 of 3 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • In addition to PDF generation, I also focus on Web and Ruby/Rails Performance info at www.RailsPerformance.com
    Are you sure you want to
    Your message goes here
    Processing…
  • updated the links in the example slides to point to the generated pdfs on http://prawn.heroku.com

    which also contains a video of this presentation from the windycityrails.org conference, and the ability to follow the video and the slides at the same time so you can better understand what was presented.

    There are live examples, and further updates of prawn, prawn-to, google charts api, and the twitter mashup.
    Are you sure you want to
    Your message goes here
    Processing…
  • Pictures of the event at this time
    http://picasaweb.google.com/jangdiafoto/WindyCityRails2009#5380828812420908162
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • What I want you to walk away with: an understanding of the types of libraries out there, and the general strengths/weaknesses of each What prawn can and can’t do General pdf testing techniques some advanced examples and libraries
  • Ask them to turn in and introduce them selves to at least one new person Bring up the ‘Used it’, ‘Still Using it’ papers
  • PDFs are the defacto standard for reports, invoices and print friendly documents Sometimes its just easier than making the HTML print friendly The Pdf spec is quite large and complicated, you can do a lot with pdfs. One of the projects we had a Pathfinder was a PDF annotation tool for the Construction space But today I’m going to focus on the more common uses of PDF, for reports and basic print-friendly content
  • Not all PDFs are created equally. When I started looking into PDF generation in ruby I had some basic requirements, but pdfs can be complicated, and not all the tools will handle your needs. Let’s take a look at what’s out there
  • To me the libraries fall in three main categories Taking rendered html and converting or printing it as a pdf Creating a static pdf file and binding data against it Writing the code that creates the pdf components from scratch (now there are other pdf manipulation tools out there, but we’re talking about creation, not editing)
  • Show example of kocg form, bluebook blueprint, Mention that the library may be os specific which could affect the deployment process
  • May also depend on the team skills While its great to be able to reuse something you’ve already built, its pretty common to want the PDFs to have some additional element, be it security styling, etc
  • Get more details of these types Just because a library is commercial doesn’t mean you shouldn’t use it. Evaluate the true cost of your time In the case of HTMLDOC, its not like you are taking your full site and instantly getting pdfs out of it. Mostly you will be altering your view or creating a second view that looks the way you want it without external css. Htmlconverter – haven’t used it http:// github.com/dcu/htmlconverter/tree/master Pd4ml - http:// www.extonrails.com/?q =node/26 similar to prince, but costs less
  • Show example of kocg form. Now this is an example of a pdf that is better suited in the template style, but we’re going to be focusing more on regular reporting style pdfs
  • What other libraries are there to do this? Both pdftk and iText go beyond just template binding. They are great libraries and if you are not looking for an open source, but not necessarily pure-ruby solution, you should check them out Is pdftk tcl based? Pragmatic uses iText to insert the ‘this book licensed only to Darth Vader’ footer
  • You want to be mindful of the document size, because you are building so much. Not sure if there is any way to cache fragments. Prawn-to has a concept of compiled templates, but I’m not sure what that’s about
  • http://www.cnblogs.com/hardrock/archive/2006/07/24/458184.html RubyFpdf – php port Pdflib and rfpdf wrapper for it Does jasper reports just call out to iText? What about Flyingsaucer java How much faster is prawn than pdf writer? 40 times? And even faster on ruby 1.9
  • Prawn seemed to be the only one that had good test coverage
  • RBP will go creative-commons free in march 2010 Ruport is an interesting framework, and if you have to output reports in more than one format (csv, html, pdf) you should take a look at it. Currently the pdf side of it isn’t using prawn under the covers, but it will be soon
  • For positioning: you create your bounding box, then do all commands relative to that space Prawn was good for me, and depending on what you are doing, there’s a good chance it will be good for you too. Let’s take a look under the hood and see what it can do. Even if you feel that Prawn is not the right library for you, I’m hoping you’ll still get some good ideas for general pdf structure, testing and maintenance Prawn is moving fast, and there are a lot of related sub projects going on. So while it might not cover your needs right now, there’s a good chance it will soon, so keep an eye out.
  • Show a screen shot of the tests passing
  • Text – type it out and show how simple the initial syntax is Images – show just local file images first Bounding box – show the relative coordinates, and how easy it is Overflow – show how the page flow is taken care of Orientation – show landscape, portrait, and mixing the two within a document (how does that print?)
  • http://groups.google.com/group/pdf-writer/browse_thread/thread/5bc8de74e1a7d3c5 Groupon recently started using prawn to render the coupons, and they were able to get a good looking first cut in less than a day, and finished the final version pretty quickly They are using prawn in production to serve up over 8k pdfs a day Most of the pdfs I’ve shown you today are generated in less than 3 secods
  • Also mention image comparisons like bluebook
  • Also mention image comparisons like bluebook
  • http://cracklabs.com/prawnto/
  • Using prawn-to can help you stick to a pure MVC pattern, and not jumbling too much logic into the pdf creation
  • When it becomes easy to make pdfs, you can start to see creative things you never would have before
  • Now you might not want to call out to a 3 rd party for charts in production, but using google charts can allow you to make a quick prototype and move from concept to delivery faster
  • Great way to leverage your existing style concepts Make a script to parse css file and register those styles No support for div or table
  • Is susan Potter here? Sorry for not using twitter4r, it looks great, but I just found this one first I have this up at prawn.heroku.com, but I’m having an issue with one of the gems at the moment
  • Prawn-js for scripting within the pdf http://github.com/yob/prawn-js/tree/master http:// wiki.github.com /sandal/prawn/development-roadmap

PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey Presentation Transcript

  • Rails PDF Generation John McCaffrey
  • What we’re going to cover
    • Agenda
      • PDF Types and Libraries
      • Prawn
      • General PDF Testing
      • Advanced Prawn examples
      • Prawn-to
      • Q&A
  • Intro: after_me
    • John McCaffrey Presented at WindyCityRails 08
    • Using Prawn since 10/08
    • http://www.pathf.com/blogs/author/john-mccaffrey /
    • And you are? :include => :first_name
    Col 1 Col 2 Col 3 Col 4
  • Why users Love PDFs
    • They are great for:
      • Reports
      • Static data
      • Forms
      • Invoices
      • Tightly controlled formatting
      • Print friendly
      • Portable
      • Looks the same for everyone
  • PDF types and libraries
    • PDF Library types
    • HTML to PDF
    • PDF Template binding
    • Dynamic
  • What type should I use?
      • What is the content? (tax form, invoice, eBook, product list)
      • How large will the files be?
      • How complex is the formatting?
      • Do they need to be generated ‘on demand’, or would it be a batch or background job?
      • Do you mind calling out to a library or command line tool?
      • Character encoding, utf-8, internationalization, etc
      • Who will be in charge of maintaining them? (developer or designer?)
  • Use HTML to PDF if..
      • You already have an html view that is structured the way you want it.
      • You don’t want to mess with any ‘pdf syntax’
      • Don’t mind requiring a native library, or command line invocation.
      • Don’t mind the licensing agreements or cost of commercial tools
      • Your team skill set is more aligned with HTML/CSS
  • HTML to PDF Libraries
    • PrinceXML
      • Best in class html to pdf. Passes Acid2 test
      • Princely ruby wrapper
      • Commercial: $3800 server license
    • HTMLDOC
      • Has been around for awhile
      • Supports a subset of html (no css, no xhtml or html 4.0)
      • Supports basic UTF-8/Unicode for ‘western’ languages
    • wkhtmltopdf
      • Based on WebKit rendering engine
      • Might be some issues in different OSes (windows)
      • Relatively new library
  • Use PDF Templates if..
      • The document is a form to be filled in (text fields, checkboxes, etc)
      • The structure is mostly static
      • The document is very large
      • The formatting is complex
      • You don’t mind calling out to a library
  • PDF Template binding
    • pdftk
      • Build document with any tool that can output as pdf
      • Add in the form fields with Acrobat editor
      • Bind fdf data against pdf template
      • Very powerful pdf manipulation features
    • iText
      • Java library
      • Very powerful
      • Well known with lots of examples/tutorials, and books
      • Can also merge/split pdf files, add watermarks, etc
      • Pdf Form Binding example
  • Use PDF Generation if..
      • The content/structure is dynamic
      • The document is not too large
      • The formatting is not too complex
      • You prefer a pure ruby library
      • You prefer an open source solution
  • PDF Generation Libraries
    • JasperReports
      • Well known java library, multiple outputs
      • May complicate dev/deployment dependencies
    • PDF::WRITER
      • Pure Ruby. Has been around for awhile
      • Many examples out there
      • Powerful, but ‘tedious’ syntax for positioning, styling, etc
    • Prawn
      • Lightweight, easy to learn syntax
      • Newer library, still in alpha, but very promising
      • Faster than PDF::Writer
      • Continues to get faster and better
  • I chose Prawn
    • Because…
      • Lots of clear examples
      • Worked well for the report style that I needed (dynamic structure, simple table-based layout)
      • Quick to generate pdfs on demand
      • Fastest pure ruby pdf tool out there
      • Code was well tested and easy to follow
      • Forum/mailing list was active and helpful
  • Prawn history
    • Gregory Brown
      • Mendicant project, raised $10k to work on open-source projects
      • Goal was to improve report and pdf generation for ruby
      • Ruport reporting framework (multiple report types)
      • Prawn
      • Just released Ruby Best Practices book
  • Prawn 101
    • What it can’t do
      • Complex formatting, nested tables
      • Edit existing pdfs
      • Encryption/security (coming soon – 9/15)
    • What it can do
      • Very easy to learn syntax
      • Easy Image embedding
      • Easy to manage table-based layouts
      • Simplified positioning commands
  • Prawn examples
  • Examples
    • Install
      • Gem install prawn
      • Or git clone git://github.com/sandal/prawn.git
      • Make sure the tests pass
        • May have to install another submodule or two
  • Examples
    • Methods
      • PDF
      • Text
      • Bounding_box
      • Font
      • Move_down
      • Mask
      • Page
      • Image
      • Stroke
  • Examples
    • Basic syntax examples
      • Text
      • Images
      • Overflow
      • Orientation
  • Text & Image Text_image_sample
  • Text overflow Text_overflow_sample
  • Page Orientation orientation sample
  • Examples
    • Intermediate
      • Table
      • Utf-8
      • Drawing
      • Bounding box
    • Install
      • Gem install prawn-layout
  • Tables table sample
  • Utf-8 UTF-8 sample
  • Drawing & bounding_box bounding
  • Prawn Performance
    • Numbers
      • Around 20 times faster than PDF::Writer
      • Even faster on ruby 1.9
      • Well designed (seems to scale linearly)
      • Images are only loaded once
  • PDF TESTING
  • How do I test a PDF?
    • Tools
      • Basic file and attachment testing
      • PDF::Reader
      • PDF::Inspector
      • Oragami library
      • Convert to Image and do a bit diff
  • How do I test a PDF?
    • Poor-man techniques
      • Nothing blew up
      • File is present
      • Mocks were invoked as expected
      • Generate your pdfs and look at them
    • PDF toolkits
      • Assert page size
      • Open PDF and read its contents
      • Grep for objects
      • Assert order of objects
  • Testing PDF structure
  • Testing PDF contents
  • Prawn-to
  • Prawn in your view
    • All the good stuff
      • script/plugin install git://github.com/thorny-sun/prawnto.git
      • Render pdf template with .pdf.prawn extension
      • DRY up common pdf configuration settings
      • Access to helpers (useful for currency, dates, text)
  • Prawn-to: controller
  • Prawn-to: view Prawn-to sample
  • Advanced Prawn with_reckless_abandon
  • Advanced examples
    • What else can we do?
      • Grid based layout
      • Labels & Calendars
      • Annotations & Links
      • Google charts
  • Grid Layout Grid sample
  • Grids & Labels Labels sample
  • Google Charts api Charts sample
  • Prawn-Format Nice and easy, just got nicer, and easier
  • Prawn-format
    • Use HTML and style syntax
      • Basic tags
      • Alter existing styles
      • Create new styles
      • Links
  • Prawn-format: html and style Format
  • Putting it all together
    • Mash up
      • Prawn, prawn-layout
      • Prawn-to
      • Prawn-format
      • Google charts api
      • Twitter- search
      • Heroku & git
    Twitter_Timelines
  • More to come
    • Roadmap & projects
      • Security & encryption ( prawn-security)
      • Prawn-js
      • Clean up
      • Improved grid/table support
      • Improved docs and examples
  • Questions? speakerrate.com /jmccaffrey
  • Links
    • Prawn: prawn.majesticseacreature.com /
    • Prawn-to: cracklabs.com/prawnto
    • Google Charts: chart.apis.google.com /
    • Twitter-Search: github.com/dancroak/twitter -search
    • Blog: http://www.pathf.com/blogs/author/john-mccaffrey /
    PDF Generation in Rails John McCaffrey