1需求
图表的在线预览和下载
2.实现
1 采用html进行展示,再用js代码进行快照,进行pdf保存。实现简单,无后台,但是图片质量差,不可编辑
感谢https://blog.csdn.net/program_guys/article/details/79035244
2.采用编辑pdf的方式,可以用工具转换成pdf,也可以用itext自定义生成pdf
工具生成pdf
感谢https://blog.csdn.net/wy240036141/article/details/78181265
工具 感谢 https://blog.csdn.net/ddooo_liu/article/details/81004487
列子 感谢 https://blog.csdn.net/lingmao555/article/details/78320689
采用自定义生成pdf'
3.将html文件进行解析保存采用itext的实现。
感谢 https://blog.csdn.net/noaboutfengyue/article/details/45174787#comments
之前一直是中文出不来,后来解决如上。
4.采用现有的软件 wkhtmltopdf
3 代码
package com.jf.itext;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;/**
* @Description
* @Author zengzhiqiang
* @Date 2018年8月24日
*/public class pdf3 {
public static void main(String[] args) throws FileNotFoundException,
Exception {
String htmlFile = "D://workspace//mavenLogin//src//main//java//com//jf//itext//index.html";String pdfFile = "D://workspace//mavenLogin//src//main//java//com//jf//itext//index196.pdf";
// PdfUtils.parseHTML2PDFFile(pdfFile, new FileInputStream(htmlFile));
String ss = "";
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(htmlFile), "UTF-8"));
String t = "";
while ((t = br.readLine()) != null) {
// System.out.println(t);
ss += t;
}
pdf3.parseHTML2PDFFile2(pdfFile, ss);
System.out.println("ok ........");
}public static void parseHTML2PDFFile2(String pdfFile, String html)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(pdfFile));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(html.getBytes("Utf-8")),
Charset.forName("UTF-8"));
document.close();
}
}
pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jf</groupId>
<artifactId>mavenLogin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mavenLogin</name>
<url>http://maven.apache.org</url>
<!-- 定义SpringBoot的版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- SpringBoot web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.0</version>
</dependency>
<!-- 映入itex java操作pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>4aug2000r7-dev</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-light</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>avalon-framework</groupId>
<artifactId>avalon-framework-impl</artifactId>
<version>4.2.0</version>
</dependency>
<!-- pdf -->
</dependencies>
</project>
4结果
谦谦君子 -记录