找不到包装fop(Ithink)

时间:2010-04-21 12:44:07

标签: java apache-fop

朋友您好我尝试以编程方式使用fop引擎我搜索了一个示例,我找到了这个类

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

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;

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

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;

public class Invokefop 
{

    public void convertXML2PDF(File xml, File xslt, File pdf) 
                throws IOException, FOPException, TransformerException {
        Driver driver = new Driver();
        Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
        driver.setLogger(logger);
        MessageHandler.setScreenLogger(logger);       
        driver.setRenderer(Driver.RENDER_PDF);
        OutputStream out = new java.io.FileOutputStream(pdf);
        try {
            driver.setOutputStream(out);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xslt));
            Source src = new StreamSource(xml);
            Result res = new SAXResult(driver.getContentHandler());
            transformer.transform(src, res);
        } finally {
            out.close();
        }
    }


    public static void main(String[] args) {
        try {
            System.out.println("JAVA XML2PDF:: FOP ExampleXML2PDF\n");
            System.out.println("JAVA XML2PDF:: Preparing...");

            File base = new File("../");
            File baseDir = new File(base, "in");
            File outDir = new File(base, "out");
            outDir.mkdirs();


            File xmlfile = new File(baseDir, args[0]);
            File xsltfile = new File(baseDir, args[1]);
            File pdffile = new File(outDir, args[2]);

            System.out.println("JAVA XML2PDF:: Input: XML (" + xmlfile + ")");
            System.out.println("JAVA XML2PDF:: Stylesheet: " + xsltfile);
            System.out.println("JAVA XML2PDF:: Output: PDF (" + pdffile + ")");
            System.out.println();
            System.out.println("JAVA XML2PDF:: Transforming...");

            Invokefop app = new Invokefop();
            app.convertXML2PDF(xmlfile, xsltfile, pdffile);

            System.out.println("JAVA XML2PDF:: Success!");
        } catch (Exception e) {
            System.err.println(ExceptionUtil.printStackTrace(e));
            System.exit(-1);
        }
    }
}

来自Fop的所有Lib都在Classpath中,包括构建目录中的fop.jar。后 我运行javac Invokefop.java我收到此错误:

> C:\....\fop>javac Invokefop.java
Invokefop.java:21: cannot find symbol
symbol  : class Driver
location: package org.apache.fop.apps
import org.apache.fop.apps.Driver;
                          ^
Invokefop.java:23: package org.apache.fop.messaging does not exist
import org.apache.fop.messaging.MessageHandler;
                               ^
Invokefop.java:31: cannot find symbol
symbol  : class Driver
location: class Invokefop
        Driver driver = new Driver();
        ^
Invokefop.java:31: cannot find symbol
symbol  : class Driver
location: class Invokefop
        Driver driver = new Driver();
                            ^
Invokefop.java:36: cannot find symbol
symbol  : variable MessageHandler
location: class Invokefop
        MessageHandler.setScreenLogger(logger);
        ^
Invokefop.java:39: cannot find symbol
symbol  : variable Driver
location: class Invokefop
        driver.setRenderer(Driver.RENDER_PDF);
                           ^
6 errors

我对Java比较陌生,但是通过这种方法,我尝试使用这个java类在c ++中执行fop引擎。

有任何想法,如何解决这个错误...... Thanx提前..

2 个答案:

答案 0 :(得分:1)

fop.jar文件中不存在“无法找到符号”错误的类。

我刚刚下载了FOP库的v0.95,我无法在发行版的任何jar文件中看到这些类。 (您可以在任何zip程序中打开一个jar文件并查看 - 一个jar基本上是一个zip文件,其中有一些Java使用的额外位)。

我猜测示例代码中引用的类在FOP库的某个阶段被删除,示例适用于旧版本。因此,您可以尝试查找包含这些类的库的旧版本,或者查找更新的示例。二进制发行版附带了一些示例 - 可能会查看这些示例以查看是否对您有用?

答案 1 :(得分:0)

就我而言,我在 pom.xml

中添加了依赖项
<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>fop</artifactId>
    <version>2.5</version>
</dependency>