天天看點

使用jacob調用Windows的com對象,進行word、ppt等轉換成ptf、html(二)

富文本轉pdf :

注意:simsun.ttc 可以百度下載下傳:http://www.pc6.com/softview/SoftView_100415.html

package com.orangecds.officeconvert.utils;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.lowagie.text.pdf.BaseFont;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @program: office-convert
 * @ClassName PdfUtilsTest
 * @description:
 * @author: xiyun.zhao
 * @create: 2021-09-27 14:17
 **/
public class PdfUtilsTest {
    /**
     * @description: 将HTML轉成PD格式的檔案html檔案的格式比較嚴格
     * @author xiyun.zhao
     * @param:  htmlFile
     * @param:  pdfFile
     * @return:
     * @date: 2021/9/27 18:50
     */
// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
public static void html2pdf(String htmlFile, String pdfFile) throws Exception {
    // step 1
    String url = new File(htmlFile).toURI().toURL().toString();
    System.out.println(url);
    // step 2
    OutputStream os = new FileOutputStream(pdfFile);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(url);

    // step 3 解決中文支援
    ITextFontResolver fontResolver = renderer.getFontResolver();
    if("linux".equals(getCurrentOperatingSystem())){
        //Linux下的和Windows的不一樣,要自己下載下傳,不要拷貝Windows的到Linux
        fontResolver.addFont("/usr/share/fonts/chiness/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    }else{
        fontResolver.addFont("src/main/resources/dev/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    }

    renderer.layout();
    renderer.createPDF(os);
    os.close();

    System.out.println("create pdf done!!");

}
    /**
     * html轉換成pdf檔案
     *
     * @param htmlContent
     * @throws Exception
     */
    protected static void htmlToPdf(String htmlContent,String pdfFile) throws Exception {
        //1 打開檔案流
        Document document = new Document();
        FileOutputStream fos = new FileOutputStream(pdfFile);
        InputStream is = new ByteArrayInputStream(htmlContent.getBytes(Charset.forName("UTF-8")));
//        InputStream cssIs = new ByteArrayInputStream(getCssFile());
        PdfWriter writer = null;
        try {
            writer = PdfWriter.getInstance(document, fos);
            //3. 設定字型
            XMLWorkerFontProvider fontProvider1 = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
            fontProvider1.register("D:\\hjf\\simsunttc\\simsun.ttc");
            //3 打開文檔
            document.open();
            //4 html轉為pdf
            XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, Charset.forName("UTF-8"), fontProvider1);

        } catch (DocumentException | IOException e) {
            throw new RuntimeException("轉pdf失敗羅~");
        } finally {
            if (null != writer) {
                writer.flush();
            }
            //5 關閉文檔
            document.close();
            fos.close();
            is.close();
            writer.close();
        }
    }


    public static String getCurrentOperatingSystem(){
        String os = System.getProperty("os.name").toLowerCase();
        System.out.println("---------目前作業系統是-----------" + os);
        return os;
    }
    /**
     * 擷取html
     *
     * @return
     */
    protected static String content2Html() {
        String content = COMPLETE_CONTENT;
        content = content.replace("<br>", "<br/>");
        String repContent = content.replaceAll("<img(.+?)>", "<img$1/>");
        System.out.println(""+repContent);
        return repContent;
    }
    /**
     * 擷取樣式檔案
     *
     * @return
     * @throws Exception
     */
    protected byte[] getCssFile() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/css/editor.css");
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1204];
        int len = 0;
        while ((len = fileInputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        fileInputStream.close();
        return outStream.toByteArray();
    }
    /**
     * 拼接CONTENT 使成為完整的html,\"font-family: SimSun;\"
     */
    private static final String COMPLETE_CONTENT = "<html><head></head><body style=\"font-family: SimSun;\"><p class=\"ql-align-center\"><strong>合同内容測試</strong></p><p><strong>甲方(出租方) :</strong></p><p><strong>乙方(承租方) :</strong></p><p>甲乙雙方就乙方租賃甲方電梯轎廂設定看闆媒體(以下簡稱“看闆”)、刊發廣告一</p><p>事進行友好協商,達成以下條款,雙方共同執行:</p><p class=\"ql-align-center\"><strong>第一條租賃地點</strong></p><p>甲方同意将市區花園(大廈/小區)共部電梯租賃給乙方設定看闆,刊發廣告。</p><p class=\"ql-align-center\"><strong>第二條設定方式和範圍</strong></p><p>1、看闆設定于電梯轎廂壁上(超薄型,厚度不超過2cm、長度:、寬度: ;),用</p><p>粘貼或敷膜方式貼在轎廂壁上,共三面(左、中、右),</p><p>2、采用進口美國3m技術貼敷在電梯廳門上; (學生打架調解協定書)</p><p>3、電梯轎廂(面向電梯i ]及出梯門時目光所能觸及的轎廂壁)。</p>" +
            "<p class=\"ql-align-center\"><strong>第三條設定内容</strong></p><p>1、公益廣告(包括社群文化建設、公民道德規範、防火、防盜知識、保健小常識、</p><p>生活小竅門等) ;</p>" +
            "<p>2、商業廣告(廣告内容必須符合國家廣告法的規定,不得釋出不宜機關、社群和家<img src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic16.nipic.com%2F20111008%2F5203963_093910733000_2.jpg&refer=http%3A%2F%2Fpic16.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1635329488&t=9d73780cedba58d89b052512d894955c\" style=\"max-width:100%;\"></p><p>庭的廣告,更不得釋出影響少年兒童身心健康的廣告) ;</p><p class=\"ql-align-right\"><br></p></body></html>";

    public static void main(String[] args) {

        String htmlFile = "D:\\hjf\\u.html";
        String pdfFile = "D:/hjf/z.pdf";
        try {
            //html以檔案形式轉pdf
//            PdfUtilsTest.html2pdf(htmlFile, pdfFile);
            //以字元串形式轉pdf
            PdfUtilsTest.htmlToPdf(content2Html(),pdfFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}      
poi

繼續閱讀