ruby and rails pdf generation with prawn, by john mccaffrey at windycityrails 09
DESCRIPTION
Slides and and tutorial info about PDF generation in Ruby/Rails, including the most commonly used libraries, techniques, an in depth review of Prawn, with advanced examples including google charts and twitter to create a dynamic pdf. There are links to the generated pdfs and further information on http://prawn.heroku.comTRANSCRIPT
![Page 1: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/1.jpg)
John McCaffrey
Rails PDF Generation
![Page 2: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/2.jpg)
What we’re going to cover
Agenda★ PDF Types and Libraries★ Prawn★ General PDF Testing★ Advanced Prawn examples★ Prawn-to★ Q&A
![Page 3: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/3.jpg)
Intro:after_me
![Page 4: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/4.jpg)
John McCaffreyPresented at WindyCityRails 08
Using Prawn since 10/08
http://www.pathf.com/blogs/author/john-mccaffrey/
![Page 5: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/5.jpg)
And you are?:include => :first_name
Col 1 Col 2 Col 3 Col 4
![Page 6: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/6.jpg)
Why users Love PDFs
They are great for:★ Reports★ Static data★ Forms★ Invoices★ Tightly controlled formatting★ Print friendly★ Portable★ Looks the same for everyone
![Page 7: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/7.jpg)
PDF types and libraries
![Page 8: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/8.jpg)
PDF Library types
•HTML to PDF•PDF Template binding•Dynamic
![Page 9: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/9.jpg)
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?)
![Page 10: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/10.jpg)
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
![Page 11: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/11.jpg)
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
![Page 12: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/12.jpg)
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
![Page 13: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/13.jpg)
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
![Page 14: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/14.jpg)
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
![Page 15: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/15.jpg)
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
![Page 16: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/16.jpg)
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
![Page 17: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/17.jpg)
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
![Page 18: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/18.jpg)
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
![Page 19: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/19.jpg)
Prawn examples
![Page 20: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/20.jpg)
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
![Page 21: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/21.jpg)
Examples
Methods★ PDF★ Text★ Bounding_box★ Font★ Move_down★ Mask★ Page★ Image★ Stroke
![Page 22: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/22.jpg)
Examples
Basic syntax examples★Text★Images★Overflow★Orientation
![Page 24: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/24.jpg)
Text overflow
Text_overflow_sample
![Page 25: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/25.jpg)
Page Orientation
orientation sample
![Page 26: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/26.jpg)
Examples
Intermediate★ Table★ Utf-8★ Drawing★ Bounding box
Install★ Gem install prawn-layout
![Page 27: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/27.jpg)
Tables
table sample
![Page 28: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/28.jpg)
Utf-8
UTF-8 sample
![Page 29: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/29.jpg)
Drawing & bounding_box
bounding
![Page 30: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/30.jpg)
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
![Page 31: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/31.jpg)
PDF TESTING
![Page 32: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/32.jpg)
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
![Page 33: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/33.jpg)
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
![Page 34: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/34.jpg)
Testing PDF structure
![Page 35: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/35.jpg)
Testing PDF contents
![Page 36: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/36.jpg)
Prawn-to
![Page 37: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/37.jpg)
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)
![Page 38: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/38.jpg)
Prawn-to: controller
![Page 39: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/39.jpg)
Prawn-to: view
Prawn-to sample
![Page 40: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/40.jpg)
Advanced Prawnwith_reckless_abandon
![Page 41: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/41.jpg)
Advanced examples
What else can we do?★ Grid based layout★ Labels & Calendars★ Annotations & Links★ Google charts
![Page 42: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/42.jpg)
Grid Layout
Grid sample
![Page 43: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/43.jpg)
Grids & Labels
Labels sample
![Page 44: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/44.jpg)
Google Charts api
Charts sample
![Page 45: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/45.jpg)
Prawn-FormatNice and easy, just got nicer, and easier
![Page 46: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/46.jpg)
Prawn-format
Use HTML and style syntax ★ Basic tags★ Alter existing styles★ Create new styles★ Links
![Page 47: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/47.jpg)
Prawn-format: html and style
Format
![Page 48: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/48.jpg)
Putting it all together
Mash up ★ Prawn, prawn-layout★ Prawn-to★ Prawn-format★ Google charts api ★ Twitter- search★ Heroku & git
Twitter_Timelines
![Page 49: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/49.jpg)
More to come
Roadmap & projects ★ Security & encryption (prawn-security)★ Prawn-js★ Clean up ★ Improved grid/table support★ Improved docs and examples
![Page 50: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/50.jpg)
Questions?
speakerrate.com/jmccaffrey
![Page 51: Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09](https://reader034.vdocuments.us/reader034/viewer/2022051411/5469d517af79594f098b4de1/html5/thumbnails/51.jpg)
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