cvs: pear /XML_fo2pdf README.fo2pdf fo2pdf.php package.xml simple.fo
| From: | Christian Stocker | Date: | Thu, 13 Sep 2001 15:53:49 +0000 |
| Subject: | cvs: pear /XML_fo2pdf README.fo2pdf fo2pdf.php package.xml simple.fo | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-843@lists.php.net to get a copy of this message | ||
chregu Thu Sep 13 11:53:49 2001 EDT
Added files:
/pear/XML_fo2pdf README.fo2pdf simple.fo
Modified files:
/pear/XML_fo2pdf fo2pdf.php package.xml
Log:
- runs (only?) with fop 0.20
- call of java objects is completely rewritten
- $fop = new XML_fo2pdf("input.fo") should not be used anymore, use
$fop->run("input.fo") instead
- can output not only pdf, but also ps/pcl/txt and whatever fop supports
Index: pear/XML_fo2pdf/fo2pdf.php diff -u pear/XML_fo2pdf/fo2pdf.php:1.3 pear/XML_fo2pdf/fo2pdf.php:1.4 --- pear/XML_fo2pdf/fo2pdf.php:1.3 Tue Aug 14 09:41:37 2001 +++ pear/XML_fo2pdf/fo2pdf.php Thu Sep 13 11:53:48 2001 @@ -15,14 +15,14 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: fo2pdf.php,v 1.3 2001/08/14 13:41:37 alexmerz Exp $ +// $Id: fo2pdf.php,v 1.4 2001/09/13 15:53:48 chregu Exp $ /** * fo to pdf converter. * * with fo (formating objects) it's quite easy to convert xml-documents into -* pdf-docs. +* pdf-docs (and not only pdf, but also ps, pcl, txt and more) * * An introduction into formating objects can be found at * http://www.w3.org/TR/xsl/slice6.html#fo-section @@ -45,28 +45,30 @@ * * Requirements: * -* You need Fop from the xml-apache project (http://xml.apache.org/fop) and -* Java (1.1.x or later, i tested it with 1.2.2 from sun on linux, see the -* Fop-Docs for details). +* You need Fop (version 0.20.1 was used for this) from the xml-apache +* project (http://xml.apache.org/fop) and Java (1.1.x or later, i tested +* it with 1.2.2 from sun on linux, see the Fop-Docs for details). * Furthermore you have to compile your php with --with-java and to adjust * your php.ini file. It can be a rather painful task to get java and php -* to work together. +* to work together. (i also tested this only with jdk 1.2.2, i couldn't 1.3.1 +* get to work) * See http://www.phpbuilder.com/columns/marknold20001221.php3 or * http://www.linuxwebdevnews.com/articles/php-java-xslt.php?pid=347 * for more details about java and php or ask me, if you're stuck * (especially with linux. windows is not my area..) * -* Todo: -* - Errordetection -* - Use embedding instead of org.apache.fop.apps.CommandLine- -* this way, we maybe do not have to write temp-files and -* we can render other stuff than only pdf (txt,pcl,ps,...) -* see http://xml.apache.org/fop/embedding.html for details -* * Usage: * require_once("XML/fo2pdf.php"); * //make a pdf from simple.fo and save the pdf in a tmp-folder -* $fop = new xml_fo2pdf("simple.fo"); +* $fop = new xml_fo2pdf(); +* // the following 2 lines are the default settins, so not +* // necessary here, but you can set it to other values +* $fop->setRenderer("pdf"); +* $fop->setContentType("application/pdf"); +* if (PEAR::isError($error = $fop->run("simple.fo"))) +* { +* die("FOP ERROR: ". $error->getMessage()); +* } * //print pdf to the outputbuffer, * // including correct Header ("Content-type: application/pdf") * $fop->printPDF(); @@ -85,17 +87,19 @@ * print $content; * die(); * } -* $fop = new xml_fo2pdf("simple.fo"); +* $fop = new xml_fo2pdf(); +* $fop->run("simple.fo"); * $fop->printPDF(); * $fop->deletePDF(); * print $cache->end("+30"); * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: fo2pdf.php,v 1.3 2001/08/14 13:41:37 alexmerz Exp $ +* @version $Id: fo2pdf.php,v 1.4 2001/09/13 15:53:48 chregu Exp $ * @package XML */ +require_once( "PEAR.php") ; -class XML_fo2pdf { +class XML_fo2pdf extends pear { /** * fo-file used in this class @@ -125,23 +129,44 @@ */ var $tmppdfprefix = "pdffo"; - + /** + * the render Type. At the moment (fop 0.20.1), possible values are + * - awt + * - mif + * - pcl + * - pdf + * - ps + * - txt + * - xml + * + * @var string + */ + var $renderer = "pdf"; + + /** + * the content-type to be sent if printPDF is called. + * + * @var contenttype + " @see printPDF() + */ + var $contenttype = "application/pdf"; + /** * constructor - * One can pass an input fo-file already here (the other possibility - * is with the run or runFromString method). - * and if the pdf should be stored permanently, a filename/path for - * that can also be passed here. + * ATTENTION (you've been warned!): + * You should not pass the values here, 'cause then you don't have + * Error-Reporting. This variables are only here for Backwards Compatibilty.. + * Use $fop->run("input.fo","output.pdf") instead. * - * @param string $fo file input fo-file - * @param string $pdf file output pdf-file + * @param string $fo file input fo-file (do not use it anymore) + * @param string $pdf file output pdf-file (do not use it anymore) * @see run(), runFromString(), runFromFile() * @access public */ - function xml_fo2pdf($fo = "", $pdf = "") + function xml_fo2pdf ($fo = Null, $pdf = "") { - if ($fo) { - $this->run($fo, $pdf); + if (!(is_null($fo))) { + $this->run($fo, $pdf); } } @@ -166,12 +191,86 @@ $this->pdf = $pdf; $this->fo = $fo; - $options = array($this->fo, $this->pdf); - $java = new Java("org.apache.fop.apps.CommandLine", $options); - $java->run(); + $options = array( $this->fo,"-".$this->renderer,$this->pdf); + + /** + * according to the documentation, the following lines should be enough, + * to do, what we want. but it didn't work. Yes, it did, but it produced + * approx. 10 pdf-files for each run... + * + * $options = new Java("org.apache.fop.apps.CommandLineOptions",$options); + * $starter = $options->getStarter(); + * $starter->run(); + * + * Therefore i took the code from org/apache/fop/apps/CommandLineStarter.java + * converted it to php-code and it works now... if anyone has a better solution + * please inform me ;) + */ + java_last_exception_clear(); + $commandLineOptions= @new Java("org.apache.fop.apps.CommandLineOptions",$options); + + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + + $starter = new Java("org.apache.fop.apps.CommandLineStarter",$commandLineOptions); + + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + + $input = $commandLineOptions->getInputHandler(); + + $parser = $input->getParser(); + $starter->setParserFeatures($parser); + + $driver = new Java ("org.apache.fop.apps.Driver"); + + $driver->setBufferFile($commandLineOptions->getBufferFile()); + + $renderer = @$commandLineOptions->getRenderer(); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + @$driver->setRenderer($renderer); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + $stream = @new Java("java.io.FileOutputStream",$commandLineOptions->getOutputFile()); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + + @$driver->setOutputStream($stream); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + $renderer = $driver->getRenderer(); + $renderer->setOptions($commandLineOptions->getRendererOptions()); + + @$driver->render($parser, $input->getInputSource()); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + if ($DelFo) { $this->deleteFo($fo); } + + return True; } /** @@ -194,7 +293,7 @@ $fp = fopen($fo, "w+"); fwrite($fp, $fostring); fclose($fp); - $this->run($fo, $pdf, True); + return $this->run($fo, $pdf, True); } /** * A wrapper to run for better readabilty @@ -257,7 +356,7 @@ function printPDF($pdf = "") { $pdf = $this->returnPDF($pdf); - Header("Content-type: application/pdf\nContent-Length: " . strlen($pdf)); + Header("Content-type: ".$this->contenttype."\nContent-Length: " . strlen($pdf)); print $pdf; } @@ -280,6 +379,31 @@ fclose($fd); return $content; } + + /** + * sets the rendertype + * + * @see $this-renderer + * @access public + */ + + function setRenderer($renderer = "pdf") + { + $this->renderer = $renderer; + } + + /** + * sets the content-type + * + * @see $this-contenttype + * @access public + */ + + function setContentType($contenttype = "application/pdf") + { + $this->contenttype = $contenttype; + } + } ?> Index: pear/XML_fo2pdf/package.xml diff -u pear/XML_fo2pdf/package.xml:1.4 pear/XML_fo2pdf/package.xml:1.5 --- pear/XML_fo2pdf/package.xml:1.4 Tue Aug 28 13:23:59 2001 +++ pear/XML_fo2pdf/package.xml Thu Sep 13 11:53:48 2001 @@ -3,22 +3,27 @@ generated package.xml files --> <package> <name>XML_fo2pdf</name> - <summary>Convert a xsl-fo file to pdf with the help of apache-fop</summary> + <summary>Converts a xsl-fo file to pdf/ps/pcl/text/etc with the help of apache-fop</summary> <maintainer> <initials>cs</initials> <name>Christian Stocker</name> <email>chregu@php.net</email> </maintainer> <release> - <version>0.9</version> - <date>2001-06-21</date> + <version>0.95</version> + <date>2001-09-21</date> <notes> - Documentation is in the php-file itself. + - runs with fop 0.20 + - call of java objects is completely rewritten + - $fop = new XML_fo2pdf("input.fo") should not be used anymore, use $fop->run("input.fo") instead + - can output not only pdf, but also ps/pcl/txt and whatever fop supports </notes> </release> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">fo2pdf.php</file> + <file role="doc">README.fo2pdf</file> + <file role="doc">simple.fo</file> </dir> </filelist> </package> Index: pear/XML_fo2pdf/README.fo2pdf +++ pear/XML_fo2pdf/README.fo2pdf fo to pdf converter. with fo (formating objects) it's quite easy to convert xml-documents into pdf-docs (and not only pdf, but also ps, pcl, txt and more) An introduction into formating objects can be found at http://www.w3.org/TR/xsl/slice6.html#fo-section http://www.ibiblio.org/xml/books/bible/updates/15.html A tutorial is here: http://www.xml.com/pub/a/2001/01/17/xsl-fo/ http://www.xml.com/pub/a/2001/01/24/xsl-fo/ A html_to_fo.xsl can also be found there http://www.xml.com/2001/01/24/xsl-fo/fop_article.tgz but it didn't work for my simple xhtml files.. The way to use this class is, produce a fo-file from a xml-file with a xsl-stylesheet, then feed this class with this fo-file and you get a pdf back (either directly to the browser for really dynamic-pdf production or as a file on your filesystem) It is recommended to use the Cache-Classes from PEAR, if you want dynamic pdf production, since the process of making the pdfs takes some time. For an example of how to use Cache and fo2pdf see below. Requirements: You need Fop (version 0.20.1 was used for this) from the xml-apache project (http://xml.apache.org/fop) and Java (1.1.x or later, i tested it with 1.2.2 from sun on linux, see the Fop-Docs for details). Furthermore you have to compile your php with --with-java and to adjust your php.ini file. It can be a rather painful task to get java and php to work together. (i also tested this only with jdk 1.2.2, i couldn't 1.3.1 get to work) See http://www.phpbuilder.com/columns/marknold20001221.php3 or http://www.linuxwebdevnews.com/articles/php-java-xslt.php?pid=347 for more details about java and php or ask me, if you're stuck (especially with linux. windows is not my area..) Usage: require_once("XML/fo2pdf.php"); //make a pdf from simple.fo and save the pdf in a tmp-folder $fop = new xml_fo2pdf(); // the following 2 lines are the default settins, so not // necessary here, but you can set it to other values $fop->setRenderer("pdf"); $fop->setContentType("application/pdf"); if (PEAR::isError($error = $fop->run("simple.fo"))) { die("FOP ERROR: ". $error->getMessage()); } //print pdf to the outputbuffer, // including correct Header ("Content-type: application/pdf") $fop->printPDF(); //delete the temporary pdf file $fop->deletePDF(); With Cache: require_once("XML/fo2pdf.php"); require_once("Cache/Output.php"); $container = "file"; $options = array("cache_dir"=>"/tmp/"); $cache = new Cache_Output("$container",$options); $cache_handle = $cache->generateID($REQUEST_URI); if ($content = $cache->start($cache_handle)) { Header("Content-type: application/pdf"); print $content; die(); } $fop = new xml_fo2pdf(); $fop->run("simple.fo"); $fop->printPDF(); $fop->deletePDF(); print $cache->end("+30"); Index: pear/XML_fo2pdf/simple.fo +++ pear/XML_fo2pdf/simple.fo <?xml version="1.0" encoding="utf-8"?> <!-- example for a simple fo file. At the beginning the page layout is set. Below fo:root there is always - a single fo:layout-master-set which defines one or more page layouts - an optional fo:declarations - and a sequence of one or more fo:page-sequences containing the text and formatting instructions --> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <!-- fo:layout-master-set defines in its children the page layout: the pagination and layout specifications - page-masters: have the role of describing the intended subdivisions of a page and the geometry of these subdivisions In this case there is only a simple-page-master which defines the layout for all pages of the text --> <!-- layout information --> <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-body margin-top="3cm"/> <fo:region-before extent="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set> <!-- end: defines page layout --> <!-- start page-sequence here comes the text (contained in flow objects) the page-sequence can contain different fo:flows the attribute value of master-name refers to the page layout which is to be used to layout the text contained in this page-sequence--> <fo:page-sequence master-name="simple"> <!-- start fo:flow each flow is targeted at one (and only one) of the following: xsl-region-body (usually: normal text) xsl-region-before (usually: header) xsl-region-after (usually: footer) xsl-region-start (usually: left margin) xsl-region-end (usually: right margin) ['usually' applies here to languages with left-right and top-down writing direction like English] in this case there is only one target: xsl-region-body --> <fo:flow flow-name="xsl-region-body"> <!-- each paragraph is encapsulated in a block element the attributes of the block define font-family and size, line-heigth etc. --> <!-- this defines a title --> <fo:block font-size="18pt" font-family="sans-serif" line-height="24pt" space-after.optimum="15pt" background-color="blue" color="white" text-align="center" padding-top="3pt"> Extensible Markup Language (XML) 1.0 </fo:block> <!-- this defines normal text --> <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="3pt" text-align="justify"> The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML. </fo:block> <!-- this defines normal text --> <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="3pt" text-align="justify"> The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML. </fo:block> </fo:flow> <!-- closes the flow element--> </fo:page-sequence> <!-- closes the page-sequence --> </fo:root>
Index: pear/XML_fo2pdf/fo2pdf.php diff -u pear/XML_fo2pdf/fo2pdf.php:1.3 pear/XML_fo2pdf/fo2pdf.php:1.4 --- pear/XML_fo2pdf/fo2pdf.php:1.3 Tue Aug 14 09:41:37 2001 +++ pear/XML_fo2pdf/fo2pdf.php Thu Sep 13 11:53:48 2001 @@ -15,14 +15,14 @@ // | Authors: Christian Stocker <chregu@nomad.ch> | // +----------------------------------------------------------------------+ // -// $Id: fo2pdf.php,v 1.3 2001/08/14 13:41:37 alexmerz Exp $ +// $Id: fo2pdf.php,v 1.4 2001/09/13 15:53:48 chregu Exp $ /** * fo to pdf converter. * * with fo (formating objects) it's quite easy to convert xml-documents into -* pdf-docs. +* pdf-docs (and not only pdf, but also ps, pcl, txt and more) * * An introduction into formating objects can be found at * http://www.w3.org/TR/xsl/slice6.html#fo-section @@ -45,28 +45,30 @@ * * Requirements: * -* You need Fop from the xml-apache project (http://xml.apache.org/fop) and -* Java (1.1.x or later, i tested it with 1.2.2 from sun on linux, see the -* Fop-Docs for details). +* You need Fop (version 0.20.1 was used for this) from the xml-apache +* project (http://xml.apache.org/fop) and Java (1.1.x or later, i tested +* it with 1.2.2 from sun on linux, see the Fop-Docs for details). * Furthermore you have to compile your php with --with-java and to adjust * your php.ini file. It can be a rather painful task to get java and php -* to work together. +* to work together. (i also tested this only with jdk 1.2.2, i couldn't 1.3.1 +* get to work) * See http://www.phpbuilder.com/columns/marknold20001221.php3 or * http://www.linuxwebdevnews.com/articles/php-java-xslt.php?pid=347 * for more details about java and php or ask me, if you're stuck * (especially with linux. windows is not my area..) * -* Todo: -* - Errordetection -* - Use embedding instead of org.apache.fop.apps.CommandLine- -* this way, we maybe do not have to write temp-files and -* we can render other stuff than only pdf (txt,pcl,ps,...) -* see http://xml.apache.org/fop/embedding.html for details -* * Usage: * require_once("XML/fo2pdf.php"); * //make a pdf from simple.fo and save the pdf in a tmp-folder -* $fop = new xml_fo2pdf("simple.fo"); +* $fop = new xml_fo2pdf(); +* // the following 2 lines are the default settins, so not +* // necessary here, but you can set it to other values +* $fop->setRenderer("pdf"); +* $fop->setContentType("application/pdf"); +* if (PEAR::isError($error = $fop->run("simple.fo"))) +* { +* die("FOP ERROR: ". $error->getMessage()); +* } * //print pdf to the outputbuffer, * // including correct Header ("Content-type: application/pdf") * $fop->printPDF(); @@ -85,17 +87,19 @@ * print $content; * die(); * } -* $fop = new xml_fo2pdf("simple.fo"); +* $fop = new xml_fo2pdf(); +* $fop->run("simple.fo"); * $fop->printPDF(); * $fop->deletePDF(); * print $cache->end("+30"); * * @author Christian Stocker <chregu@nomad.ch> -* @version $Id: fo2pdf.php,v 1.3 2001/08/14 13:41:37 alexmerz Exp $ +* @version $Id: fo2pdf.php,v 1.4 2001/09/13 15:53:48 chregu Exp $ * @package XML */ +require_once( "PEAR.php") ; -class XML_fo2pdf { +class XML_fo2pdf extends pear { /** * fo-file used in this class @@ -125,23 +129,44 @@ */ var $tmppdfprefix = "pdffo"; - + /** + * the render Type. At the moment (fop 0.20.1), possible values are + * - awt + * - mif + * - pcl + * - pdf + * - ps + * - txt + * - xml + * + * @var string + */ + var $renderer = "pdf"; + + /** + * the content-type to be sent if printPDF is called. + * + * @var contenttype + " @see printPDF() + */ + var $contenttype = "application/pdf"; + /** * constructor - * One can pass an input fo-file already here (the other possibility - * is with the run or runFromString method). - * and if the pdf should be stored permanently, a filename/path for - * that can also be passed here. + * ATTENTION (you've been warned!): + * You should not pass the values here, 'cause then you don't have + * Error-Reporting. This variables are only here for Backwards Compatibilty.. + * Use $fop->run("input.fo","output.pdf") instead. * - * @param string $fo file input fo-file - * @param string $pdf file output pdf-file + * @param string $fo file input fo-file (do not use it anymore) + * @param string $pdf file output pdf-file (do not use it anymore) * @see run(), runFromString(), runFromFile() * @access public */ - function xml_fo2pdf($fo = "", $pdf = "") + function xml_fo2pdf ($fo = Null, $pdf = "") { - if ($fo) { - $this->run($fo, $pdf); + if (!(is_null($fo))) { + $this->run($fo, $pdf); } } @@ -166,12 +191,86 @@ $this->pdf = $pdf; $this->fo = $fo; - $options = array($this->fo, $this->pdf); - $java = new Java("org.apache.fop.apps.CommandLine", $options); - $java->run(); + $options = array( $this->fo,"-".$this->renderer,$this->pdf); + + /** + * according to the documentation, the following lines should be enough, + * to do, what we want. but it didn't work. Yes, it did, but it produced + * approx. 10 pdf-files for each run... + * + * $options = new Java("org.apache.fop.apps.CommandLineOptions",$options); + * $starter = $options->getStarter(); + * $starter->run(); + * + * Therefore i took the code from org/apache/fop/apps/CommandLineStarter.java + * converted it to php-code and it works now... if anyone has a better solution + * please inform me ;) + */ + java_last_exception_clear(); + $commandLineOptions= @new Java("org.apache.fop.apps.CommandLineOptions",$options); + + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + + $starter = new Java("org.apache.fop.apps.CommandLineStarter",$commandLineOptions); + + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + + $input = $commandLineOptions->getInputHandler(); + + $parser = $input->getParser(); + $starter->setParserFeatures($parser); + + $driver = new Java ("org.apache.fop.apps.Driver"); + + $driver->setBufferFile($commandLineOptions->getBufferFile()); + + $renderer = @$commandLineOptions->getRenderer(); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + @$driver->setRenderer($renderer); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + $stream = @new Java("java.io.FileOutputStream",$commandLineOptions->getOutputFile()); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + + @$driver->setOutputStream($stream); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + + $renderer = $driver->getRenderer(); + $renderer->setOptions($commandLineOptions->getRendererOptions()); + + @$driver->render($parser, $input->getInputSource()); + if ($exc = java_last_exception_get()) + { + return new PEAR_Error($exc->getMessage(), 11, PEAR_ERROR_RETURN, null, null ); + } + if ($DelFo) { $this->deleteFo($fo); } + + return True; } /** @@ -194,7 +293,7 @@ $fp = fopen($fo, "w+"); fwrite($fp, $fostring); fclose($fp); - $this->run($fo, $pdf, True); + return $this->run($fo, $pdf, True); } /** * A wrapper to run for better readabilty @@ -257,7 +356,7 @@ function printPDF($pdf = "") { $pdf = $this->returnPDF($pdf); - Header("Content-type: application/pdf\nContent-Length: " . strlen($pdf)); + Header("Content-type: ".$this->contenttype."\nContent-Length: " . strlen($pdf)); print $pdf; } @@ -280,6 +379,31 @@ fclose($fd); return $content; } + + /** + * sets the rendertype + * + * @see $this-renderer + * @access public + */ + + function setRenderer($renderer = "pdf") + { + $this->renderer = $renderer; + } + + /** + * sets the content-type + * + * @see $this-contenttype + * @access public + */ + + function setContentType($contenttype = "application/pdf") + { + $this->contenttype = $contenttype; + } + } ?> Index: pear/XML_fo2pdf/package.xml diff -u pear/XML_fo2pdf/package.xml:1.4 pear/XML_fo2pdf/package.xml:1.5 --- pear/XML_fo2pdf/package.xml:1.4 Tue Aug 28 13:23:59 2001 +++ pear/XML_fo2pdf/package.xml Thu Sep 13 11:53:48 2001 @@ -3,22 +3,27 @@ generated package.xml files --> <package> <name>XML_fo2pdf</name> - <summary>Convert a xsl-fo file to pdf with the help of apache-fop</summary> + <summary>Converts a xsl-fo file to pdf/ps/pcl/text/etc with the help of apache-fop</summary> <maintainer> <initials>cs</initials> <name>Christian Stocker</name> <email>chregu@php.net</email> </maintainer> <release> - <version>0.9</version> - <date>2001-06-21</date> + <version>0.95</version> + <date>2001-09-21</date> <notes> - Documentation is in the php-file itself. + - runs with fop 0.20 + - call of java objects is completely rewritten + - $fop = new XML_fo2pdf("input.fo") should not be used anymore, use $fop->run("input.fo") instead + - can output not only pdf, but also ps/pcl/txt and whatever fop supports </notes> </release> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">fo2pdf.php</file> + <file role="doc">README.fo2pdf</file> + <file role="doc">simple.fo</file> </dir> </filelist> </package> Index: pear/XML_fo2pdf/README.fo2pdf +++ pear/XML_fo2pdf/README.fo2pdf fo to pdf converter. with fo (formating objects) it's quite easy to convert xml-documents into pdf-docs (and not only pdf, but also ps, pcl, txt and more) An introduction into formating objects can be found at http://www.w3.org/TR/xsl/slice6.html#fo-section http://www.ibiblio.org/xml/books/bible/updates/15.html A tutorial is here: http://www.xml.com/pub/a/2001/01/17/xsl-fo/ http://www.xml.com/pub/a/2001/01/24/xsl-fo/ A html_to_fo.xsl can also be found there http://www.xml.com/2001/01/24/xsl-fo/fop_article.tgz but it didn't work for my simple xhtml files.. The way to use this class is, produce a fo-file from a xml-file with a xsl-stylesheet, then feed this class with this fo-file and you get a pdf back (either directly to the browser for really dynamic-pdf production or as a file on your filesystem) It is recommended to use the Cache-Classes from PEAR, if you want dynamic pdf production, since the process of making the pdfs takes some time. For an example of how to use Cache and fo2pdf see below. Requirements: You need Fop (version 0.20.1 was used for this) from the xml-apache project (http://xml.apache.org/fop) and Java (1.1.x or later, i tested it with 1.2.2 from sun on linux, see the Fop-Docs for details). Furthermore you have to compile your php with --with-java and to adjust your php.ini file. It can be a rather painful task to get java and php to work together. (i also tested this only with jdk 1.2.2, i couldn't 1.3.1 get to work) See http://www.phpbuilder.com/columns/marknold20001221.php3 or http://www.linuxwebdevnews.com/articles/php-java-xslt.php?pid=347 for more details about java and php or ask me, if you're stuck (especially with linux. windows is not my area..) Usage: require_once("XML/fo2pdf.php"); //make a pdf from simple.fo and save the pdf in a tmp-folder $fop = new xml_fo2pdf(); // the following 2 lines are the default settins, so not // necessary here, but you can set it to other values $fop->setRenderer("pdf"); $fop->setContentType("application/pdf"); if (PEAR::isError($error = $fop->run("simple.fo"))) { die("FOP ERROR: ". $error->getMessage()); } //print pdf to the outputbuffer, // including correct Header ("Content-type: application/pdf") $fop->printPDF(); //delete the temporary pdf file $fop->deletePDF(); With Cache: require_once("XML/fo2pdf.php"); require_once("Cache/Output.php"); $container = "file"; $options = array("cache_dir"=>"/tmp/"); $cache = new Cache_Output("$container",$options); $cache_handle = $cache->generateID($REQUEST_URI); if ($content = $cache->start($cache_handle)) { Header("Content-type: application/pdf"); print $content; die(); } $fop = new xml_fo2pdf(); $fop->run("simple.fo"); $fop->printPDF(); $fop->deletePDF(); print $cache->end("+30"); Index: pear/XML_fo2pdf/simple.fo +++ pear/XML_fo2pdf/simple.fo <?xml version="1.0" encoding="utf-8"?> <!-- example for a simple fo file. At the beginning the page layout is set. Below fo:root there is always - a single fo:layout-master-set which defines one or more page layouts - an optional fo:declarations - and a sequence of one or more fo:page-sequences containing the text and formatting instructions --> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <!-- fo:layout-master-set defines in its children the page layout: the pagination and layout specifications - page-masters: have the role of describing the intended subdivisions of a page and the geometry of these subdivisions In this case there is only a simple-page-master which defines the layout for all pages of the text --> <!-- layout information --> <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-body margin-top="3cm"/> <fo:region-before extent="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set> <!-- end: defines page layout --> <!-- start page-sequence here comes the text (contained in flow objects) the page-sequence can contain different fo:flows the attribute value of master-name refers to the page layout which is to be used to layout the text contained in this page-sequence--> <fo:page-sequence master-name="simple"> <!-- start fo:flow each flow is targeted at one (and only one) of the following: xsl-region-body (usually: normal text) xsl-region-before (usually: header) xsl-region-after (usually: footer) xsl-region-start (usually: left margin) xsl-region-end (usually: right margin) ['usually' applies here to languages with left-right and top-down writing direction like English] in this case there is only one target: xsl-region-body --> <fo:flow flow-name="xsl-region-body"> <!-- each paragraph is encapsulated in a block element the attributes of the block define font-family and size, line-heigth etc. --> <!-- this defines a title --> <fo:block font-size="18pt" font-family="sans-serif" line-height="24pt" space-after.optimum="15pt" background-color="blue" color="white" text-align="center" padding-top="3pt"> Extensible Markup Language (XML) 1.0 </fo:block> <!-- this defines normal text --> <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="3pt" text-align="justify"> The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML. </fo:block> <!-- this defines normal text --> <fo:block font-size="12pt" font-family="sans-serif" line-height="15pt" space-after.optimum="3pt" text-align="justify"> The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML has been designed for ease of implementation and for interoperability with both SGML and HTML. </fo:block> </fo:flow> <!-- closes the flow element--> </fo:page-sequence> <!-- closes the page-sequence --> </fo:root>