dynamic transformations from xml to pdf documents

60
Transforming XML to PDF Dynamic transformations from XML to PDF-Documents with use of LaTeX International PHP Conference 2003 Spring Edition May 9 th 2003, Amsterdam Stephan Schmidt

Upload: sumit-gharat

Post on 04-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 1/60

Transforming XML to PDF

Dynamic transformations fromXML to PDF-Documents with use

of LaTeX

International PHP Conference 2003Spring Edition

May 9th

2003, Amsterdam

Stephan Schmidt

Page 2: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 2/60

16.09.2005 Slide 2

Transforming XML to PDF

Agenda

• About the speakers

• Types of documents

• Transforming XML documents

• Introduction to LaTeX

• Basic usage of LaTeX

• Converting LaTeX to PDF• Dynamic creation of LaTeX and PDF documents

• Transforming XML documents

• Using patXMLRenderer to transform XML to PDF

Page 3: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 3/60

16.09.2005 Slide 3

Transforming XML to PDF

Stephan Schmidt

• Web Application Developer at Metrix Internet Design GmbH inKarlsruhe/Germany

• Programming since 1988, PHP since 1998

• Publishing OS on http://www.php-tools.net

• Contributor to the German PHP Magazine

• Regular speaker at conferences

• Maintainer of patXMLRenderer, patTemplate, patUser andothers

Page 4: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 4/60

16.09.2005 Slide 4

Transforming XML to PDF

The problem

• Have been developing a really large application

• Writing technical as well as end-user documentation

• Documentation was available in XML (made available inthe application as HTML)

• customers wanted documentation on paper

T f i XML PDF

Page 5: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 5/6016.09.2005 Slide 5

Transforming XML to PDF

XML documents

• Readable by humans:» self-explaining tag names» self-explaining attribute names» structured by indentation

• Readable by machines:

» Well-formed document

» only ASCII data» Validation with DTD or schema

• Describe only the content

T f i XML t PDF

Page 6: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 6/6016.09.2005 Slide 6

Transforming XML to PDF

PDF documents

• Readable by humans:

» nice layout

» can be view on any platform

» can be easily printed

• Not readable by machines

» Binary document» Mixture of content and layout

• Describe the content and layout

T f i XML t PDF

Page 7: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 7/6016.09.2005 Slide 7

Transforming XML to PDF

Getting the best of both

• Use XML documents for online documentation

• Use PDF for printed documentation

Disadvantage:» two documents to maintain

Solution:

» create two documents from one source

Transforming XML to PDF

Page 8: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 8/6016.09.2005 Slide 8

Transforming XML to PDF

Transforming XML

• Data is stored in an XML document

• Needed in different formats and environments

» Other XML formats (DocBook, SVG, …)» HTML

» Plain text

» LaTeX

» Anything else you can imagine

• Content remains the same

Transforming XML to PDF

Page 9: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 9/6016.09.2005 Slide 9

Transforming XML to PDF

Transforming XML to HTML

Source document:

<example title= „My Example“><greeting>

Hello <imp>Clark Kent</imp>!<greeting>

</example>

Result of transformation to HTML:

<html>

<head><title>My Example</title>

</head><body>

<h1>Hello <b>Clark Kent</b></h1></body>

</html>

Transforming XML to PDF

Page 10: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 10/6016.09.2005 Slide 10

Transforming XML to PDF

Transforming XML to PDF

• XML may only be transformed to ASCII documents• PDF documents are binary files

Problem:no direct transformation

Solution:

Step 1 » Transform XML to LaTeXStep 2 » Transform LaTeX to PDF

Transforming XML to PDF

Page 11: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 11/6016.09.2005 Slide 11

Transforming XML to PDF

Introduction to LaTeX

• based on TeX by Donald E. Knuth

• not a word-processor

• document preparation system for high-quality type-setting

• used for medium to large scientific documents

• can be used for any document: articles, books, letters,invoices, …

Transforming XML to PDF

Page 12: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 12/6016.09.2005 Slide 12

Transforming XML to PDF

Introduction to LaTeX (cont.)

• encourages you to concentrate on content instead oflayout

• similar concept to XML, but not based on tags

• has to be "compiled" to view or print the result

• generates layout for your documents

Transforming XML to PDF

Page 13: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 13/6016.09.2005 Slide 13

Transforming XML to PDF

Introduction to LaTeX (cont.)

• no WYSIWYG interface• can be edited with your favourite editor (vi, emacs, HomeSite

or even notepad, but not Frontpage)

» can be created by any application or script that is able tocreate ASCII files.

<?PHP

$fp = fopen( "file.txt", "w" );fputs( $fp, "Hello Clark Kent!" );fclose( $fp );

?>

Transforming XML to PDF

Page 14: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 14/6016.09.2005 Slide 14

Transforming XML to PDF

Introduction to LaTeX (example)

\documentclass{article}\title{Dynamic transformations of XML to PDF with LaTex}

\author{Stephan Schmidt}

\date{April 2003}

\begin{document}

\maketitle

We love XML, but everyone wants PDF.

\end{document}

Transforming XML to PDF

Page 15: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 15/60

16.09.2005 Slide 15

Transforming XML to PDF

Easy to understand

\documentclass{article}» the document is an article

\title{Dynamic transformations of XML to PDF with LaTex}» the title is "Dynamic transformations …"

\author{Stephan Schmidt}

» Stephan Schmidt is the author

\date{April 2003}» it has been written in April 2003

Transforming XML to PDF

Page 16: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 16/60

16.09.2005 Slide 16

g

Easy to understand

\begin{document}\maketitle

We love XML, but everyone wants PDF.

\end{document}

» document consists of a title (somehow generated) andsome text.

Transforming XML to PDF

Page 17: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 17/60

16.09.2005 Slide 17

g

LaTeX features

• Typesetting articles, technical reports, letters, books andslide presentations

• Control over large (and I really mean large) documents

• Control over sectioning, cross references, footnote,tables and figures

• Automatic creation of bibliographies and indexes

• Inclusion of images

• Using PostScript or Metafont fonts

Transforming XML to PDF

Page 18: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 18/60

16.09.2005 Slide 18

g

Basic usage of LaTeX

LaTeX documents consist of:

• commands» text markup, paper definitions, etc.

• macros» collection of commands

• environments

» split the document into logical components

• plain text

• comments

Transforming XML to PDF

Page 19: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 19/60

16.09.2005 Slide 19

g

LaTeX commands

• start with a backslash ("\ ")

• parameters enclosed in curly braces ("{" and "}")

• optional parameters enclosed in brackets ("[" and "]")

and separated by commas

Example:

\maketitle

\footnote{I am a footnote}

\documentclass[a4paper,twoside]{book}

Transforming XML to PDF

Page 20: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 20/60

16.09.2005 Slide 20

LaTeX comments

• start with percent sign ("%")

• end at the end of the line

Example:

\documentclass{article} % This will be an article

% This line is a comment and will be ignored later

Transforming XML to PDF

Page 21: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 21/60

16.09.2005 Slide 21

LaTeX environments

• used to split the document into logical parts

• similar to tags in an XML document

• start with "\begin" command and end with "\end"

command

Example:

\begin{document}

% Place anything that is part of the document here

\end{document}

Transforming XML to PDF

Page 22: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 22/60

16.09.2005 Slide 22

LaTeX special chars

• Some specialchars like "$", "{", "}", " _ ", etc. have to bequoted by adding a preceding backslash.

• "\\ " marks the end of a paragraph

• "~" is similar to HTML's &nbsp;

• "\dots" will display "…"

Transforming XML to PDF

Page 23: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 23/60

16.09.2005 Slide 23

Creating a document

• document always starts with "\documentclass"command to define the type of document

• responsible for the available commandset(no use for "\chapter" when you are writing a letter)

• used to define the basic layout style

• load packages after this command

Transforming XML to PDF

Page 24: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 24/60

16.09.2005 Slide 24

Creating a document (cont.)

• include meta information ("\author", "\date", etc.) afterthe "\documentclass" command

• "\begin{document}" marks the start of the actualdocument (like <body> in HTML)

• Inside "document" environments any LaTeX command

that structures the document may be used.

Transforming XML to PDF

Page 25: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 25/60

16.09.2005 Slide 25

Creating a document (cont.)\documentclass[a4paper,twocolumn]{article}\usepackage{hyperref }

\title{Me and the myDocument}\author{Me, of course}

\date{\today}

\begin{document}\maketitle\tableofcontents

\section{My relationship to Superman}\subsection{How it started}When I was twelve, Superman was my greatest hero.\subsection{Our relationship grew stronger}I first met him in person at the age of 16.\subsection{Everything has to end}

When he died at the hands of {\em Doomsday}, I was really sad and devoted my life to Batman.\section{My relationship to Batman}My relationship to Batman started last week so there’s not much to tell, yet.But I already know some of his friends:\begin{itemize}

\item{\em Robin}, the Boy Wonder

\item{\em Oracle}, the former Batgirl\end{itemize}\end{document}

Transforming XML to PDF

Page 26: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 26/60

16.09.2005 Slide 26

Common LaTeX commands

• \section, \subsection and \subsubsection to structure thedocument

• \em to emphasize parts of the document

• \item to create lists

• \footnote (for footnotes, of course)• \label, \bibitem, \ref and \href to create cross-references

• \includegraphics to include images

• \begin{table}, \begin{itemize} to create commonly used

environments• \tableofcontents, \listoftables and \listoffigures to createindexes

• ... and many more

Transforming XML to PDF

Page 27: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 27/60

16.09.2005 Slide 27

Converting LaTeX to PDF

1. LaTeX needs to be installed on your system(Don't panic, installing LaTeX is mere child's play)

2. "latex myDocument.tex" creates "myDocument.dvi"(dvi means Device Independent, can be converted topostscript, PDF or printer-native formats)

3. "xdvi myDocument.dvi" displays result

4. "dvipdf myDocument.dvi" creates "myDocument.pdf "

Transforming XML to PDF

Page 28: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 28/60

16.09.2005 Slide 28

Converting LaTeX to PDF

If PDF is the only destination format, use

pdflatex myDocument.tex

to generate a PDF file directly from your LaTeX source files.

Advantages:

» faster

» better support for fonts

Transforming XML to PDF

Page 29: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 29/60

16.09.2005 Slide 29

Resulting document:

Bookmark table:

Transforming XML to PDF

Page 30: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 30/60

16.09.2005 Slide 30

Resulting files

After "pdflatex" has been called, several files are availablein the folder:

• myDocument.pdf is the PDF file you wanted to create

• myDocument.log is a log file containing all log messages

• myDocument.toc contains the table of contents

• myDocument.out contains bookmarks for the PDF reader

• myDocument.aux contains all data needed for crossreferences

Transforming XML to PDF

Page 31: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 31/60

16.09.2005 Slide 31

Two-pass transformations

• LaTeX parses file from top-down

• generates table of contents, anchor files for links, PDFbookmarks and stores them in external files

• This information often has to be included at thebeginning of the document (e.g. table of contents)

• Latex file has to be parsed twice

» two-pass transformation

» pdflatex has to be called twice

Transforming XML to PDF

Page 32: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 32/60

16.09.2005 Slide 32

Dynamic creation of LaTeX documents

LaTeX documents are plain text (like HTML)» PHP can be embedded and any data inserted by using"echo"

\documentclass{article}\begin{document}

Hi, my name is <?PHP echo $_GET[ “name” ]; ?>.

\end{document}

Now open http://localhost/latex.php?name=Aquaman and

save the result

» Your first dynamic LaTeX document!

Transforming XML to PDF

Page 33: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 33/60

16.09.2005 Slide 33

Dynamic creation of LaTeX documents

But: No real automation :-(as a delevoper needs to sit next to your webserver tohandle all request » expensive!

Better:

Step 1 » Capture result with output control functions

Step 2 » Save result with file system functions

Step 3 » Transform file using system commands

Transforming XML to PDF

Page 34: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 34/60

16.09.2005 Slide 34

dynamicLatex.php

<?PHPob_start();

?>

\documentclass{article}\begin{document}

Hi, my name is <?PHP echo $_GET[“name”]; ?>.\end{document}

<?PHP$latex = ob_get_contents();ob_end_clean();$fp = fopen( "dynamic.tex", "w" );

fputs( $fp, $latex );fclose( $fp );system( "pdflatex dynamic.tex" );system( "shutdown -h now" );

?>

Transforming XML to PDF

Page 35: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 35/60

16.09.2005 Slide 35

Not state-of-the-art

Creating larger and complex files can get messy:

• PHP and LaTeX commands in one file

• No separation of logic, content and layout

» you are a bad programmer!

(shame on you!)

Transforming XML to PDF

Page 36: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 36/60

16.09.2005 Slide 36

State-of-the-art techniques

Implement the same techniques that are used indynamic webpages:

• use templates• store content in databases or XML

• use caching to gain performance

Transforming XML to PDF

Page 37: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 37/60

16.09.2005 Slide 37

Transforming XML

• XSLT has been developed for the task of transforming XML

documents• XSLT stylesheets are XML documents

• Transforms XML trees that are stored in memory

• Uses XPath to access parts of a document

• Based on pattern matching(“When see you something that looks like this, do that…”)

• Functional syntax

Sounds good?

» think again!

Transforming XML to PDF

Page 38: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 38/60

16.09.2005 Slide 38

Drawbacks of XSLT

XSLT is domain specific:

• Developed to work with XML

• Creating plain text/LaTeX is quite hard, as whitespace isimportant (<xslt:text>)

• Transforming “world” to “W O R L D” is next toimpossible

Transforming XML to PDF

Page 39: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 39/60

16.09.2005 Slide 39

Drawbacks of XSLT

XSLT is verbose and circumstantial:

<xsl:choose><xsl:when test="@author">

<xsl:value-of select="@author" /><xsl:text> says: </xsl:text><xsl:value-of select="." />

</xsl:when><xsl:otherwise>

<xsl:text>Somebody says: </xsl:text><xsl:value-of select="." />

</xsl:otherwise></xsl:choose>

Transforming XML to PDF

Page 40: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 40/60

16.09.2005 Slide 40

Drawbacks of XSLT

XSLT is hard to learn:

• Functional programming language

• Complex structure (see “if/else” example”)

• XPath is needed

• Designer needs to learn it

Transforming XML to PDF

Page 41: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 41/60

16.09.2005 Slide 41

Transforming XML using PHP

Transforming an XML document is easy:

1. Define a transformation rule for each tag

2. Start at the root element

3. Traverse the document recursively

4. Insert the transformation result to the parent tag

5. Go home early as you have completed the task faster thanwith XSLT.

Transforming XML to PDF

Page 42: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 42/60

16.09.2005 Slide 42

Creating transformation rules

Rules are simple:

• “When you see this, replace it with that.” 

• Implemented in PHP using templates

• Attributes of the tag are used as template variables

• PCData of the tag is used as template variable “CONTENT” 

Transforming XML to PDF

Page 43: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 43/60

16.09.2005 Slide 43

Example

<section title="XML and PDF"><para>We love XML, but everybody wants PDF.</para>

</section>

XML

Template for <section>

<table border="0" cellpadding="0" cellspacing="2" width="500">

<tr><td><b>{TITLE}</b></td><tr>

<tr><td>{CONTENT}</td></tr>

</table>

Template for <para>

<font face="Arial" size="2">{CONTENT}<br></font>

Transforming XML to PDF

Page 44: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 44/60

16.09.2005 Slide 44

Example (Result)

<table border="0" cellpadding="0" cellspacing="2"width="500">

<tr><td><b>XML and PDF</b></td><tr>

<tr><td>

<font face="Arial" size="2">We love XML but, everybody wants PDF.<br>

</font>

</td></tr></table>

Transforming XML to PDF

Page 45: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 45/60

16.09.2005 Slide 45

Don’t reinvent the wheel

There already are XML transformers available for PHP:

• patXMLRendererhttp://www.php-tools.net

• PEAR::XML_Transformerhttp://pear.php.net

• phpTagLibhttp://chocobot.d2g.com

Transforming XML to PDF

Page 46: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 46/60

16.09.2005 Slide 46

Installation of patXMLRenderer

• Download archive at http://www.php-tools.de• Unzip the archive

• Adjust all path names and options in the config file (cache, log,etc.)

• Create the templates (transformation rules)• Create your XML files

• Let patXMLRenderer transform the files

Finished: » It’s mere child’s play «

Transforming XML to PDF

Page 47: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 47/60

16.09.2005 Slide 47

Introduction to patTemplate

• PHP templating class published under LGPL• Supports LaTeX templates when instantiated with

$tmpl = new patTemplate( "tex" );

• Placeholder for variables have to be UPPERCASE and enclosedin { and } or <{ and }> if used with LaTeX templates

• Uses <patTemplate:tmpl name="..."> tags to split files intotemplate blocks that may be addressed seperately

• Other Properties of the templates are written down asattributes, e.g: type="condition" or whitespace="trim"

• Emulation of simple switch/case and if/else statement by using<patTemplate:sub condition="..."> tags

Transforming XML to PDF

Page 48: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 48/60

16.09.2005 Slide 48

patTemplate Example

simple Template with two variables

(Corresponds to the XML tag <box>)

<patTemplate:tmpl name="box">

<table border="1" cellpadding="5" cellspacing="0" width="{WIDTH}"><tr>

<td>{CONTENT}</td>

</tr>

</table></patTemplate:tmpl>

Transforming XML to PDF

Page 49: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 49/60

16.09.2005 Slide 49

patTemplate Example 2

Task:

Box should be available in three sizes: “small”, “large” and “medium” (default)

Solution:Condition Template to emulate a switch/case statment:

• Template type is "condition"

• Variable that should be checked is called "size"

• Three possible values for "size": "small", "large" and "medium"(or any other unknown value)

» three Subtemplates.

Transforming XML to PDF

Page 50: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 50/60

16.09.2005 Slide 50

patTemplate Example 2

<patTemplate:tmpl name="box" type="condition" conditionvar="size">

<patTemplate:sub condition="small">

<table border="1" cellpadding="5" cellspacing="0" width="200">

<tr><td>{CONTENT}</td></tr>

</table>

</patTemplate:sub>

<patTemplate:sub condition="large"><table border="1" cellpadding="5" cellspacing="0" width="800">

<tr><td>{CONTENT}</td></tr>

</table>

</patTemplate:sub>

<patTemplate:sub condition="default"><table border="1" cellpadding="5" cellspacing="0" width="500">

<tr><td>{CONTENT}</td></tr>

</table>

</patTemplate:sub>

</patTemplate:tmpl>

Transforming XML to PDF

Page 51: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 51/60

16.09.2005 Slide 51

Transforming XML to LaTeX

<?xml version=”1.0” standalone=”yes”?><article title=” Me and the superheroes, part 2”>

<paragraph title=” I lied to you”>

When I was talking about <imp>Superman</imp>,lied. He came back from the dead and rose to the gloryhe once had.

</paragraph>

</article>

Transforming XML to PDF

Page 52: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 52/60

16.09.2005 Slide 52

The LaTeX template

<patTemplate:tmpl name=”article”>\documentclass[a4paper,twocolumn]{article}

\usepackage{hyperref}

\title{<{TITLE}>}\author{Me, of course}

\begin{document}

\tableofcontents<{CONTENT}>\end{document}</patTemplate:tmpl>

<patTemplate name=”paragraph”>

\section{<{TITLE}>}<{CONTENT}>\\ </patTemplate:tmpl>

<patTemplate:tmpl name=”imp” whitespace=”trim”>{\em <{CONTENT}>}

</patTemplate:tmpl>

Transforming XML to PDF

Page 53: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 53/60

16.09.2005 Slide 53

The Result

\documentclass[a4paper,twocolumn]{article}\usepackage{hyperref }

\title{Me and the superheroes, part 2}

\author{Me, of course}

\begin{document}

\tableofcontents

\section{I lied to you}When I was talking about {\em Superman}, I lied. He came back

from the dead and rose to the glory he once had.\\ 

\end{document}

Transforming XML to PDF

Page 54: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 54/60

16.09.2005 Slide 54

Creating the PDF document

What are you waiting for?

Step 1 » Save the resulting LaTeX document

Step 2 » Use system( "pdflatex myDocument.tex" ) to

create a PDF document

Step 3 » Use header( "Location: myDocument.pdf" ) to

start the download.

Transforming XML to PDF

Page 55: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 55/60

16.09.2005 Slide 55

To infinity … and beyond!

patXMLRenderer can do even more for you:

• Supports overloading of namespaces to include anydynamic content (files, databases, rss streams, etc).

• Caching mechanism

• Logging

• Administration interface

• Offline generation of plain HTML

Transforming XML to PDF

Page 56: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 56/60

16.09.2005 Slide 56

Simple Example

<example>

Today is <time:current format= “m-d-Y”  />.

</example>

Will be transformed to…

<example>

Today is 05-09-2004.

</example>

…Which will then be transformed to LaTeX using the rules

defined in the templates.

Transforming XML to PDF

Page 57: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 57/60

16.09.2005 Slide 57

patXMLRenderer Example

<page><dbc:connection name="foo">

<dbc:type>mysql</dbc:type><dbc:host>localhost</dbc:host><dbc:db>myDb</dbc:db>

<dbc:user>me</dbc:user><dbc:pass>secret</dbc:pass>

</dbc:connection>...place any XML code here...

<dbc:query connection="foo" returntype="assoc">SELECT id,name,email FROM authorsWHERE id=<var:get scope="_GET" var="uid" />

</dbc:query><page>

Transforming XML to PDF

Page 58: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 58/60

16.09.2005 Slide 58

patXMLRenderer Example (Result)

<page>...any static XML...

<result>

<row>

<id>1</id><name>Stephan</name>

<email>[email protected]</email>

</row>

</result>

</page>

Transforming XML to PDF

Page 59: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 59/60

16.09.2005 Slide 59

Existing Extensions

• Repository on http://www.php-tools.net

• Examples:» <xml:...> for XML syntax highlighting» <php:...> for PHP syntax highlighting

» <dbc:...> database interface» <var:...> access to variables» <control:...> control structures» <rss:...> to include content from RSS feeds

» <file:...> file operations» and many more...

• Allow you to develop "XML Applications"

Transforming XML to PDF

Page 60: Dynamic Transformations From XML to PDF Documents

8/13/2019 Dynamic Transformations From XML to PDF Documents

http://slidepdf.com/reader/full/dynamic-transformations-from-xml-to-pdf-documents 60/60

16.09.2005 Slide 60

The End

Thank you!

More information:

• http://www.php-tools.net• [email protected]

Thanks to:Sebastian Mordziol, gERD Schaufelberger, Metrix Internet

Design GmbH