也许只要大神几分钟时间,多谢
来源:互联网 发布:沈阳百度seo关键词 编辑:程序博客网 时间:2024/04/27 14:45
自己在做一个Html转换为Pdf格式的java实现的程序。不料却老报错,执行不了,希望得到大家的帮助,不尽感谢啊啊啊啊~错误如图。代码如下。(运行环境:Netbeans 6.8,WinXp)
import java.io.FileInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
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.w3c.tidy.Tidy;
import org.w3c.dom.Document;
import org.apache.fop.apps.Driver;
import org.apache.fop.tools.DocumentInputSource;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
/*
* 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 = args[0];
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, args[1]);
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);
try {
ByteArrayOutputStream 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) {
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;
}
}
}
- 也许只要大神几分钟时间,多谢
- 格式化时间,几分钟前,几小时前
- Oracle 通过时间(分钟)计算有几天几小时几分钟
- 只要几分钟就可以你的服务器或空间有防盗链功能
- VC制作一个简单的播放器(只要几分钟)
- 打造“免杀”的恶意程序需要多久?用这个工具,只要几分钟
- 只要6分钟,告诉你少走6年弯路,花点时间看看吧
- ios时间获取刚刚几分钟几秒钟几小时之前
- 深度学习及机器学习整理--多谢各个网络大神
- 获取系统当前时间前几分钟的时间
- 指定时间是否在当前时间几分钟之内
- 码农与大神也许只是一步之遥
- 时间转换 获取几分钟前的那一刻
- IOS 时间更新算法 刚刚 几分钟前
- iOS_时间戳与日期转换(几分钟前)
- iOS计算时间,刚刚,几分钟前,几小时前
- 几分钟时间,搞清开源许可证之间的区别
- iOS 时间戳转换为几分钟前等
- HDU 1157 快速排序
- xmu 1100无向图最小割
- 解决:对COM 组件的调用返回了错误 HRESULT E_FAIL
- android 中 unable to start service 错误解决方法
- 解决google账户不能同步的方法,如何修改host文件。和如何更新获取最新的IP地址!
- 也许只要大神几分钟时间,多谢
- [ERROR] Inconsistent accessibility --- 不一致的访问性
- android 内核编译,arm-eabi-gcc command not found,解决方案
- 20个开发人员非常有用的Java功能代码
- poj 2914 无向图最小割
- ORACLE_OCM.MGMT_CONFIG_JOB_2_2 ORA-12012
- 第一次部门聚餐
- 三维空间绕坐标轴的旋转变换
- 求职一些事