Transcript
Page 1: Dynamic PDFs with Zend Framework and Java Bridge

Copyright © 2007, Zend Technologies Inc.

Generating Dynamic PDFs Using Zend Framework and Zend Java BridgeBy Eric Ritchie

Page 2: Dynamic PDFs with Zend Framework and Java Bridge

• Senior Technical Consultant and Trainerat Zend Technologies

• Zend Framework and PHP 5 ZCE

• Fifteen years of system administration experience

• Ten years of PHP (3,4 & 5) and four years Zend Framework development experience

• Hobbies: Sampling good wines/whiskies (gifts welcome)

Eric Ritchie bids you welcome!

Page 3: Dynamic PDFs with Zend Framework and Java Bridge

What do we want to achieve?

• Template based PDF generation

• Assumption: You know a little about Zend Framework. If this is a bad assumption raise your hand

| Feb 3, 2010

| 3

Page 4: Dynamic PDFs with Zend Framework and Java Bridge

Other (historic?) methods of PDF generation

• Many options… PDFlib (PECL component) Zend_Pdf (Zend Framework component) HTML2PDF Others…

• … but somehow not quite right Either complex to use Or not flexible enough

| Feb 3, 2010

| 4

Page 5: Dynamic PDFs with Zend Framework and Java Bridge

A better way: Apache FOP

• Many advantages FO: Printing industry standard HTML like syntax Very powerful Details of PDF generation are hidden from the user.

• The catch? FO format is a bit touchy: The price of power The J word: Java Work in progress: Not feature complete (yet)

| Feb 3, 2010

| 5

Page 6: Dynamic PDFs with Zend Framework and Java Bridge

A better way: Apache FOP

• What catch? There is no catch! Use a template engine Use Zend Java Bridge

• How do we put this together…?

| Feb 3, 2010

| 6

Page 7: Dynamic PDFs with Zend Framework and Java Bridge

Zend Server

| Feb 3, 2010

| 7

• Complete, well-tested PHP stack – PHP, Zend Framework, debugging extension, Java Bridge and more

• A free community edition and a commercial edition – both are production-ready

• Application monitoring and diagnostics (integrated with Zend Studio)

• Multi-level performance enhancement capabilities

• Software updates and security hot fixes

• Easy and quick installation

Page 8: Dynamic PDFs with Zend Framework and Java Bridge

Why Java Bridge?

• Easy to use

• Transparently integrates Java objects into PHP projects

• High performance

• Free!

| Feb 3, 2010

| 8

Page 9: Dynamic PDFs with Zend Framework and Java Bridge

Why Zend Framework?

• Popular implementation of MVC

• Lightweight templating engine

• Easily extendable

• Free!

| Feb 3, 2010

| 9

Page 10: Dynamic PDFs with Zend Framework and Java Bridge

Zend_View: The V in MVC

• Zend_View is a component class of Zend Framework for working with the “View” portion of the MVC design pattern

• It provides a system of helpers, output filters, and variable escaping

• Zend_View is not tied to one template system - it can utilize PHP templates or other systems, manipulated through the view script

| Feb 3, 2010

| 10

Page 11: Dynamic PDFs with Zend Framework and Java Bridge

Zend_View: The V in MVC

• But Zend_View works with HTML, right? Yes, by default, but we can bend it to our will! Time to create a class:

| Feb 3, 2010

| 11

Page 12: Dynamic PDFs with Zend Framework and Java Bridge

Zend_View_FO2PDF: Continued

| Feb 3, 2010

| 12

Page 13: Dynamic PDFs with Zend Framework and Java Bridge

Zend_View_FO2PDF: Continued

| Feb 3, 2010

| 13

Page 14: Dynamic PDFs with Zend Framework and Java Bridge

Zend_View_FO2PDF: Java Bridge call

• Could not be easier… Create an instance of our Java class in PHP Then use it by calling class methods Only two lines of code!

| Feb 3, 2010

| 14

Page 15: Dynamic PDFs with Zend Framework and Java Bridge

The dark side: Our Java code

| Feb 3, 2010

| 15

Page 16: Dynamic PDFs with Zend Framework and Java Bridge

Defining our class path

• Who said “hack”? Need to edit /usr/local/zend/etc/watchdog.jb

| Feb 3, 2010

| 16

Page 17: Dynamic PDFs with Zend Framework and Java Bridge

Feed the view class: Write a template

• We need to write some FO (nothing offensive)• All features of ZF available to us• …but we must omit something!• The XML header

<?xml causes a conflict! So we add it in the view class (did you notice?)

| Feb 3, 2010

| 17

Page 18: Dynamic PDFs with Zend Framework and Java Bridge

Putting it together

• We need a controller…

| Feb 3, 2010

| 18

Page 19: Dynamic PDFs with Zend Framework and Java Bridge

…and done!

| Feb 3, 2010

| 19

Page 20: Dynamic PDFs with Zend Framework and Java Bridge

What next?

• The sky’s the limit Add tables Add images Add vector graphics (SVG)

| Feb 3, 2010

| 20

Page 21: Dynamic PDFs with Zend Framework and Java Bridge

What next?

• But I don’t like PDFs what about me? You can render Excel/Word documents too …or anything text based really Use your imagination!

| Feb 3, 2010

| 21

Page 22: Dynamic PDFs with Zend Framework and Java Bridge

Questions?

| Feb 3, 2010

| 22

Page 23: Dynamic PDFs with Zend Framework and Java Bridge

| Feb 3, 2010

| 23

• Thanks for listening!


Top Related