shadedecho
Programmer
(I searched and searched on this site, thinking surely i'm not the first person in these forums to want to know how, in purely PHP, to convert an HTML page to a PDF file. However, I couldn't find any relevant FAQ's or posts, so here it goes.)
FYI: I am running PHP 5.0.3 on Apache2 on a Linux machine. I have complete control over the server, so I can install PHP libraries/extensions at will, but I want to keep them FREE (open-source, non-commercial) and NON-JAVA.
I already have PHP compiled with PDF, XSL, and XSLT support.
What I want seems simple: I want to be able to provide a "printer-friendly" version of certain pages on my existing website, which is coded entirely in PHP.
The problem is, I didn't plan ahead enough to have the site using any kind of templates or XML as the base data store, so my only real choice (aside from 1. re-coding ths site, or 2. having to maintain two sets of layout code, one for HTML and one for printer-friendly) is to have a way for a PHP script to take some of my existing HTML (code snippets for the page content only, for instance) and convert THAT (with maybe some other HTML formatting around it) to a pdf file.
Here's what I've got in that process so far (and please enlighten me if this is not the best path, etc).
1. Convert the HTML to XML:
I have the PHP Tidy library installed, and based on other FAQ's I've found, I believe Tidy can very easily (if nothing else but from the command line) convert some HTML to XML/XHTML compliant format.
2. Convert the XML to XSL-FO:
I believe this step can either be accomplished by applying an XSLT transformation to the resulting XML (XSL stylesheet) or by simply reading in the XML into PHP using standard XML parsing, and then applying PHP logic to what is read to generate and output the XSL-FO. I have to read up on XSL-FO, but I believe I should be able to accomplish this step in time.
3. Render the XSL-FO into PDF:
Using some sort of XSL rendering engine, I believe I undestand this can be accomplished. However, this is where my knowledge and understanding breaks down. I have seen that there are several commercial rendering programs out there, such as "FOP" (from Apache) and "XSL Formatter", which claim to be able to accomplish this goal.
However, it appears that they are all either Java or Commercial (fee-based). I thought for sure that PHP had some way of doing XSL rendering (isn't this the same as doing an XSLT transformation, which I know PHP supports?) Does PHP support rendering of XSL-FO?
I'd really like to avoid having to use some external program like this, I'd like to keep the transformation from HTML to PDF completely inside of PHP, without having to actually input/output physical xml/pdf files.
FYI: I am running PHP 5.0.3 on Apache2 on a Linux machine. I have complete control over the server, so I can install PHP libraries/extensions at will, but I want to keep them FREE (open-source, non-commercial) and NON-JAVA.
What I want seems simple: I want to be able to provide a "printer-friendly" version of certain pages on my existing website, which is coded entirely in PHP.
The problem is, I didn't plan ahead enough to have the site using any kind of templates or XML as the base data store, so my only real choice (aside from 1. re-coding ths site, or 2. having to maintain two sets of layout code, one for HTML and one for printer-friendly) is to have a way for a PHP script to take some of my existing HTML (code snippets for the page content only, for instance) and convert THAT (with maybe some other HTML formatting around it) to a pdf file.
Here's what I've got in that process so far (and please enlighten me if this is not the best path, etc).
1. Convert the HTML to XML:
I have the PHP Tidy library installed, and based on other FAQ's I've found, I believe Tidy can very easily (if nothing else but from the command line) convert some HTML to XML/XHTML compliant format.
2. Convert the XML to XSL-FO:
I believe this step can either be accomplished by applying an XSLT transformation to the resulting XML (XSL stylesheet) or by simply reading in the XML into PHP using standard XML parsing, and then applying PHP logic to what is read to generate and output the XSL-FO. I have to read up on XSL-FO, but I believe I should be able to accomplish this step in time.
3. Render the XSL-FO into PDF:
Using some sort of XSL rendering engine, I believe I undestand this can be accomplished. However, this is where my knowledge and understanding breaks down. I have seen that there are several commercial rendering programs out there, such as "FOP" (from Apache) and "XSL Formatter", which claim to be able to accomplish this goal.
However, it appears that they are all either Java or Commercial (fee-based). I thought for sure that PHP had some way of doing XSL rendering (isn't this the same as doing an XSLT transformation, which I know PHP supports?) Does PHP support rendering of XSL-FO?
I'd really like to avoid having to use some external program like this, I'd like to keep the transformation from HTML to PDF completely inside of PHP, without having to actually input/output physical xml/pdf files.