Problems running Apache FOP: org.apache.fop.fo.FOTreeBuilder fatalError

Discussion in 'Java' started by Pablo, Mar 28, 2007.

  1. Pablo

    Pablo Guest

    Dear all,

    I am trying to use Apache FOP to create PDF from URLs, but something
    is going wrong.

    I am using JTidy to convert a URL to XHTML, and then FOP to transform
    this XHTML to PDF.

    I am using a xslt file that I have downloaded here to transform xhtml
    to FO format: http://www.antennahouse.com/XSLsample/XSLsample.htm
    and http://www-128.ibm.com/developerworks/library/x-xslfo2app/

    I am wondering if anyone have a working example of FOP to generate
    PDFs from HTML that could be shared here.

    My code is bellow:

    public static void main(String[] args) {
    PDFMaker pdf = new PDFMaker();
    pdf.setPdfPath("teste.pdf");
    pdf.setUrl("http://validator.w3.org/");
    pdf.setXmlPath("test.xml");
    pdf.setXsltPath("xhtml2fo-ms.xsl");
    pdf.convert2PDF();
    }


    public void convert2PDF() {
    try {
    // Setup directories
    File baseDir = new File("e:/pdf-temp/");
    File outDir = new File(baseDir, "out");
    outDir.mkdirs();

    // Setup input and output files
    File xmlfile = this.convert2Xhtml(baseDir,
    this.getXmlPath()); //input xml using JTidy
    File xsltfile = new File(baseDir, this.getXsltPath()); //
    xslt style sheet
    File pdffile = new File(outDir, this.getPdfPath()); //
    output pdf

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

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

    // Setup output
    OutputStream out = new java.io.FileOutputStream(pdffile);
    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(xsltfile));

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

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

    //set transformer parameters
    transformer.setParameter("title-text","PDFTester");
    transformer.setParameter("docid-text","JK130759");
    transformer.setParameter("version-text","V0.0");
    transformer.setParameter("status-text","Onbekend");
    transformer.setParameter("authorizor-text","Jacques
    Kors");
    transformer.setParameter("date-text",(new
    SimpleDateFormat("dd/MM/yyyy")).format(new Date()));
    transformer.setParameter("copytype-text","Test
    kopie");

    // Start XSLT transformation and FOP processing
    transformer.transform(src, res);
    } finally {
    out.close();
    }
    } catch (Exception e) {
    e.printStackTrace(System.err);
    System.exit(-1);
    }
    }
     
    Pablo, Mar 28, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    952
  2. Manoj Nair
    Replies:
    0
    Views:
    634
    Manoj Nair
    Jun 8, 2004
  3. Replies:
    4
    Views:
    608
    Joseph Kesselman
    Aug 10, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    701
    PerlFAQ Server
    Feb 3, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    708
    PerlFAQ Server
    Apr 4, 2011
Loading...

Share This Page