HTML转PDF
各位大哥大街们,我想把HTML转换成PDF,使用的是FOP,但是报错:
java.lang.ClassCastException: org.apache.fop.layout.BlockArea cannot be cast to org.apache.fop.layout.AreaContainer
at com.test.Html2Pdf.fo2PDF(Html2Pdf.java:115)
115是driver.run()。程序代码如下:
各位清帮帮我吧,非常感谢!
java.lang.ClassCastException: org.apache.fop.layout.BlockArea cannot be cast to org.apache.fop.layout.AreaContainer
at com.test.Html2Pdf.fo2PDF(Html2Pdf.java:115)
115是driver.run()。程序代码如下:
package com.test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
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.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Driver;
import org.apache.fop.tools.DocumentInputSource;
import org.w3c.dom.Document;
import org.w3c.tidy.Tidy;
/*
* Class that converts HTML to PDF using
* the DOM interfaces of JTidy, Xalan, and FOP.
*
* @author N. Afshartous
*
*/
public class Html2Pdf {
public static void main(String[] args) {
// open file
// if (args.length != 2) {
// System.out.println("Usage: Html2Pdf htmlFile styleSheet");
// System.exit(1);
// }
FileInputStream input = null;
String htmlFileName = "/home/zoutuo/hot_problem.html";
String styleSheet = "/home/zoutuo/xhtml2fo.xsl";
try {
input = new FileInputStream(htmlFileName);
} catch (java.io.FileNotFoundException e) {
System.out.println("File not found: " + htmlFileName);
}
Tidy tidy = new Tidy();
Document xmlDoc = tidy.parseDOM(input, null);
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);
}
}
/*
* 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);
ByteArrayOutputStream out = null;
try {
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) {
ex.printStackTrace();
}
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;
}
}
}
各位清帮帮我吧,非常感谢!
...全文
相关推荐
java pdf转html
NULL 博文链接:https://wu1g119.iteye.com/blog/2281017
Java HTML转PDF完整教程
在当今的IT领域中,对于数据的处理和呈现方式要求越来越严格,特别是在需要将信息转化成PDF格式的场景下。iText是一个强大的Java类库,广泛用于创建和操作PDF文档。它为开发者提供了便捷的API,以编程方式创建高质量的PDF文档,并可包含文本、图像、表格和样式。iText库历经了多年的发展,已被广泛应用于各种项目中,尤其是在涉及到生成PDF报表和发票时。此外,iText还提供了丰富的插件,比如iTextPDF,这为处理复杂的PDF文档提供了更多便捷的方式。
html转pdf、字符串转pdf
1.使用PD4ML 2.HTML转PDF 3.字符串转PDF 4.中文无乱码 4.排版不乱
JAVA实现PDF转HTML
该包包含后端代码,转换控件,测试文件,正式项目应用表现正常,注意不能转换水印、印章这类特殊类型,且行且珍惜。
vue html转pdf
vue框架中html转pdf代码,很实用,适合项目常用作为工具类和平时开发调试用。


