gpt4 book ai didi

java - Apache FOP - Fop 类型的 getDefaultHandler() 方法未定义

转载 作者:行者123 更新时间:2023-12-02 11:17:15 25 4
gpt4 key购买 nike

我需要使用 XSL - Apache FOP (Java) 将 XML 转换为 PDF 文档。我收到以下错误 The method getDefaultHandler() is undefined for the type Fop for the lower line

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

请找到我完整的JAVA代码。

public static void main (String args[])
{
// the XSL FO file
File xsltfile = new File("sample2.xsl");
// the XML file from which we take the name
StreamSource source = new StreamSource(new File("sample2.xml"));
// creation of transform source
StreamSource transformSource = new StreamSource(xsltfile);
// create an instance of fop factory
FopFactory fopFactory = FopFactory.newInstance();
// a user agent is needed for transformation
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// to store output
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

Transformer xslfoTransformer;
try
{
xslfoTransformer = getTransformer(transformSource);
// Construct fop with desired output format
Fop fop;
try
{
fop = fopFactory.newFop
(MimeConstants.MIME_PDF, foUserAgent, outStream);
// Resulting SAX events (the generated FO)
// must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());

// Start XSLT transformation and FOP processing
try
{
// everything will happen here..
xslfoTransformer.transform(source, res);
// if you want to get the PDF bytes, use the following code
//return outStream.toByteArray();

// if you want to save PDF file use the following code
File pdffile = new File("Result.pdf");
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
FileOutputStream str = new FileOutputStream(pdffile);
str.write(outStream.toByteArray());
str.close();
out.close();

}
catch (TransformerException e) {
throw e;
}
}
catch (FOPException e) {
throw e;
}
}
catch (TransformerConfigurationException e)
{
throw e;
}
catch (TransformerFactoryConfigurationError e)
{
throw e;
}
}

private static Transformer getTransformer(StreamSource streamSource)
{
// setup the xslt transformer
net.sf.saxon.TransformerFactoryImpl impl =
new net.sf.saxon.TransformerFactoryImpl();

try {
return impl.newTransformer(streamSource);

} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
return null;
}

请找到我的依赖项:

<!-- https://mvnrepository.com/artifact/fop/fop -->
<dependency>
<groupId>fop</groupId>
<artifactId>fop</artifactId>
<version>0.20.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.8.0-11</version>
</dependency>

最佳答案

您同时拥有 an old以及您的依赖项中的新版本 fop。因此,您的 IDE 或 Java 编译器必须选择旧版本,而它没有该方法。从依赖项中删除旧版本。

关于java - Apache FOP - Fop 类型的 getDefaultHandler() 方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50190666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com