1

你好朋友我尝试以编程方式使用 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 的所有库都在类路径中,包括构建目录中的 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 引擎。

有没有人一些想法,如何解决这个错误......提前谢谢......

4

2 回答 2

1

文件中不存在出现“找不到符号”错误的类fop.jar

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

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

于 2010-04-21T13:16:16.110 回答
0

就我而言,我添加了依赖项pom.xml

<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>fop</artifactId>
    <version>2.5</version>
</dependency>
于 2020-12-31T23:42:55.850 回答