html转pdf 下载操作

本文介绍如何利用itextpdf插件将HTML内容转换为PDF文件并提供下载功能。文章详细展示了如何配置Maven依赖、处理HTML内容、生成临时HTML文件及最终PDF文件,并设置HTTP响应头来支持不同浏览器环境下的文件下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.基于itexpdf 插件


pom.xml文件添加
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>


/**
 * 下载接口文档pdf
 * @param req
 * @param response
 * @param apiId
 */
@RequestMapping(value = "downloadPdf", method = RequestMethod.GET)
@ResponseBody
public void downloadPdf(HttpServletRequest req, HttpServletResponse response) {
String newsContent = "<div id='pano_camera' class='servie_info1'><h3>111</h3>222</div>";
newsContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
+ newsContent;
// newsContent = newsContent.replaceAll("\240$", "");
// newsContent = "<html><body>123</body></html>";

String path = req.getRealPath("/") + "pdf";

File pathFile = new File(path);    
//如果文件夹不存在则创建
if(!pathFile.exists() && !pathFile.isDirectory()) {
pathFile.mkdir();
}

String filename = "test";
FileOutputStream fos;
try {
fos = new FileOutputStream(path + File.separator + filename + ".html");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(newsContent);
osw.flush();
fos.close();
osw.close();

//通过这个 方法来进行转换
ParseHtmlAsian.createPdf(path, filename);


} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
File file = new File(path + File.separator + filename + ".pdf");
byte[] buf = new byte[1024 * 1024 * 10];
int len = 0;
ServletOutputStream ut = null;
BufferedInputStream br = null;
response.reset();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "must-revalidate, no-transform");
response.setDateHeader("Expires", 0L);
String userAgent = req.getHeader("User-Agent");
boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);
String displayFilename = file.getName();
response.setContentType("application/x-download");
if (isIE) {
try {
displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
} else {
try {
displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
}
try {
br = new BufferedInputStream(new FileInputStream(file));
ut = response.getOutputStream();
while ((len = br.read(buf)) != -1)
ut.write(buf, 0, len);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值