51
John McCaffrey Rails PDF Generation

Ruby and Rails PDF Generation with Prawn, by John McCaffrey at WindyCityRails 09

  • Upload
    jmccaf

  • View
    4.445

  • Download
    0

Embed Size (px)

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.com

Citation preview

Page 1: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

John McCaffrey

Rails PDF Generation

Page 2: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

Intro:after_me

Page 4: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

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

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

PDF types and libraries

Page 8: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

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

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

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

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

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

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

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

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

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

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

Prawn examples

Page 20: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

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

Examples

Basic syntax examples★Text★Images★Overflow★Orientation

Page 23: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Text & Image

Text_image_sample

Page 25: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Page Orientation

orientation sample

Page 26: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

Tables

table sample

Page 28: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Utf-8

UTF-8 sample

Page 29: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Drawing & bounding_box

bounding

Page 30: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

PDF TESTING

Page 32: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

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

Testing PDF structure

Page 35: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Testing PDF contents

Page 36: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Prawn-to

Page 37: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

Prawn-to: controller

Page 39: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Prawn-to: view

Prawn-to sample

Page 40: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Advanced Prawnwith_reckless_abandon

Page 41: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

Grid Layout

Grid sample

Page 43: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Grids & Labels

Labels sample

Page 44: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Google Charts api

Charts sample

Page 45: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

Prawn-FormatNice and easy, just got nicer, and easier

Page 46: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

Prawn-format: html and style

Format

Page 48: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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

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

Questions?

speakerrate.com/jmccaffrey

Page 51: Ruby and Rails PDF Generation with Prawn, by John McCaffrey  at WindyCityRails 09

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