一般页面都是.jsp页面,所以要把.jsp转换成html,在生成pdf,在网上找了好多方法,只有用一个插件,wkhtmltopdf-0.8.3.exe,生成的pdf会相对的好看。

先附上我做的.jsp页面。

<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en" id="htmlId">
<head><meta charset="UTF-8" /><style>.box{/* width: 1000px;border:1px solid red; */text-align: center;}table{width: 100%;}table, th, td {border: 1px solid grey;border-collapse: collapse;font-size: 12px;}table th {height: 40px;line-height: 40px;background: #cdcdcd;text-align: center;}table td {height: 40px;line-height: 40px;color: black;text-align: center;}/*如果要隔行变色把下面解开换色就行*//*   table tr:nth-child(odd) {background-color: white;}table tr:nth-child(even) {background-color:#99ccff;}*/.civilRecord{margin-top: 20px;}/*这是固定某一列表格的宽度*/.civilRecord .th1{width: 70px;}.civilRecord .th3{width: 190px;}.civilRecord .th4{width: 190px;}.civilRecord .th5{width: 190px;}.protectionin{margin-top: 20px;}.bj{margin-left: 5%;margin-right: 5%;        }</style><script type="text/javascript" src="resource/jquery/jquery-1.9.1.min.js"></script><script type="text/javascript">function msg() {/* var url = 'assurehistory.do?reqCode=exportAssurehistoryPDF';//window.location.href = url; var html=$("#htmlId").html();//alert(html);//return;$.ajax({type: "post",url: url,data: {htmlContent:html},dataType: "json",success: function(data){//alert(data.msg);}}); */var form=$("<form>");//定义一个form表单form.attr("style","display:none");form.attr("target","");form.attr("method","post");form.attr("action","assurehistory.do?reqCode=exportAssurehistoryPDF");var input1=$("<input>");input1.attr("type","hidden");input1.attr("name","htmlContent");var html=$("#htmlId").html();// alert(html);input1.attr("value",html);$("#pdfBody").append(form);//将表单放置在web中form.append(input1);form.submit();//表单提交 }</script>
</head>
<body id="pdfBody">
<div class="box" style="height:430px;overflow:auto"><div style="margin-left:80%;line-height: 28px;position: relative;"><input type="button" value="导出PDF" οnclick="msg()"/></div><!-- <h3>三 公共信息明细</h3> --><div class="taxesRecord bj"><p>个人担保基础信息</p><table ><thead><tr><th>信息记录类型</th><th>账户类型</th><th>账户标识码</th><th>信息报告日期</th></tr></thead><tbody><tr><c:forEach var="assureinfos" items="${assureinfos}"><td>${assureinfos.infotypetext}</td><td>${assureinfos.acctypetext}</td><td>${assureinfos.acccode}</td><td>${assureinfos.repdateStr}</td></c:forEach></tr></tbody></table><table><thead><tr><th>报告时点说明代码</th><th>债务人姓名</th><th>债务人证件类型</th><th>债务人证件号码</th><th>业务管理机构代码</th></tr></thead><tbody><tr><c:forEach var="assureinfos" items="${assureinfos}"><td>${assureinfos.repcodetext}</td><td>${assureinfos.debtorname}</td><td>${assureinfos.debtoridtypetext}</td><td>${assureinfos.debtoridnum}</td><td>${assureinfos.organcode}</td></c:forEach></tr></tbody></table></div><div class="civilRecord bj"><p>担保人基本信息</p><table><thead><tr><th class="th1">担保业务大类</th><th class="th1">担保业务种类细分</th><th class="th1">开户日期</th><th class="th1">信用额度</th><th class="th1">币种</th></tr></thead><tbody><tr><c:forEach var="assurebasicinfos" items="${assurebasicinfos}"><td>${assurebasicinfos.suretybusstext }</td><td>${assurebasicinfos.suretybussspeartext}</td><td>${assurebasicinfos.opendateStr }</td><td>${assurebasicinfos.creditlimit }</td><td>${assurebasicinfos.currencytypetext }</td></c:forEach></tr></tbody></table><table><thead><tr><th class="th1">到期日期</th><th class="th1">反担保方式</th><th class="th1">其他还款保证方式</th><th class="th1">保证金百分比</th><th class="th1">担保合同文本编号</th></tr></thead><tbody><tr><c:forEach var="assurebasicinfos" items="${assurebasicinfos}"><td>${assurebasicinfos.enddateStr }</td><td>${assurebasicinfos.assuremodetext }</td><td>${assurebasicinfos.otherasstext }</td><td>${assurebasicinfos.marginpercent }</td><td>${assurebasicinfos.assurenum }</td></c:forEach></tr></tbody></table></div><div class="civilRecord bj"><p>在保责任信息</p><table><thead><tr><th class="th1">账户状态</th><th class="th1">在保余额</th><th class="th1">余额变化日期</th><th class="th1">五级分类</th><th class="th1">五级分类认定日期</th></tr></thead><tbody><tr><c:forEach var="protectioninfos" items="${protectioninfos}"><td>${protectioninfos.accstatetext }</td><td>${protectioninfos.protectionbal }</td><td>${protectioninfos.balchangedateStr }</td><td>${protectioninfos.classifytext }</td><td>${protectioninfos.classifydateStr }</td></c:forEach></tr></tbody></table><table><thead><tr><th class="th1">风险敞口</th><th class="th1">代偿标志</th><th class="th1">账户关闭日期</th><th class="th1"></th><th class="th1"></th></tr></thead><tbody><tr><c:forEach var="protectioninfos" items="${protectioninfos}"><td>${protectioninfos.riskexposure }</td><td>${protectioninfos.compenflagtext }</td><td>${protectioninfos.accclosedateStr }</td><td></td><td></td></c:forEach></tr></tbody></table></div><div class="civilRecord bj"><p>相关还款人信息</p><table><thead><tr><th class="th1">责任人个数</th><th class="th1">身份类别</th><th class="th1">责任人名称</th><th class="th1">责任人身份标识类型</th><th class="th1">责任人身份标识号码</th></tr></thead><c:forEach var="rep" items="${repaymentinfos}"><tbody><tr><td>${rep.perliabnum}</td><td>${rep.classifytypetext}</td><td>${rep.perliabname}</td><td>${rep.perliabtypetext}</td><td>${rep.perliabid}</td></tr></tbody></c:forEach></table><table><thead><tr><th class="th1">还款责任人类型</th><th class="th1">还款责任金额</th><th class="th1"></th><th class="th1"></th><th class="th1"></th></tr></thead><c:forEach var="rep" items="${repaymentinfos}"><tbody><tr><td>${rep.reperliabtypetext}</td><td>${rep.reperliabamot}</td><td></td><td></td><td></td></tr></tbody></c:forEach></table></div><div class="civilRecord bj"><p>抵押物信息</p><table><thead><tr><th class="th1">抵质押合同个数</th><th class="th1">抵质押合同标识码</th><th class="th1"></th><th class="th1"></th><th class="th1"></th></tr></thead><c:forEach var="contractinfos" items="${contractinfos}"><tbody><tr><td>${contractinfos.contractnum }</td><td>${contractinfos.contractid }</td><td></td><td></td><td></td></tr></tbody></c:forEach></table></div>
</div>
</body>
</html>

我用el表达式动态得到数据库里面的数据,然后在导出方法中,定义了from表单,把整个jsp源码获取到。

页面的效果:

下面附上后台的方法:

import java.io.BufferedInputStream;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.check.test.HtmlToPdf; 

 /*** 将html转成pdf* @param request* @param response* @return*/@RequestMapping(params = "reqCode=exportAssurehistoryPDF")public ModelAndView exportAssurehistoryPDF(HttpServletRequest request,HttpServletResponse response){try {request.setCharacterEncoding("UTF-8");} catch (UnsupportedEncodingException e2) {e2.printStackTrace();}String h = request.getParameter("htmlContent");System.out.println(h);String pPath=this.writeHtmlToFile(h, "assurehistory");HtmlToPdf.convert(pPath+"/assurehistory.html", pPath+"/assurehistory.pdf");try {//Thread.sleep(1000);response.setContentType("application/pdf");response.addHeader("content-disposition", "attachment;filename="+ URLEncoder.encode("个人担保信息查询统计.pdf", "UTF-8"));File file = new File(pPath+"/assurehistory.pdf");InputStream fis = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();OutputStream os = response.getOutputStream();os.write(buffer);os.flush();os.close();} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}/*** 将内容写入html文件* @param content* @param htmlName* @return*/private String writeHtmlToFile(String content,String htmlName) {String path = "";try {path = URLDecoder.decode(AssurehistoryController.class.getClassLoader().getResource("pdfFile/").getFile(), "utf-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}System.out.println(path);content=content.replace("<div class=\"box\" style=\"height:430px;overflow:auto\">", "<div class=\"box\" style=\"overflow:auto\">");content=content.replace("button", "hidden").replace("HEIGHT: 430px", "");File file = new File(path + htmlName+".html");BufferedWriter bw = null;try {bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"));  bw.write("<!DOCTYPE html><html lang=\"en\">");bw.write(content);bw.write("</html>");bw.flush();} catch (IOException e) {e.printStackTrace();} finally {if (bw != null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}return file.getParent();}

然后在定义一个HtmlToPdf类,你也可以把这个类的方法单独拿出来。

package com.check.test;import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;import com.eloan.app.assurehistory.controller.AssurehistoryController;public class HtmlToPdf {/*** html转pdf* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径* @param destPath pdf保存路径* @return 转换成功返回true*/public static boolean convert(String srcPath, String destPath){//wkhtmltopdf在系统中的路径String str="";try {str = URLDecoder.decode(AssurehistoryController.class.getClassLoader().getResource("pdfFile/wkhtmltopdf-0.8.3.exe").getFile(), "utf-8");} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}String path = new File(str).getPath();File file = new File(destPath);File parent = file.getParentFile();//如果pdf保存路径不存在,则创建路径if(!parent.exists()){parent.mkdirs();}StringBuilder cmd = new StringBuilder();cmd.append(path);cmd.append(" ");cmd.append(srcPath);cmd.append(" ");cmd.append(destPath);boolean result = true;try{Process proc = Runtime.getRuntime().exec(cmd.toString());proc.waitFor();}catch(Exception e){result = false;e.printStackTrace();}return result;}}

最后,还有个重要的,就是wkhtmltopdf-0.8.3.exe,这个大家可以在网上免费下载,我把它放在我项目的src/main/java下面

最后,生成的pdf效果是:

转载于:https://www.cnblogs.com/junrong624/p/7085017.html

html页面转换成pdf相关推荐

  1. 网页html转为pdf,html页面转换成PDF文件

    html页面转换成PDF文件 发布时间:2020-06-14 05:24:50 来源:51CTO 阅读:523 作者:robinmars package pdftest; import java.io ...

  2. jquery将html转为pdf文件,HTML+CSS入门 jsPDF插件实现将HTML页面转换成PDF详解

    本篇教程介绍了HTML+CSS入门 jsPDF插件实现将HTML页面转换成PDF详解,希望阅读本篇文章以后大家有所收获,帮助大家HTML+CSS入门. < 1.目的:在前段是 jQuery库 或 ...

  3. echart转换成pdf js_【jsPDF】jsPDF插件实现将html页面转换成PDF,并下载,支持分页...

    1.目的:在前段是 jQuery库 或者 VUE库 或者两者混合库,将html 页面和数据 转换成PDF格式并下载,支持分页 1.项目背景: 对客户报修记录进行分类统计,并生成各种饼图.柱状图.线性图 ...

  4. html长图转换成pdf,iOS将HTML页面转换成PDF文件保存到本地并分享传输文件

    第一次修改:2017.05.26 修改内容:之前的方法在转化带有图片的html页面时会出现图片缺失问题,由于之前我需要转换的html不包含图片,所以没有发现这一问题,现在增加一个直接将webView转 ...

  5. php中用tcpdf进行html合同页面转换成pdf格式的文档或者是直接打印

    1.https://github.com/mpdf/mpdf   github插件下载地址 2.dome测试 function tcpdf($str){ //引入tcpdf插件        requ ...

  6. wkhtmltopdf:wkhtmltopdf(将html转换成pdf的利器)简介、安装、使用方法详细攻略

    wkhtmltopdf:wkhtmltopdf(将html转换成pdf的利器)简介.安装.使用方法详细攻略 目录 wkhtmltopdf简介 wkhtmltopdf安装 wkhtmltopdf使用方法 ...

  7. nodejs+html转换pdf,Nodejs 中将html转换成pdf文件

    Nodejs 中将html转换成pdf文件,Nodejs Convert html into pdf 1. 下载phantomjs.exe,将该文件放在根目录 2. 编写pdf.js文件(在githu ...

  8. html压缩包转为pdf格式的,如何将HTML文件快速转换成PDF文件?

    对于某些专业人员来说,偶尔需要把HTML页面转换为PDF格式.尽管在线上有各种将HTML转换到PDF文件的转换器,但在数据保护或转换文档的布局的情况下,这些工具可能无法高质量地完成转换.因此,万兴PD ...

  9. axure8.0导出页面打不开问题_excel怎么转pdf?excel打不开?转换成PDF就行了

    excel转pdf怎么做?年底最后一天了,我都被一堆的Excel文件搞得头疼,在这些时间里,要让我对几个G的文件进行操作,我已经是忙得不可开交,而在最后的最后,我的主管还说他的电脑无法打开我的Exce ...

最新文章

  1. Android事件机制
  2. 【spark】SparkSession的API
  3. Scala入门到精通——第二十七节 Scala操纵XML
  4. Python—实训day7上—Nmupy数值计算基础
  5. Python - 排序( 插入, 冒泡, 快速, 二分 )
  6. 【Python】Python第三方库安装
  7. python读写ini文件的库支持unicode_Python读写unicode文件的方法
  8. 四轮驱动机器人(SSMR)的运动规律
  9. 机器学习中的数学系列
  10. 直播评论发弹幕切图功能点集合
  11. 自然语言处理——分词系统(双向最大匹配)
  12. 复旦退休教授直播间吸烟被举报:此前因违规吸烟被学校要求做检讨
  13. 关于Cookie在顶级域名、二级域名和三级域名之间共享的情况
  14. 七步学习法 —— 如何高效学习一项技能
  15. 在线TSV转JSON工具
  16. 电容笔和触控笔的区别是什么?好用的电容笔测评
  17. OD调试常见断点及原理
  18. 【Android 自定义控件】2.画布的基础使用
  19. 各种不等式的解法收集【初级辅导和中级辅导】
  20. C++实训练习册(C++之基础语法入门练习题)

热门文章

  1. PostgreSQL 编译安装
  2. Activity采用栈式管理的理解
  3. Android开发学习笔记(二)——编译和运行原理(1)
  4. 小程序工程化实践(上篇)-- 手把手教你撸一个小程序 webpack 插件,一个例子带你熟悉 webpack 工作流程...
  5. 【面试】吃透了这些Redis知识点,面试官一定觉得你很NB
  6. spark大批量读取Hbase时出现java.lang.OutOfMemoryError: unable to create new native thread
  7. xmake入门,构建项目原来可以如此简单
  8. Win8Metro(C#)数字图像处理--2.3图像反色
  9. UbuntuKylin技巧
  10. 出现adb连接不上的解决办法(转载)