View Full Version : Generating HTML pages from XSL:FO and XML

08-26-2008, 01:29 PM

I have a XSL:FO file which is used to create PDF and it uses XML for data population. I want to know that is there any way to use the same XSL:FO file to create HTML pages. I need to have the HTML pages with same formatting as my PDFs have and I don't want to waste time creating new HTML pages.

I have tried creating HTML using the FopFactory which I use for creating PDF but it takes MimeConstants as input and is not working for text/html MimeType.

Any help would be highly appreciated.

- Sonika

Alex Vincent
08-27-2008, 07:13 AM
FopFactory? MimeConstants? What language is this?

Source code samples strongly recommended.

08-27-2008, 07:22 AM
I am using the following class which takes XML and XSL:FO files as input and generated a PDF:

import java.io.File;
import java.io.OutputStream;

import org.apache.fop.apps.*;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

public class GeneratePDf {
public static void main(String[] args) {
try {
File filePDFfile = new File(strOutputfilePath);
File fileXMLFile = new File(strXMLFile);
File fileXSLFile = new File(strXSLFile);
System.out.println("filePDFfile = " + filePDFfile.getAbsolutePath());

// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance();

FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired

// Setup output
OutputStream out = new java.io.FileOutputStream(filePDFfile);
out = new java.io.BufferedOutputStream(out);

try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,
foUserAgent, out);

// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory
.newTransformer(new StreamSource(fileXSLFile));

// Set the value of a <param> in the stylesheet
transformer.setParameter("versionParam", "2.0");

// Setup input for XSLT transformation
Source src = new StreamSource(fileXMLFile);

// Resulting SAX events (the generated FO) must be piped through
// to FOP
Result res = new SAXResult(fop.getDefaultHandler());

// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
} catch (Exception e) {


This doesn't help me to craete HTML from XSL:FO file. I have tries using the XSLT file downloaded from http://www.renderx.com/tools/fo2html.html
But that file gives me only basic formatting.