提供两个jsp/html转pdf的方法:
/**
* 把URL转换为PDF
* @param outputFile , 示例:E:/11111.pdf
* @param url,示例:http://localhost:8080/xxx/yyy.do?method=xxx
* @return
* @throws Exception
*/
public static boolean htmlToPdf(String outputFile, String url)throws Exception {
File outFile = new File(outputFile);
if (!outFile.exists()) {
outFile.getParentFile().mkdirs();
}
OutputStream os = new FileOutputStream(outputFile);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
String content=getUrlContent(url);
ITextRenderer renderer = new ITextRenderer();
Document doc = builder.parse(new ByteArrayInputStream(url.getBytes("utf-8")));
renderer.setDocument(doc, null);
// renderer.setDocumentFromString(content);
String fontPath = PdfUtil.class.getClassLoader().getResource("").toString().replaceAll("file:/", "")+ "simsun.ttc";
System.out.println(fontPath);
// 解决中文支持问题
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
System.out.println("文件转换成功");
return true;
}
/**
* 把URL转换为PDF
* @param outputFile , 示例:E:/11111.pdf
* @param url,示例:http://localhost:8080/xxx/yyy.do?method=xxx
* @return
* @throws Exception
*/
public static boolean htmlToPdf2(String outputFile, String url)
throws Exception {
File outFile = new File(outputFile);
if (!outFile.exists()) {
outFile.getParentFile().mkdirs();
}
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
String fontPath = PdfUtil.class.getClassLoader().getResource("").toString().replaceAll("file:/", "")+ "simsun.ttc";
System.out.println(fontPath);
// 解决中文支持问题
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
System.out.println("文件转换成功");
return true;
}
使用方法如下:
htmlToPdf2("E:/11111.pdf","http://localhost:8080/xxx/ccc/aaa.do?method=xxx");
htmlToPdf("E:/11111.pdf","http://localhost:8080/xxx/ccc/aaa.do?method=xxx");
需要的依赖是:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
可能遇到的问题:
1. NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
解决: documentBuilderFactory.setNamespaceAware(true); 大多数情况下,此异常与使用的旧版本的 Xalan 有关。有条件的可以升级 到 2.7.0。
2. [Fatal Error] :1:1: Content is not allowed in prolog.
解决: 检查各种标签是否是<div></div>这种完整的标签, 尤其注意</br>这种是不行的,必须是<br />
3. The entity name must immediately follow the '&' in the entity reference
解决:&必须要转义 用&来替代&, 尤其注意&&要换成&&
4.The reference to entity "w" must end with the ';' delimiter.
解决: 字面意思,但我没找到我项目中的问题在哪...............九命!!!
5. org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
解决: 大多数情况下,此异常与使用的旧版本的 Xalan 有关。确保升级 到 2.7.0。
博主已经崩溃了,大家可以一起交流=,=