Java利⽤wkhtmltopdf实现Html转PDF,IMAGE【⾮常好⽤】前⾔:java实现html转pdf 是⾮常困难的,⽹上很多种⽅式对⽐了下很多坑,,效果⾮常不理想
通过wkhtmltopdf 实现效果⾮常完美,可以html转pdf,也可以转图⽚。
wkhtmltopdf ⼯具
官⽹链接:
windos版本
⾃⾏下载对应版本,解压后⽬录如下
C:\toms\soft\wkhtmltox\bin& file:///C:/Users/jijun.tang/Desktop/b.html
C:\Users\jijun.tang\Desktop\d1.pdf
通过exe 调⽤b.html⽂件转换成d1.pdf
docker安装
FROM ⾃⼰的基础进⾏
#需要root不然安装会提⽰⽆权限
USER root
COPY test-SNAPSHOT.jar /data/webapps
#⾃⾏下载linux版本的放在⼯程中 wkhtmltox-0.s7.x86_64.rpm
COPY target/classes/wkhtmltox-0.s7.x86_64.rpm /data/wkhtmltox/
WORKDIR /data/wkhtmltox/
RUN yum -y install fontconfig libX11 libXext libXrender libjpeg openssl xorg-x11-fonts-75dpi xorg-x11-fonts-Type1 RUN rpm -ivh wkhtmltox-0.s7.x86_64.rpm
#安装字体不然pdf会乱码
RUN yum groupinstall Fonts -y
slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author 重庆阿汤哥
* @Description: pdf⽂件下载⼯具类
* @date 2021/12/27 9:28
*/
@Slf4j
public class PdfUtils {
//public static final String toPdfTool = "C:\\toms\\soft\\wkhtmltox\\bin\\";
public static final String toPdfTool = "wkhtmltopdf";
/**
* html转pdf
*
* @param toPdfTool 转换⼯具类路径
* @param srcPath html路径,可以是硬盘上的路径,也可以是⽹络路径
* @param destPath pdf保存路径
* @return 转换成功返回true
*/
public static boolean convert(String toPdfTool, String srcPath, String destPath) {
StringBuilder cmd = new StringBuilder();
cmd.append(toPdfTool);
cmd.append(" ");
cmd.append(" --header-line");//页眉下⾯的线
cmd.append(" --header-center 这⾥是页眉这⾥是页眉这⾥是页眉这⾥是页眉 ");//页眉中间内容
cmd.append(" --header-spacing 10 ");// (设置页眉和内容的距离,默认0)
cmd.append(srcPath);
cmd.append(" ");
cmd.append(destPath);
boolean result = true;
try {
Process proc = Runtime().String());
Process proc = Runtime().String());
HtmlToPdfInterceptor error = new ErrorStream());
HtmlToPdfInterceptor output = new InputStream());
error.start();
output.start();
proc.waitFor();
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
/**
* @Author 阿汤哥
* @Description 逻辑:现将内容转为⼀个html⽂件,在输出pdf
* @Date 2021/12/28 17:58
* @Param content html内容
* @Param srcPath 来源⽂件.html
* @Param destPath ⽬标⽂件pdf
* @return java.io.InputStream
**/
public static InputStream getFile(String content, String srcPath, String destPath) {
try {
Files.(srcPath), Bytes(BrainConstants.UTF8));
File destPathFile = new File(destPath);
File parent = ParentFile();
//如果pdf保存路径不存在,则创建路径
if (!ists()) {
parent.mkdirs();
java修改html文件}
FileInputStream fileInputStream = new FileInputStream(destPath);
ists()){
destPathFile.delete();
}
return fileInputStream;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* @Author 阿汤哥
* @Description 下载返回浏览器
* @Date 2021/12/28 17:57
* @Param [stream, fileName, response]
* @return void
**/
public static void download(InputStream stream, String fileName, HttpServletResponse response) {
try (BufferedInputStream bufin = new BufferedInputStream(stream);
OutputStream outputStream = OutputStream()) {
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=\"" + de(fileName, "utf-8") + "\""); byte[] temp = new byte[1024];
int size = 0;
while ((size = ad(temp)) != -1) {
outputStream.write(temp, 0, size);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@GetMapping("/dowlodPdf")
@ApiOperation(value = "下载pdf", notes = "下载pdf")
public void dowlodPdf( HttpServletResponse response) {
/
/把需要导出的pfd内容放⼊str
StringBuffer str = new StringBuffer();
InputStream stream = String(), srcPath, destPath);
PdfUtils.download(at(".pdf"),response);
}
希望该笔记对同学们有⽤,如果过程中有什么问题欢迎给我留⾔,记得多关注点赞哟
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论