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

富文本轉pdf :

注意:simsun.ttc 可以百度下載://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" "//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=\"//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();
}
}


}

 

Tags: