将html文档转成pdf
(1)使用场景:在项目中使用到了合同,只有在合同的头部,是不相同的。在合同的主体部分都是相同的,因此就把他放到了模板(html文件)里面。
在用户线上签约完成之后,可以将pdf版的合同下载。
(2)需求:由上可以看出,首先需要把html文件转成pdf。其次需要将pdf下载。
(3)code:
1)在上午就顺利的找了一段合适的代码,成功的跑起来了,在参数里面需要传递一个html文件,因为我测试的是一个类似于hello word的简单的html,所以就侥幸成功了。可能是找的过程太顺利了!!!结果将html换成项目中的文件后死后调不出来,百度才知道,这种方式只能将标准格式的html转成pdf。这里所谓的标准,,标准到表态。。此方法,弃~【但是怎么也是费劲调出来的,就也贴出来吧】、
原文地址:https://blog.csdn.net/m15732622413/article/details/78456961?utm_source=blogxgwz1
package com.jeeplus.common.utils; 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.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer; import com.jeeplus.core.web.BaseController;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.template.PebbleTemplate; /**
* 下载PDF文件
*
* @author xiao
*
*/
@Controller public class PDFUtil extends BaseController {
private static final String FONTPATH = "C:/WINDOWS/Fonts/simsun.ttc";// 支持中文字体(放哪里都行) public static String exportPdf(String htmlpath, HttpServletResponse response, HttpServletRequest request)
throws Exception {
String html = htmlpath;
String pdf = html2Pdf(html, "D:/测试2.pdf");
// 如果在控制类有response对象可以直接转换后的pdf文件,在控制类方法需要return null
downloadFile(pdf, "我的PDF文件.pdf", response, request);
// return null;
System.out.println("create html success! 文件存放路径:" + pdf);
return "导出成功!";
} /**
* html转换pdf文件 注:支持中文,目前iText只支持上面FONTPATH定义的这种字体,所以html文件中也需要用样式设置字体为:SimSun
* htmlPath 需要转换的html源文件 pdfPath 转换后pdf文件存放地址
*/
public static String html2Pdf(String htmlPath, String pdfPath) {
try {
String url = new File(htmlPath).toURI().toURL().toString();
OutputStream output = new FileOutputStream(pdfPath);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url); // 解决中文支持问题(html的中文必须用SimSun字体,Java只能支持这1种字体)
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(FONTPATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(output);
output.close(); return pdfPath;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (DocumentException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
} /**
* 文件下载-支持中文名称
*
* @param sourcePath下载文件全路径(F:/test.pdf)
* @param fileName需要生成的下载文件名(HTML转PDF测试.pdf)
* @param response
* @throws Exception
*/
public static void downloadFile(String sourcePath, String fileName, HttpServletResponse response,
HttpServletRequest request) throws Exception {
// 读到流中
InputStream inStream = null;
try {
inStream = new FileInputStream(sourcePath);// 文件的存放路径
// 设置输出的格式
response.reset();
String name = new String((fileName));
response.addHeader("Content-Disposition",
"attachment; filename=" + new String(name.getBytes(), "iso-8859-1"));
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
response.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
response.getOutputStream().close();
// 删除源文件
/*
* File sourceFile = new File(sourcePath); if(sourceFile.exists()){
* sourceFile.delete(); }
*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(4)在网上看到了这个比较,很清晰 原文:https://blog.csdn.net/huyuyang6688/article/details/79710704?utm_source=blogxgwz0
将html文档转成pdf的更多相关文章
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- C#实现文档转换成PDF
网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...
- 用java将简单的word文档换成pdf文档
用java将简单的word文档换成pdf文档的方式很多,因为很多都没有实际测试过,所以这里就先泛泛的说一下 整体上来看分两种: 1.纯java代码实现,有很多优秀的开源软件可以用,比如poi,itex ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
- Java实现批量将word文档转换成PDF
先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...
- Python将word文档转换成PDF文件
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
- asp.net将ppt文档转换成pdf
一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法 C# 代码 复制 // ...
- C# word文档转换成PDF格式文档
最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...
随机推荐
- webpack 引入 html-webpack-plugin 报错
配置webpack当中,出现一个问题: 引入html-webpack-plugin 插件报错. 这时需要本地(也就是当前项目下)安装一下webpack就可以解决问题了. 注意:现在是webpack4版 ...
- es6+最佳入门实践(13)
13.模块化 13.1.什么是模块化 模块化是一种处理复杂系统分解为更好的可管理模块的方式.通俗的讲就是把一个复杂的功能拆分成多个小功能,并且以一种良好的机制管理起来,这样就可以认为是模块化.就像作家 ...
- 利用java.lang.reflect.Constructor动态实例化对象
} } } } } Student t = co ...
- [51nod] 1305 Pairwise Sum and Divide 数学
有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = ...
- 【BZOJ】5010: [Fjoi2017]矩阵填数
[算法]离散化+容斥原理 [题意]给定大矩阵,可以每格都可以任意填1~m,给定n个子矩阵,要求满足子矩阵内的最大值为vi,求方案数. n<=10,h,w<=1w. [题解] 此题重点之一在 ...
- 微信小程序登录状态
我们知道,WEB服务器通过浏览器携带的cookie获取session来判断是否是同一用户(或浏览器):Restful服务通过客户端传过来唯一ID,来识别调用用户. >为什么需要维护登录态? 有自 ...
- usaco 2000 contest 滑雪
2013-09-11 10:22 [题目大意]给定N个点的高度和M条相连的路线(单向),从最高点向下走, 到无法走时为一条路径,求不同的路径数,(一节点不同就叫不同) [输入样例] 4 5 (N, ...
- Python学习笔记 - day4 - 流程控制
Python流程控制 Python中的流程控制主要包含两部分:条件判断和循环. Python的缩进和语法 为什么要在这里说缩进和语法,是因为将要学习的条件判断和分支将会涉及到多行代码,在java.c等 ...
- shell脚本复制文件夹内容到另外的文件夹,如果存在则自动备份
有时我们需要将一个文件夹覆盖到我们的工作目录,但需要自动备份已经存在的文件,一个一个去备份太麻烦了,全部备份又没有必要.shell脚本可以很好滴完成这个任务.原文链接http://back.zhizh ...
- Bash Shell 下打开一个TCP / UDP SOCKET
Bash Shell 下打开一个TCP / UDP SOCKET http://jingyan.baidu.com/article/636f38bb6166c3d6b84610d1.html