51
John McCaffrey Rails PDF Generation

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

John McCaffrey

Rails PDF Generation

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

What we’re going to cover

Agenda★ PDF Types and Libraries★ Prawn★ General PDF Testing★ Advanced Prawn examples★ Prawn-to★ Q&A

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

Intro:after_me

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

John McCaffreyPresented at WindyCityRails 08

Using Prawn since 10/08

http://www.pathf.com/blogs/author/john-mccaffrey/

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

And you are?:include => :first_name

Col 1 Col 2 Col 3 Col 4

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

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

PDF types and libraries

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

PDF Library types

•HTML to PDF•PDF Template binding•Dynamic

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

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

Prawn examples

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

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

Examples

Methods★ PDF★ Text★ Bounding_box★ Font★ Move_down★ Mask★ Page★ Image★ Stroke

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

Examples

Basic syntax examples★Text★Images★Overflow★Orientation

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

Text & Image

Text_image_sample

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

Page Orientation

orientation sample

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

Examples

Intermediate★ Table★ Utf-8★ Drawing★ Bounding box

Install★ Gem install prawn-layout

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

Tables

table sample

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

Utf-8

UTF-8 sample

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

Drawing & bounding_box

bounding

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

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

PDF TESTING

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

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

Testing PDF structure

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

Testing PDF contents

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

Prawn-to

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

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: PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey

Prawn-to: controller

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

Prawn-to: view

Prawn-to sample

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

Advanced Prawnwith_reckless_abandon

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

Advanced examples

What else can we do?★ Grid based layout★ Labels & Calendars★ Annotations & Links★ Google charts

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

Grid Layout

Grid sample

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

Grids & Labels

Labels sample

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

Google Charts api

Charts sample

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

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

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

Prawn-format

Use HTML and style syntax ★ Basic tags★ Alter existing styles★ Create new styles★ Links

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

Prawn-format: html and style

Format

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

Putting it all together

Mash up ★ Prawn, prawn-layout★ Prawn-to★ Prawn-format★ Google charts api ★ Twitter- search★ Heroku & git

Twitter_Timelines

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

More to come

Roadmap & projects ★ Security & encryption (prawn-security)★ Prawn-js★ Clean up ★ Improved grid/table support★ Improved docs and examples

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

Questions?

speakerrate.com/jmccaffrey

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

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 RailsJohn McCaffrey