1

I am new with Java and I'm trying out some stuffs like generating a PDF I am following the accepted answer of this post I had done everything, slowly but secure.

I am stuck at the last step using ApacheFOP I am almost done but there's an issue I can't fix.

Code:

    package calculadoraviajes;

/**
 *
 * @author Victor
 */
import javax.xml.transform.*;
import java.io.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOPException;


import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;




public class HowToXSLT {



    public static void main(String[] args) throws IOException, TransformerException, FOPException {
      try {

        TransformerFactory tFactory = TransformerFactory.newInstance();

        Transformer transformer =
          tFactory.newTransformer
             (new javax.xml.transform.stream.StreamSource
                ("howto.xsl"));

        transformer.transform
          (new javax.xml.transform.stream.StreamSource
                ("howto.xml"),
           new javax.xml.transform.stream.StreamResult
                ( new FileOutputStream("howto.fo")));
      }
      catch (Exception e) {
        e.printStackTrace( );
        }

      FopFactory fopFactory = FopFactory.newInstance();

      try(OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("exito.pdf")))) {
          Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

          TransformerFactory factory = TransformerFactory.newInstance();
          Transformer transformer = factory.newTransformer();

          Source src = new StreamSource(new File("howto.fo"));

          Result res = new SAXResult(fop.getDefaultHandler());

          transformer.transform(src, res);
      }


    }

}

My problem is here:

FopFactory fopFactory = FopFactory.newInstance();

Seems like I can't leave the newInstance(); parameters empy but following this code example:

ApacheFOP Basic Usage Pattern

Everything should be okay but NetBeans say:

No suitable method found for newInstance(no arguments)

I am kinda sure the error it's in my code or library but to be honest my Java knowledge is totally new.

Thanks so much for your time reading this and probably giving me some light.

Community
  • 1
  • 1
  • which version of the library are you using?fop-0.9.5.jar works fine.Please check – S.B Mar 14 '16 at 10:16
  • @S.B I donwloaded the last version of ApacheFOP which is the 2.1 and I am using, obviously, the fop.jar library into that 2.1 version. – Víctor Rodríguez Mar 14 '16 at 10:21
  • 1
    2.1 version doesn't support that method,please refer the link and check if it helps : http://stackoverflow.com/questions/34821013/apache-fop-the-method-newinstancefopfactoryconfig-in-the-type-fopfactory-is-no – S.B Mar 14 '16 at 10:30
  • @S.B Ohh that's why... I am now giving it the path from my root project folder, `FopFactory fopFactory = FopFactory.newInstance(new File("ApacheFOP/conf/fop.xconf"));` But now this say **Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.fop.apps.FopFactory.(FopFactory.java:65) at calculadoraviajes.HowToXSLT.main(HowToXSLT.java:51) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory** – Víctor Rodríguez Mar 14 '16 at 10:44
  • you need to include the org-apache-commons-logging.jar file to your build path.If you get similar error(NoClassDefFoundError),you will need to include the jar files which include those classes. – S.B Mar 14 '16 at 11:02
  • @S.B Thanks you so much for you help, you enlighten me in important stuffs for my future Java develop. – Víctor Rodríguez Mar 14 '16 at 12:34

0 Answers0