You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Kapil garg <ga...@hotmail.com> on 2011/05/23 00:10:35 UTC

HTML to PDF Need :NoSuchMEthod Erros on callingorg.apache.fop.fo.FOTreeBuilder in Driver class

Can some one please throw light as why the Driver class is missing in version 1.0.0. I used version.2.03 but then it has "init" method missing for FOPTreeBuilder.
O just simply want to convert HTML into PDF. I believe there has to be a way within Apache Fop to do this ? Am I wrong?

Which version of FOP supports HTML to PDF conversion? IS there a way one can convert html into a .fo file or XML and  then FOPFOtpPDF or FopXMLToPDF?
HOw does one proceed on just html page being the input for which PDf needs ot be created? IS there any provision for this html to PDf conversion?Is there  a sample code in any of the versions of FOP which converts HTML page into a 
PDF? Even if it is not in the example I can use a Java class if at all it is present in any of the FOP versions.
Is this something out of scope for FOP?>


Kapil Garg
 




From: gargkapil@hotmail.com
To: fop-dev@xml.apache.org
Subject: HTML to PDF :NoSuchMEthod Erros on callingorg.apache.fop.fo.FOTreeBuilder in Driver class
Date: Sun, 22 May 2011 05:05:31 +1000









Hello GuysBit new on Apache FOP. I am trying to run a demo code to convert HTML into PDF. I am getting following exception on using fop 0.20.5 versionI could not find the driver class in fop 1.0.0 version.IS there  a sample code that converts html into pdf in FOP 1.0.0 version. I need urgently some sort of HTML to PDF conversion library. Am I missing here? Would FOP work for HTML to PDF conversion? Any help would be appreciated.

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.fop.fo.FOTreeBuilder: method <init>()V not found	at org.apache.fop.apps.Driver.<init>(Driver.java:221)	at org.apache.fop.apps.Driver.<init>(Driver.java:226)	at com.smartbin.smartbinpdf.Html2Pdf.fo2PDF(Html2Pdf.java:135)	at com.smartbin.smartbinpdf.Html2Pdf.main(Html2Pdf.java:61)
This is my codepackage com.smartbin.smartbinpdf;import java.io.FileInputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.ByteArrayOutputStream;import java.util.logging.Level;
import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;import javax.xml.transform.dom.DOMSource;
import org.w3c.tidy.Tidy;import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.apache.fop.apps.Driver;import org.apache.fop.messaging.MessageHandler;import org.apache.fop.tools.DocumentInputSource;
import org.apache.avalon.framework.logger.ConsoleLogger;import org.apache.avalon.framework.logger.Logger;
/* *  Class that converts HTML to PDF using *  the DOM interfaces of JTidy, Xalan, and FOP. * *  @author Kapil Garg *  */public class Html2Pdf {

    public static void main(String[] args) {        FileInputStream input = null;	try {            // open file            //if (args.length != 2) {            //   System.out.println("Usage: Html2Pdf htmlFile styleSheet");            //  System.exit(1);            //}            //String htmlFileName = args[0];            String htmlFileName = "C:/fop-1.0/examples/html2pdf/hello.html";            //try {	    input = new FileInputStream(htmlFileName);            Tidy tidy = new Tidy();            //Document xmlDoc = tidy.parseDOM(input, null);            Document xmlDoc = tidy.parseDOM(input, null) ;            String stylesheet ="C:/fop-1.0/examples/html2pdf/xhtml2fo.xsl";            Document foDoc = xml2FO(xmlDoc, stylesheet);            String pdfFileName = htmlFileName.substring(0, htmlFileName.indexOf(".")) + ".pdf";            try {                OutputStream pdf = new FileOutputStream(new File(pdfFileName));                pdf.write(fo2PDF(foDoc));            }            catch (java.io.FileNotFoundException e) {                System.out.println("Error creating PDF: " + pdfFileName);            }            catch (java.io.IOException e) {                System.out.println("Error writing PDF: " + pdfFileName);            }            	}

        	catch (FileNotFoundException ex) {		java.util.logging.Logger.getLogger(Html2Pdf.class.getName()).log(Level.SEVERE, null, ex);	} finally {            try {                input.close();            } catch (IOException ex) {                java.util.logging.Logger.getLogger(Html2Pdf.class.getName()).log(Level.SEVERE, null, ex);            }        }
	    }

    /*     *  Applies stylesheet to input.     *     *  @param xml  The xml input Document     *       *  @param stylesheet Name of the stylesheet     *     *  @return Document  Result of the transform     */    private static Document xml2FO(Document xml, String styleSheet) {
	DOMSource xmlDomSource = new DOMSource(xml);      	DOMResult domResult = new DOMResult();
	Transformer transformer = getTransformer(styleSheet);		if (transformer == null) {	    System.out.println("Error creating transformer for " + styleSheet);	    System.exit(1);	}	try {	    transformer.transform(xmlDomSource, domResult);	}	catch (javax.xml.transform.TransformerException e) {	    return null;	}	return (Document) domResult.getNode();
    }

    /*     *  Apply FOP to XSL-FO input     *     *  @param foDocument  The XSL-FO input     *       *  @return byte[]  PDF result     */    private static byte[] fo2PDF(Document foDocument) {
        DocumentInputSource fopInputSource = new DocumentInputSource(                                                         foDocument);
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();            Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
            Driver driver = new Driver(fopInputSource, out);            driver.setLogger(log);            driver.setRenderer(Driver.RENDER_PDF);            driver.run();
            return out.toByteArray();
        } catch (Exception ex) {            return null;        }    }

    /*     *  Create and return a Transformer for the specified stylesheet.     *       *  Based on the DOM2DOM.java example in the Xalan distribution.     */    private static Transformer getTransformer(String styleSheet) {
	try {
	    TransformerFactory tFactory = TransformerFactory.newInstance();
	    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
	    dFactory.setNamespaceAware(true);      	    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();	    Document xslDoc = dBuilder.parse(styleSheet);	    DOMSource xslDomSource = new DOMSource(xslDoc);
	    return tFactory.newTransformer(xslDomSource);
	}	catch (javax.xml.transform.TransformerException e) {	    e.printStackTrace();	    return null;	}	catch (java.io.IOException e) {	    e.printStackTrace();	    return null;	}	catch (javax.xml.parsers.ParserConfigurationException e) {	    e.printStackTrace();	    return null;	}	catch (org.xml.sax.SAXException e) {		    e.printStackTrace();	    return null;	}
    }
}



Kapil Garg
 


 		 	   		  

AW: HTML to PDF Need :NoSuchMEthod Erros on callingorg.apache.fop.fo.FOTreeBuilder in Driver class

Posted by Georg Datterl <ge...@geneon.de>.
Hi Kapil Garg,

Between FOP 0.20  and Fop 1.0 there was a complete internal redesign. Examples how to start FOP now should be available, fop 0.20 is no longer supported.

A transformation from HTML to PDF will most likely never be perfect. There are transformation files out there and there are commercial products, but doing it on your own will only help you for simple pages. As soon as (for example) CSS was involved, I was completely lost.

Regards,

Georg Datterl

------ Kontakt ------

Georg Datterl

Geneon media solutions gmbh
Gutenstetter Straße 8a
90449 Nürnberg

HRB Nürnberg: 17193
Geschäftsführer: Yong-Harry Steiert

Tel.: 0911/36 78 88 - 26
Fax: 0911/36 78 88 - 20

www.geneon.de<http://www.geneon.de>

Weitere Mitglieder der Willmy MediaGroup:

IRS Integrated Realization Services GmbH:    www.irs-nbg.de<http://www.irs-nbg.de>
Willmy PrintMedia GmbH:                      www.willmy.de<http://www.willmy.de>
Willmy Consult & Content GmbH:               www.willmycc.de<http://www.willmycc.de>

Von: Kapil garg [mailto:gargkapil@hotmail.com]
Gesendet: Montag, 23. Mai 2011 00:11
An: fop-dev@xml.apache.org; fop-users@xmlgraphics.apache.org
Betreff: HTML to PDF Need :NoSuchMEthod Erros on callingorg.apache.fop.fo.FOTreeBuilder in Driver class

Can some one please throw light as why the Driver class is missing in version 1.0.0. I used version.2.03 but then it has "init" method missing for FOPTreeBuilder.

O just simply want to convert HTML into PDF. I believe there has to be a way within Apache Fop to do this ? Am I wrong?

Which version of FOP supports HTML to PDF conversion? IS there a way one can convert html into a .fo file or XML and  then FOPFOtpPDF or FopXMLToPDF?

HOw does one proceed on just html page being the input for which PDf needs ot be created? IS there any provision for this html to PDf conversion?
Is there  a sample code in any of the versions of FOP which converts HTML page into a
PDF? Even if it is not in the example I can use a Java class if at all it is present in any of the FOP versions.

Is this something out of scope for FOP?>
Kapil Garg



________________________________
From: gargkapil@hotmail.com
To: fop-dev@xml.apache.org
Subject: HTML to PDF :NoSuchMEthod Erros on callingorg.apache.fop.fo.FOTreeBuilder in Driver class
Date: Sun, 22 May 2011 05:05:31 +1000

Hello Guys
Bit new on Apache FOP. I am trying to run a demo code to convert HTML into PDF. I am getting following exception on using fop 0.20.5 version
I could not find the driver class in fop 1.0.0 version.
IS there  a sample code that converts html into pdf in FOP 1.0.0 version. I need urgently some sort of HTML to PDF conversion library. Am I missing here? Would FOP work for HTML to PDF conversion? Any help would be appreciated.


Exception in thread "main" java.lang.NoSuchMethodError: org.apache.fop.fo.FOTreeBuilder: method <init>()V not found
            at org.apache.fop.apps.Driver.<init>(Driver.java:221)
            at org.apache.fop.apps.Driver.<init>(Driver.java:226)
            at com.smartbin.smartbinpdf.Html2Pdf.fo2PDF(Html2Pdf.java:135)
            at com.smartbin.smartbinpdf.Html2Pdf.main(Html2Pdf.java:61)

This is my code
package com.smartbin.smartbinpdf;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.util.logging.Level;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;

import org.w3c.tidy.Tidy;
import org.w3c.dom.Document;

import org.xml.sax.InputSource;

import org.apache.fop.apps.Driver;
import org.apache.fop.messaging.MessageHandler;
import org.apache.fop.tools.DocumentInputSource;

import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

/*
 *  Class that converts HTML to PDF using
 *  the DOM interfaces of JTidy, Xalan, and FOP.
 *
 *  @author Kapil Garg
 *
 */
public class Html2Pdf {


    public static void main(String[] args) {
        FileInputStream input = null;
            try {
            // open file
            //if (args.length != 2) {
            //   System.out.println("Usage: Html2Pdf htmlFile styleSheet");
            //  System.exit(1);
            //}
            //String htmlFileName = args[0];
            String htmlFileName = "C:/fop-1.0/examples/html2pdf/hello.html";
            //try {
               input = new FileInputStream(htmlFileName);
            Tidy tidy = new Tidy();
            //Document xmlDoc = tidy.parseDOM(input, null);
            Document xmlDoc = tidy.parseDOM(input, null) ;
            String stylesheet ="C:/fop-1.0/examples/html2pdf/xhtml2fo.xsl";
            Document foDoc = xml2FO(xmlDoc, stylesheet);
            String pdfFileName = htmlFileName.substring(0, htmlFileName.indexOf(".")) + ".pdf";
            try {
                OutputStream pdf = new FileOutputStream(new File(pdfFileName));
                pdf.write(fo2PDF(foDoc));
            }
            catch (java.io.FileNotFoundException e) {
                System.out.println("Error creating PDF: " + pdfFileName);
            }
            catch (java.io.IOException e) {
                System.out.println("Error writing PDF: " + pdfFileName);
            }

            }


            catch (FileNotFoundException ex) {
                        java.util.logging.Logger.getLogger(Html2Pdf.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
            try {
                input.close();
            } catch (IOException ex) {
                java.util.logging.Logger.getLogger(Html2Pdf.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }


    /*
     *  Applies stylesheet to input.
     *
     *  @param xml  The xml input Document
     *
     *  @param stylesheet Name of the stylesheet
     *
     *  @return Document  Result of the transform
     */
    private static Document xml2FO(Document xml, String styleSheet) {

            DOMSource xmlDomSource = new DOMSource(xml);
            DOMResult domResult = new DOMResult();

            Transformer transformer = getTransformer(styleSheet);

            if (transformer == null) {
               System.out.println("Error creating transformer for " + styleSheet);
               System.exit(1);
            }
            try {
               transformer.transform(xmlDomSource, domResult);
            }
            catch (javax.xml.transform.TransformerException e) {
               return null;
            }
            return (Document) domResult.getNode();

    }


    /*
     *  Apply FOP to XSL-FO input
     *
     *  @param foDocument  The XSL-FO input
     *
     *  @return byte[]  PDF result
     */
    private static byte[] fo2PDF(Document foDocument) {

        DocumentInputSource fopInputSource = new DocumentInputSource(
                                                         foDocument);

        try {

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);

            Driver driver = new Driver(fopInputSource, out);
            driver.setLogger(log);
            driver.setRenderer(Driver.RENDER_PDF);
            driver.run();

            return out.toByteArray();

        } catch (Exception ex) {
            return null;
        }
    }


    /*
     *  Create and return a Transformer for the specified stylesheet.
     *
     *  Based on the DOM2DOM.java example in the Xalan distribution.
     */
    private static Transformer getTransformer(String styleSheet) {

            try {

               TransformerFactory tFactory = TransformerFactory.newInstance();

               DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

               dFactory.setNamespaceAware(true);

               DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
               Document xslDoc = dBuilder.parse(styleSheet);
               DOMSource xslDomSource = new DOMSource(xslDoc);

               return tFactory.newTransformer(xslDomSource);

            }
            catch (javax.xml.transform.TransformerException e) {
               e.printStackTrace();
               return null;
            }
            catch (java.io.IOException e) {
               e.printStackTrace();
               return null;
            }
            catch (javax.xml.parsers.ParserConfigurationException e) {
               e.printStackTrace();
               return null;
            }
            catch (org.xml.sax.SAXException e) {
               e.printStackTrace();
               return null;
            }

    }

}



Kapil Garg