java 使用itextpdf工具实现HTML转PDF文件

java 使用itextpdf工具实现HTML转PDF文件

本文转载https://my.oschina.net/960823/blog/1588166内容
demo

  • maven依赖包
        <dependency>
           <groupId>com.itextpdf</groupId>
           <artifactId>itextpdf</artifactId>
           <version>5.5.9</version>
       </dependency>
       <dependency>
           <groupId>com.itextpdf.tool</groupId>
           <artifactId>xmlworker</artifactId>
           <version>5.5.9</version>
       </dependency>

       <dependency>
           <groupId>com.itextpdf</groupId>
           <artifactId>itext-asian</artifactId>
           <version>5.2.0</version>
       </dependency>

       <dependency>
           <groupId>org.xhtmlrenderer</groupId>
           <artifactId>flying-saucer-pdf-itext5</artifactId>
           <version>9.0.3</version>
       </dependency>
  • java 代码
package com.my.utils;

import com.itextpdf.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
* Utils - html转换成PDF
*/
public class HtmlToPdfUtils {

   private HtmlToPdfUtils() {}

   /**
    * html转换成PDF
    * @param htmlFile html文件
    * @param pdfPath  pdf路径
    * @throws Exception 异常
    */
   public static void htmlToPdf(File htmlFile, String pdfPath) throws Exception {

       OutputStream os = new FileOutputStream(pdfPath);
       ITextRenderer iTextRenderer = new ITextRenderer();
       iTextRenderer.setDocument(htmlFile);

       //解决中文编码
       ITextFontResolver fontResolver = iTextRenderer.getFontResolver();
       if ("linux".equals(getCurrentOperationSystem())) {
           fontResolver.addFont("/usr/share/fonts/chiness/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       } else {
           fontResolver.addFont("c:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
       }

       iTextRenderer.layout();
       iTextRenderer.createPDF(os);
       os.flush();
       os.close();
   }

   private static String getCurrentOperationSystem() {
       String os = System.getProperty("os.name").toLowerCase();
       return os;
   }

   public static void main(String[] args){
       File file = new File("E:\\test.html");
       String pdfPath = "E:\\testPdf.pdf";
       try{
           htmlToPdf(file, pdfPath);
       }catch (Exception e){
           e.printStackTrace();
       }
   }
}

你可能感兴趣的:(java,web,springMVC)