SlideShare a Scribd company logo
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

More Related Content

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

Implementing Structured FrameMaker
Implementing Structured FrameMakerImplementing Structured FrameMaker
Implementing Structured FrameMaker
Dkadilak62263
 
Markdown - friend or foe?
Markdown - friend or foe?Markdown - friend or foe?
Markdown - friend or foe?
Ellis Pratt
 
Tcs3 stc creating e pub and apps with tcs3
Tcs3 stc creating e pub and apps with tcs3Tcs3 stc creating e pub and apps with tcs3
Tcs3 stc creating e pub and apps with tcs3
Matt Sullivan
 
1 Introduction to Drupal Web Development
1 Introduction to Drupal Web Development1 Introduction to Drupal Web Development
1 Introduction to Drupal Web Development
Wingston
 
Websites Unlimited - Pay Monthly Websites
Websites Unlimited - Pay Monthly WebsitesWebsites Unlimited - Pay Monthly Websites
Websites Unlimited - Pay Monthly Websites
websiteunlimited
 
How to reduce DTP and translation costs with FrameMaker
How to reduce DTP and translation costs with FrameMakerHow to reduce DTP and translation costs with FrameMaker
How to reduce DTP and translation costs with FrameMaker
Globalization Partners International
 
Drupal Camp Victoria
Drupal Camp VictoriaDrupal Camp Victoria
Drupal Camp Victoria
Image X Media
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docx
fantabulous2024
 
Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...
webhostingguy
 
Using rest to create responsive html 5 share point intranets
Using rest to create responsive html 5 share point intranetsUsing rest to create responsive html 5 share point intranets
Using rest to create responsive html 5 share point intranets
InnoTech
 
Web UI migration
Web UI migrationWeb UI migration
Web UI migration
Doug Lucy
 
Fundamentals of web_design_v2
Fundamentals of web_design_v2Fundamentals of web_design_v2
Fundamentals of web_design_v2
hussain534
 
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Cloudera, Inc.
 
Apex RnD APEX 5 - Printing
Apex RnD APEX 5 - PrintingApex RnD APEX 5 - Printing
Apex RnD APEX 5 - Printing
Lino Schildenfeld
 
Cakefest higher education
Cakefest higher educationCakefest higher education
Cakefest higher education
therealphpdiddy
 
The Trip to DITA
The Trip to DITAThe Trip to DITA
The Trip to DITA
ClearPath, LLC
 
Google Docs for Researchers: Creating, Editing, And Sharing Your Work Online
Google Docs for Researchers: Creating, Editing, And Sharing Your Work OnlineGoogle Docs for Researchers: Creating, Editing, And Sharing Your Work Online
Google Docs for Researchers: Creating, Editing, And Sharing Your Work Online
University of Michigan Taubman Health Sciences Library
 
Production and Distribution of Digital Content
Production and Distribution of Digital ContentProduction and Distribution of Digital Content
Production and Distribution of Digital Content
Digital Publishing Forum NZ
 
Hadoop basics
Hadoop basicsHadoop basics
Hadoop basics
Antonio Silveira
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
Tikal Knowledge
 

Similar to PDF Generation in Rails with Prawn and Prawn-to: John McCaffrey (20)

Implementing Structured FrameMaker
Implementing Structured FrameMakerImplementing Structured FrameMaker
Implementing Structured FrameMaker
 
Markdown - friend or foe?
Markdown - friend or foe?Markdown - friend or foe?
Markdown - friend or foe?
 
Tcs3 stc creating e pub and apps with tcs3
Tcs3 stc creating e pub and apps with tcs3Tcs3 stc creating e pub and apps with tcs3
Tcs3 stc creating e pub and apps with tcs3
 
1 Introduction to Drupal Web Development
1 Introduction to Drupal Web Development1 Introduction to Drupal Web Development
1 Introduction to Drupal Web Development
 
Websites Unlimited - Pay Monthly Websites
Websites Unlimited - Pay Monthly WebsitesWebsites Unlimited - Pay Monthly Websites
Websites Unlimited - Pay Monthly Websites
 
How to reduce DTP and translation costs with FrameMaker
How to reduce DTP and translation costs with FrameMakerHow to reduce DTP and translation costs with FrameMaker
How to reduce DTP and translation costs with FrameMaker
 
Drupal Camp Victoria
Drupal Camp VictoriaDrupal Camp Victoria
Drupal Camp Victoria
 
Company Visitor Management System Report.docx
Company Visitor Management System Report.docxCompany Visitor Management System Report.docx
Company Visitor Management System Report.docx
 
Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...Web Development From the Ground Up, a Series for Novice ...
Web Development From the Ground Up, a Series for Novice ...
 
Using rest to create responsive html 5 share point intranets
Using rest to create responsive html 5 share point intranetsUsing rest to create responsive html 5 share point intranets
Using rest to create responsive html 5 share point intranets
 
Web UI migration
Web UI migrationWeb UI migration
Web UI migration
 
Fundamentals of web_design_v2
Fundamentals of web_design_v2Fundamentals of web_design_v2
Fundamentals of web_design_v2
 
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
Hadoop World 2011: Building Web Analytics Processing on Hadoop at CBS Interac...
 
Apex RnD APEX 5 - Printing
Apex RnD APEX 5 - PrintingApex RnD APEX 5 - Printing
Apex RnD APEX 5 - Printing
 
Cakefest higher education
Cakefest higher educationCakefest higher education
Cakefest higher education
 
The Trip to DITA
The Trip to DITAThe Trip to DITA
The Trip to DITA
 
Google Docs for Researchers: Creating, Editing, And Sharing Your Work Online
Google Docs for Researchers: Creating, Editing, And Sharing Your Work OnlineGoogle Docs for Researchers: Creating, Editing, And Sharing Your Work Online
Google Docs for Researchers: Creating, Editing, And Sharing Your Work Online
 
Production and Distribution of Digital Content
Production and Distribution of Digital ContentProduction and Distribution of Digital Content
Production and Distribution of Digital Content
 
Hadoop basics
Hadoop basicsHadoop basics
Hadoop basics
 
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With DeadlinesJBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
JBUG 11 - Django-The Web Framework For Perfectionists With Deadlines
 

More from John McCaffrey

John's Sample
John's SampleJohn's Sample
John's Sample
John McCaffrey
 
A Taste of TDD: The basics of TDD, why it is hard and how to do it better
A Taste of TDD: The basics of TDD, why it is hard and how to do it betterA Taste of TDD: The basics of TDD, why it is hard and how to do it better
A Taste of TDD: The basics of TDD, why it is hard and how to do it better
John McCaffrey
 
Becoming a more Productive Rails Developer
Becoming a more Productive Rails DeveloperBecoming a more Productive Rails Developer
Becoming a more Productive Rails Developer
John McCaffrey
 
LeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than developmentLeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than development
John McCaffrey
 
Becoming a more productive Rails Developer
Becoming a more productive Rails DeveloperBecoming a more productive Rails Developer
Becoming a more productive Rails Developer
John McCaffrey
 
Cloud tools
Cloud toolsCloud tools
Cloud tools
John McCaffrey
 
Windycityrails page performance
Windycityrails page performanceWindycityrails page performance
Windycityrails page performance
John McCaffrey
 
Irb Tips and Tricks
Irb Tips and TricksIrb Tips and Tricks
Irb Tips and Tricks
John McCaffrey
 
Freelancing and side-projects on Rails
Freelancing and side-projects on RailsFreelancing and side-projects on Rails
Freelancing and side-projects on Rails
John McCaffrey
 
Cvcc performance tuning
Cvcc performance tuningCvcc performance tuning
Cvcc performance tuning
John McCaffrey
 
Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...
Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...
Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...
John McCaffrey
 
Windy cityrails performance_tuning
Windy cityrails performance_tuningWindy cityrails performance_tuning
Windy cityrails performance_tuning
John McCaffrey
 
improving the performance of Rails web Applications
improving the performance of Rails web Applicationsimproving the performance of Rails web Applications
improving the performance of Rails web Applications
John McCaffrey
 

More from John McCaffrey (13)

John's Sample
John's SampleJohn's Sample
John's Sample
 
A Taste of TDD: The basics of TDD, why it is hard and how to do it better
A Taste of TDD: The basics of TDD, why it is hard and how to do it betterA Taste of TDD: The basics of TDD, why it is hard and how to do it better
A Taste of TDD: The basics of TDD, why it is hard and how to do it better
 
Becoming a more Productive Rails Developer
Becoming a more Productive Rails DeveloperBecoming a more Productive Rails Developer
Becoming a more Productive Rails Developer
 
LeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than developmentLeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than development
 
Becoming a more productive Rails Developer
Becoming a more productive Rails DeveloperBecoming a more productive Rails Developer
Becoming a more productive Rails Developer
 
Cloud tools
Cloud toolsCloud tools
Cloud tools
 
Windycityrails page performance
Windycityrails page performanceWindycityrails page performance
Windycityrails page performance
 
Irb Tips and Tricks
Irb Tips and TricksIrb Tips and Tricks
Irb Tips and Tricks
 
Freelancing and side-projects on Rails
Freelancing and side-projects on RailsFreelancing and side-projects on Rails
Freelancing and side-projects on Rails
 
Cvcc performance tuning
Cvcc performance tuningCvcc performance tuning
Cvcc performance tuning
 
Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...
Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...
Ruby on Rails Performance Tuning. Make it faster, make it better (WindyCityRa...
 
Windy cityrails performance_tuning
Windy cityrails performance_tuningWindy cityrails performance_tuning
Windy cityrails performance_tuning
 
improving the performance of Rails web Applications
improving the performance of Rails web Applicationsimproving the performance of Rails web Applications
improving the performance of Rails web Applications
 

Recently uploaded

HTTP Adaptive Streaming – Quo Vadis (2024)
HTTP Adaptive Streaming – Quo Vadis (2024)HTTP Adaptive Streaming – Quo Vadis (2024)
HTTP Adaptive Streaming – Quo Vadis (2024)
Alpen-Adria-Universität
 
Supercomputing from the Desktop Workstation
Supercomputingfrom the Desktop WorkstationSupercomputingfrom the Desktop Workstation
Supercomputing from the Desktop Workstation
Larry Smarr
 
GDG Cloud Southlake #34: Neatsun Ziv: Automating Appsec
GDG Cloud Southlake #34: Neatsun Ziv: Automating AppsecGDG Cloud Southlake #34: Neatsun Ziv: Automating Appsec
GDG Cloud Southlake #34: Neatsun Ziv: Automating Appsec
James Anderson
 
Leveraging AI for Software Developer Productivity.pptx
Leveraging AI for Software Developer Productivity.pptxLeveraging AI for Software Developer Productivity.pptx
Leveraging AI for Software Developer Productivity.pptx
petabridge
 
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdfPigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions
 
Artificial Intelligence and Its Different Domains.pptx
Artificial Intelligence and Its Different Domains.pptxArtificial Intelligence and Its Different Domains.pptx
Artificial Intelligence and Its Different Domains.pptx
officialnavya2010
 
New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024
ThousandEyes
 
Building an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and MilvusBuilding an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and Milvus
Zilliz
 
Chapter 5 - Managing Test Activities V4.0
Chapter 5 - Managing Test Activities V4.0Chapter 5 - Managing Test Activities V4.0
Chapter 5 - Managing Test Activities V4.0
Neeraj Kumar Singh
 
9 Ways Pastors Will Use AI Everyday By 2029
9 Ways Pastors Will Use AI Everyday By 20299 Ways Pastors Will Use AI Everyday By 2029
9 Ways Pastors Will Use AI Everyday By 2029
Big Click Syndicate LLC
 
Summer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdf
Summer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdfSummer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdf
Summer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdf
Anna Loughnan Colquhoun
 
Research Directions for Cross Reality Interfaces
Research Directions for Cross Reality InterfacesResearch Directions for Cross Reality Interfaces
Research Directions for Cross Reality Interfaces
Mark Billinghurst
 
intra-mart Accel series 2024 Spring updates_En
intra-mart Accel series 2024 Spring updates_Enintra-mart Accel series 2024 Spring updates_En
intra-mart Accel series 2024 Spring updates_En
NTTDATA INTRAMART
 
Blockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre timesBlockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre times
anupriti
 
Cookies program to display the information though cookie creation
Cookies program to display the information though cookie creationCookies program to display the information though cookie creation
Cookies program to display the information though cookie creation
shanthidl1
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
Aurora Consulting
 
Artificial Intelligence (AI), Robotics and Computational fluid dynamics
Artificial Intelligence (AI), Robotics and Computational fluid dynamicsArtificial Intelligence (AI), Robotics and Computational fluid dynamics
Artificial Intelligence (AI), Robotics and Computational fluid dynamics
Chintan Kalsariya
 
Knowledge and Prompt Engineering Part 2 Focus on Prompt Design Approaches
Knowledge and Prompt Engineering Part 2 Focus on Prompt Design ApproachesKnowledge and Prompt Engineering Part 2 Focus on Prompt Design Approaches
Knowledge and Prompt Engineering Part 2 Focus on Prompt Design Approaches
Earley Information Science
 
Database Management Myths for Developers
Database Management Myths for DevelopersDatabase Management Myths for Developers
Database Management Myths for Developers
John Sterrett
 
What’s New in Teams Calling, Meetings and Devices May 2024
What’s New in Teams Calling, Meetings and Devices May 2024What’s New in Teams Calling, Meetings and Devices May 2024
What’s New in Teams Calling, Meetings and Devices May 2024
Stephanie Beckett
 

Recently uploaded (20)

HTTP Adaptive Streaming – Quo Vadis (2024)
HTTP Adaptive Streaming – Quo Vadis (2024)HTTP Adaptive Streaming – Quo Vadis (2024)
HTTP Adaptive Streaming – Quo Vadis (2024)
 
Supercomputing from the Desktop Workstation
Supercomputingfrom the Desktop WorkstationSupercomputingfrom the Desktop Workstation
Supercomputing from the Desktop Workstation
 
GDG Cloud Southlake #34: Neatsun Ziv: Automating Appsec
GDG Cloud Southlake #34: Neatsun Ziv: Automating AppsecGDG Cloud Southlake #34: Neatsun Ziv: Automating Appsec
GDG Cloud Southlake #34: Neatsun Ziv: Automating Appsec
 
Leveraging AI for Software Developer Productivity.pptx
Leveraging AI for Software Developer Productivity.pptxLeveraging AI for Software Developer Productivity.pptx
Leveraging AI for Software Developer Productivity.pptx
 
Pigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdfPigging Solutions Sustainability brochure.pdf
Pigging Solutions Sustainability brochure.pdf
 
Artificial Intelligence and Its Different Domains.pptx
Artificial Intelligence and Its Different Domains.pptxArtificial Intelligence and Its Different Domains.pptx
Artificial Intelligence and Its Different Domains.pptx
 
New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024New ThousandEyes Product Features and Release Highlights: June 2024
New ThousandEyes Product Features and Release Highlights: June 2024
 
Building an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and MilvusBuilding an Agentic RAG locally with Ollama and Milvus
Building an Agentic RAG locally with Ollama and Milvus
 
Chapter 5 - Managing Test Activities V4.0
Chapter 5 - Managing Test Activities V4.0Chapter 5 - Managing Test Activities V4.0
Chapter 5 - Managing Test Activities V4.0
 
9 Ways Pastors Will Use AI Everyday By 2029
9 Ways Pastors Will Use AI Everyday By 20299 Ways Pastors Will Use AI Everyday By 2029
9 Ways Pastors Will Use AI Everyday By 2029
 
Summer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdf
Summer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdfSummer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdf
Summer24-ReleaseOverviewDeck - Stephen Stanley 27 June 2024.pdf
 
Research Directions for Cross Reality Interfaces
Research Directions for Cross Reality InterfacesResearch Directions for Cross Reality Interfaces
Research Directions for Cross Reality Interfaces
 
intra-mart Accel series 2024 Spring updates_En
intra-mart Accel series 2024 Spring updates_Enintra-mart Accel series 2024 Spring updates_En
intra-mart Accel series 2024 Spring updates_En
 
Blockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre timesBlockchain and Cyber Defense Strategies in new genre times
Blockchain and Cyber Defense Strategies in new genre times
 
Cookies program to display the information though cookie creation
Cookies program to display the information though cookie creationCookies program to display the information though cookie creation
Cookies program to display the information though cookie creation
 
Quality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of TimeQuality Patents: Patents That Stand the Test of Time
Quality Patents: Patents That Stand the Test of Time
 
Artificial Intelligence (AI), Robotics and Computational fluid dynamics
Artificial Intelligence (AI), Robotics and Computational fluid dynamicsArtificial Intelligence (AI), Robotics and Computational fluid dynamics
Artificial Intelligence (AI), Robotics and Computational fluid dynamics
 
Knowledge and Prompt Engineering Part 2 Focus on Prompt Design Approaches
Knowledge and Prompt Engineering Part 2 Focus on Prompt Design ApproachesKnowledge and Prompt Engineering Part 2 Focus on Prompt Design Approaches
Knowledge and Prompt Engineering Part 2 Focus on Prompt Design Approaches
 
Database Management Myths for Developers
Database Management Myths for DevelopersDatabase Management Myths for Developers
Database Management Myths for Developers
 
What’s New in Teams Calling, Meetings and Devices May 2024
What’s New in Teams Calling, Meetings and Devices May 2024What’s New in Teams Calling, Meetings and Devices May 2024
What’s New in Teams Calling, Meetings and Devices May 2024
 

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

  • 1. Rails PDF Generation John McCaffrey
  • 2. What we’re going to cover Agenda PDF Types and Libraries Prawn General PDF Testing Advanced Prawn examples Prawn-to Q&A
  • 4. John McCaffrey Presented at WindyCityRails 08 Using Prawn since 10/08 http://www.pathf.com/blogs/author/john-mccaffrey /
  • 5. And you are? :include => :first_name Col 1 Col 2 Col 3 Col 4
  • 6. Why users Love PDFs They are great for: Reports Static data Forms Invoices Tightly controlled formatting Print friendly Portable Looks the same for everyone
  • 7. PDF types and libraries
  • 8. PDF Library types HTML to PDF PDF Template binding Dynamic
  • 9. 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?)
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 20. 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
  • 21. Examples Methods PDF Text Bounding_box Font Move_down Mask Page Image Stroke
  • 22. Examples Basic syntax examples Text Images Overflow Orientation
  • 23. Text & Image Text_image_sample
  • 26. Examples Intermediate Table Utf-8 Drawing Bounding box Install Gem install prawn-layout
  • 30. 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
  • 32. 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
  • 33. 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
  • 37. 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)
  • 41. Advanced examples What else can we do? Grid based layout Labels & Calendars Annotations & Links Google charts
  • 43. Grids & Labels Labels sample
  • 44. Google Charts api Charts sample
  • 45. Prawn-Format Nice and easy, just got nicer, and easier
  • 46. Prawn-format Use HTML and style syntax Basic tags Alter existing styles Create new styles Links
  • 47. Prawn-format: html and style Format
  • 48. Putting it all together Mash up Prawn, prawn-layout Prawn-to Prawn-format Google charts api Twitter- search Heroku & git Twitter_Timelines
  • 49. More to come Roadmap & projects Security & encryption ( prawn-security) Prawn-js Clean up Improved grid/table support Improved docs and examples
  • 51. 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

Editor's Notes

  1. 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
  2. Ask them to turn in and introduce them selves to at least one new person Bring up the ‘Used it’, ‘Still Using it’ papers
  3. 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
  4. 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
  5. 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)
  6. Show example of kocg form, bluebook blueprint, Mention that the library may be os specific which could affect the deployment process
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. Prawn seemed to be the only one that had good test coverage
  14. 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
  15. 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.
  16. Show a screen shot of the tests passing
  17. 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?)
  18. 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
  19. Also mention image comparisons like bluebook
  20. Also mention image comparisons like bluebook
  21. http://cracklabs.com/prawnto/
  22. Using prawn-to can help you stick to a pure MVC pattern, and not jumbling too much logic into the pdf creation
  23. When it becomes easy to make pdfs, you can start to see creative things you never would have before
  24. 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
  25. 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
  26. 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
  27. Prawn-js for scripting within the pdf http://github.com/yob/prawn-js/tree/master http:// wiki.github.com /sandal/prawn/development-roadmap