1. 程式人生 > >圖表的線上預覽和java 生成pdf下載,itext問題總結

圖表的線上預覽和java 生成pdf下載,itext問題總結

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'

https://blog.csdn.net/Darkjazz11/article/details/79647252

 

3.將html檔案進行解析儲存採用itext的實現。

感謝  https://blog.csdn.net/noaboutfengyue/article/details/45174787#comments

之前一直是中文出不來,後來解決如上。

4.採用現有的軟體 wkhtmltopdf

https://blog.csdn.net/qq_14873105/article/details/51394026

 

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結果

 謙謙君子 -記錄