1.首先下载itext2.08,iTextAsian(亚洲国际化包支持pdf中显示中文),core-Renderer(解析xhtml转换成pdf支持包),jtidy(html转换成xhtml支持包).其中core-Renderer包必须配合itext2.08版本的包,否则会抛出没有是public int[] getCharBBox(char c) 方法异常。
2.使用HttpURLConnection类发送一个post 请求。并利用HttpURLConnection中getInputStream方法获取生成的html的流。
3.利用jtidy将html流转换成xhtml流。并将xhtml流生成xhtml的document文档,最后利用ITextRenderer解析xhtml的document文档生成pdf的OutPutStream流生成pdf.
注意:如果网页html的栏位太多,使网页html转换成pdf只会显示一半,这样就必须将字体变小
如将上面字体12px改成11px或更小就可以:
style.setTextContent("body { font-family: SimHei;font-size:11px;}");
4.通过拦截器获html并将html转换成excel和pdf
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
String queryString = ((HttpServletRequest) request).getQueryString();
String uri = ((HttpServletRequest) request).getRequestURI();
if (queryString == null) {
chain.doFilter(request, response);
return;
}
response.setCharacterEncoding("UTF-8");
WapperedResponse wrapper = new WapperedResponse((HttpServletResponse) response);
chain.doFilter(request, wrapper);
byte[] excelBytes = wrapper.getResponseData();
ByteArrayInputStream bis = new ByteArrayInputStream(excelBytes);
java.io.BufferedOutputStream bos = null;
HttpServletResponse httpResp = (HttpServletResponse) response;
httpResp.reset();
if (!p.matcher(queryString).find()) {
response.setCharacterEncoding("UTF-8");
httpResp.setHeader("Pragma", "no-cache");
httpResp.setHeader("Cache-Control", "no-cache");
httpResp.setDateHeader("Expires", 0);
httpResp.setContentType("application/vnd.ms-excel");
httpResp.setHeader("Content-disposition", "attachment; filename="
+ encodingFileName(getFileName(uri).replaceAll(".pdf", ".xls")));
String filePath = ApplicationSetting.getProperty("save_report_excelTemp");
if (!new File(filePath).exists())
new File(filePath).mkdirs();
String htmlName = filePath + UUID.randomUUID().toString().replace("-", "") + ".html";
String excelName = filePath + UUID.randomUUID().toString().replace("-", "") + ".xls";
FileOutputStream stream = new FileOutputStream(new File(htmlName));
stream.write(excelBytes);
stream.close();
stream = null;
bis = null;
excelBytes = null;
System.gc();
try {
htmlToExcel(htmlName, excelName);// 一般耗時在打開html檔上
} catch (Exception e) {
e.printStackTrace();
} finally {
if (new File(htmlName).exists())
new File(htmlName).delete();
}
FileInputStream inStream = new FileInputStream(new File(excelName));
byte[] content = new byte[1000];
while (inStream.read(content) != -1) {
response.getOutputStream().write(content);
}
if (inStream != null)
inStream.close();
if (response.getOutputStream() != null)
response.getOutputStream().close();
if (new File(excelName).exists())
new File(excelName).delete();
} else {
httpResp.setHeader("Content-Disposition", " attachment; filename=\"my.pdf\"");
httpResp.setHeader("Expires", "0");
httpResp.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
httpResp.setHeader("Pragma", "public");
String fileName = getFileName(uri);
try {
OutputStream ou = response.getOutputStream();
bos = new java.io.BufferedOutputStream(ou);
httpResp.setContentType("application/pdf");
httpResp.setHeader("Content-disposition", "attachment; filename=" + encodingFileName(fileName));
String pdfFontSize = ((HttpServletRequest) request).getParameter("pdfFontSize");
PdfRender.html2pdf(bis, "utf-8", pdfFontSize).writeTo(bos);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.部分参考资料网址
http://www.iteye.com/topic/509417?page=6
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
http://blog.csdn.net/zdtwyjp/archive/2010/07/27/5769353.aspx
- 大小: 97.3 KB
- 大小: 147.9 KB
分享到:
相关推荐
1.用户导入word生成pdf -首先导入word的时候读取word里面的全部内容。调用(ReadAndWriteDoc)然后将内容 传送到createPdf里面生成pdf。导入的时候读取采用poi /生成的时候采用的是...4.将html转换成pdf(itext)
java Itext 将tif转换成PDF,如果tif多页的情况下PDF也多页展示,里面提供程序所用的Itext的jar包
itext7 html转换为pdf;iText7页码、页眉、页脚,itext 的复杂表格实现;完整springboot项目代码
java实现html转pdf,包含后端代码,html测试页面,pom依赖,jar包,如页面过于复杂需调整页面,有些样式itextpdf不支持,请寻找其它样式替换。
使用iText-2.0.8将Html转换为pdf,带图片功能
使用itext7将html转换为pdf,支持中文,可以生产页眉,页脚,这个是windows系统的,里面还需要自己在pom.xml中导入相关的包,不会的可以找我
逻辑思路 1、先将excel转为pdf (TestForExcel2PDF测试类) 2、然后再pdf上加水印(TestWaterPrint、TestwaterMark都为加水印测试类)
包含 itext-asian-5.2.0.jar itextpdf-5.5.5.jar
iText7 用于html转换pdf核心包-html2pdf-1.0.1.jar。。。。
csv数据文件转换成pdf文件 itextpdf-5.1.3.jar iTextAsian.jar
1、itext将静态html转pdf中文显示及换行问题 2、itext将静态html转pdf 字体问题 3、html转pdf 参考我的另外一篇博文https://blog.csdn.net/qq_38616723/article/details/125188407?spm=1001.2014.3001.5502
IText5实现Html转PDF(不含图片、表格) 含:字体文件、hmtl文件、jar包、Java代码 安装字体或将字体文件放到相应的目录下。 jar包中的 core-renderer-all.jar 为其他大牛重写的,解决了中文字体和换行的问题。 Html...
NULL 博文链接:https://jkfd.iteye.com/blog/2065757
itext5.4.2 html2pdf 支持中文
Itext实现Html转化为Doc源码、Doc中部分代码为html转化
不用改变源码,就可以实现换行
itextpdf将html生成pdf所需要的全部jar,亲测版本兼容性没有问题,可以放心使用
该项目实现了使用IText将html文件转为pdf文件功能。 步骤: (1):将字体文件按代码中写的路径放到磁盘对应位置。 (2):将template.html文件按代码中路径放到磁盘对应位置。 (3):直接运行HtmlToPdfUtilForCss...
利用itext将html转为pdf,支持中文,和图片,代码地址在https://www.cnblogs.com/bert227/p/9724254.html
利用itext读取PDF后使用pdfbox将PDF转为图片。压缩包内附有相应JAR文件