将html生成pdf
使用itext
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
java代码
public void downPdf() {
PdfUtils pdfUtils = new PdfUtils();
//html文件路径
String htmlFilePath = "/testechart.html";
// 中文字体存储路径
String chineseFontPath = "/simsun.ttc";
String fileName = "2.pdf";
OutputStream outputStream;
outputStream = null;
try {
// 防止中文乱码
fileName = URLEncoder.encode(fileName, "UTF-8");
//生成pdf文件
pdfUtils.html2pdf(htmlFilePath,"/2.pdf",chineseFontPath);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import com.lowagie.text.pdf.BaseFont;
import lombok.extern.slf4j.Slf4j;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.net.MalformedURLException;
/**
* @author :zyt
* @date :Created in 2020-06-24 11:12
* @description:
* @modified By:
* @version: 1.0.0
*/
@Slf4j
public class PdfUtils {
public static void html2pdf(String htmlFile, String pdfFile, String chineseFontPath) {
// step 1
String url;
OutputStream os = null;
try {
url = new File(htmlFile).toURI().toURL().toString();
os = new FileOutputStream(pdfFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
// 解决中文不显示问题
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
} catch (MalformedURLException e) {
System.out.println(e.toString());
} catch (FileNotFoundException e) {
System.out.println(e.toString());
} catch (com.lowagie.text.DocumentException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
}
普通html都可以转,大家可以手动试试,但是有一个问题,echart类型的图表不能直接转,因为echart本身不支持导出pdf,只有将echart在网页上渲染过之后,才可以获取渲染后的数据再生成pdf。
尝试将html生成图片
既然echart不能生成pdf,那么是否可以生成图片呢?尝试一下吧,代码如下
<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>0.9</version>
</dependency>
public void downImage() throws FileNotFoundException, MalformedURLException {
//html文件路径
String htmlFilePath = "/testechart3.html";
//imageHtml为获取的html源码字符串
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
URL url = new File(htmlFilePath).toURI().toURL();
String imageName = "/echart"+".png";
imageGenerator.loadUrl(url);//也可以根据html url引用 loadUrl的方式加载
//Thread.sleep(1000); //有时会有加载图片延迟,因此这里设置下延时
imageGenerator.getBufferedImage();
//Thread.sleep(2000);
imageGenerator.saveAsImage(imageName);
}
经过尝试,echart依然不能生成图片,唯一使用echart生成图片的方式为
https://blog.csdn.net/tjj3027/article/details/80421170?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-9.nonecase
从前端获取echart的base64码,传回后端再生成图片。
使用wkhtmltox
这个是个额外的软件,windows,mac os,linux的版本都有,官网地址为:https://wkhtmltopdf.org/downloads.html
我尝试过mac os和linux CentOS7的,效果都还不错
mac os的直接下载安装包,安装即可。
linux的需要的依赖则比较多,步骤如下,官网上面比较难下载,我会把我用到的安装包都放在我的资源中,大家可以自行获取。
1.安装
rpm -ivh wkhtmltox-0.12.6-1.centos7.x86_64.rpm
2.安装依赖字体
yum install xorg-x11-fonts-75dpi.noarch
3.生成pdf
报错:/lib64/libcrypto.so.10: version `OPENSSL_1.0.2’ not found (required by wkhtm)
升级OPENSSL版本
4.强迫安装
rpm -ivh openssl-libs-1.0.2k-12.el7.x86_64.rpm —force
5.导出全部乱码
sudo yum install urw-fonts
数字可以显示
6.中文没有展示
拷贝simsun.ttc到centos的/usr/share/fonts/