基於libreoffice實現的文檔轉換項目,無框架依賴,即插即用html
項目源代碼:github/workable-convertergit
CentOS請直接參考這篇文章:CentOS7安裝LibreOffice6.2.3github
windows跟Mac一樣能夠在上述文章中拿到下載連接apache
安裝完成後,請記住您的LibreOffice的Home目錄,後面須要用到bootstrap
默認目錄:windows
<dependency>
<groupId>com.liumapp.workable.converter</groupId>
<artifactId>workable-converter</artifactId>
<version>v1.2.0</version>
</dependency>複製代碼
compile group: 'com.liumapp.workable.converter', name: 'workable-converter', version: 'v1.2.0'複製代碼
在項目的resources目錄下,建立一個yml配置文件,須要確保文件名稱爲application.yml、bootstrap.yml或workable-converter.yml三種命名任意一個便可api
添加如下配置:服務器
com:
liumapp:
workable-converter:
libreofficePath: "/Applications/LibreOffice.app/Contents"複製代碼
libreofficePath的值爲LibreOffice:6.2.3的安裝目錄markdown
完整的配置項列表以下app
參數名 | 解釋 | 默認值 |
---|---|---|
libreofficePath | LibreOffice安裝目錄 | (String) 無默認值,該項必填 |
libreofficePort | LibreOffice監聽端口 | (int) 2002 |
tmpPath | 臨時存儲目錄 | (String) "./data/" |
以doc轉PDF爲例
WorkableConverter converter = new WorkableConverter();//實例化的同時,初始化配置項,配置項的校驗經過Decorator裝飾
ConvertPattern pattern = ConvertPatternManager.getInstance();
pattern.fileToFile("./data/test.doc", "./data/pdf/result1.pdf"); //test.doc爲待轉換文件路徑,result1.pdf爲轉換結果存儲路徑
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.DOC);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);
converter.setConverterType(CommonConverterManager.getInstance());//策略模式,後續實現了新的轉換策略後,在此處更換,圖片轉換將考慮使用新的策略來完成
boolean result = converter.convert(pattern.getParameter();複製代碼
若是要用html轉PDF,將上述代碼的
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.DOC);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);複製代碼
改成
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.HTML);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);複製代碼
其餘類型的同理
以doc轉pdf爲例
// you can also choice not use proxy
WorkableConverter converter = new WorkableConverter();
ConvertPattern pattern = ConvertPatternManager.getInstance();
pattern.streamToStream(new FileInputStream("./data/test.doc"), new FileOutputStream("./data/pdf/result1_2.pdf"));
// attention !!! convert by stream must set prefix.
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.DOC);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);
converter.setConverterType(CommonConverterManager.getInstance());
boolean result = converter.convert(pattern.getParameter();複製代碼
跟上例基本相同,惟一的變化是經過pattern.streamToStream()來設置輸入輸出流,轉換源文件數據從輸入流中讀取,轉換結果會直接寫入輸出流中,
同時要切換轉換格式,跟上例同樣設置不一樣的prefix便可
仍以doc轉pdf爲例
WorkableConverter converter = new WorkableConverter();
ConvertPattern pattern = ConvertPatternManager.getInstance();
pattern.base64ToBase64(Base64FileTool.FileToBase64(new File("./data/test.doc")));
// attention !!! convert by base64 must set prefix.
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.DOC);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);
converter.setConverterType(CommonConverterManager.getInstance());
boolean result = converter.convert(pattern.getParameter();
String destBase64 = pattern.getBase64Result();複製代碼
輸入base64執行轉換,首先經過pattern.base64ToBase64()來設置轉換源的base64值
轉換結果result仍然是一個boolean類型,經過pattern.getBase64Result來獲取轉換結果的base64值
要切換轉換格式,跟上例同樣設置不一樣的prefix便可
目前對於圖片的處理,只支持將PDF轉PNG圖片(若是1份pdf文件有20頁,那麼將會轉換爲20張png圖片),該功能的實現基於PDFBox:2.0.12
pattern.fileToFiles()第一個參數爲待轉換的pdf文件路徑,第二個參數爲轉換後的圖片存儲路徑
WorkableConverter converter = new WorkableConverter();
ConvertPattern pattern = ConvertPatternManager.getInstance();
pattern.fileToFiles("./data/test5.pdf", "./data/");
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PNG);
converter.setConverterType(PdfBoxConverterManager.getInstance()); // pdf box converter manager only support pdf to png
assertEquals(true, converter.convert(pattern.getParameter()));
assertEquals(true, FileTool.isFileExists("./data/test5_0.png"));
assertEquals(true, FileTool.isFileExists("./data/test5_1.png"));
assertEquals(true, FileTool.isFileExists("./data/test5_2.png"));
assertEquals(true, FileTool.isFileExists("./data/test5_3.png"));複製代碼
pattern.base64ToBase64()的參數爲待轉換pdf文件的base64值
轉換結束後,經過List
獲取轉換後的圖片base64值的集合
WorkableConverter converter = new WorkableConverter();
ConvertPattern pattern = ConvertPatternManager.getInstance();
pattern.base64ToBase64(Base64FileTool.FileToBase64(new File("./data/test5.pdf")));
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PNG);
converter.setConverterType(PdfBoxConverterManager.getInstance()); // pdf box converter manager only support pdf to png
boolean result = converter.convert(pattern.getParameter());
List<String> resultBase64 = pattern.getBase64Results();
assertEquals(true, result);
assertEquals(4, resultBase64.size());複製代碼
水印的轉換策略爲WaterMarkConverter
添加水印注意事項
好比說,要添加透明度爲0.3的文本做爲水印的話,本身使用word等工具繪製透明度爲0.3的字體(或者上包含透明度的png圖片也能夠)並另存爲一個watermark.pdf文件複製代碼
而後使用waterMarkRequire.setWaterMarkPDFBase64(Base64FileTool.FileToBase64(new File("./data/watermark.pdf")))複製代碼
或者waterMarkRequire.setWaterMarkPDFBytes(FileUtils.readFileToByteArray(new File("./data/watermark.pdf")))將該文件的base64或者bytes值輸入便可複製代碼
具體使用能夠分爲三種方式
WorkableConverter converter = new WorkableConverter();
converter.setConverterType(WaterMarkConverterManager.getInstance());//選擇具體的水印轉換策略
ConvertPattern pattern = ConvertPatternManager.getInstance();
WaterMarkRequire waterMarkRequire = new WaterMarkRequire();//建立水印所須要的參數
//指定在具體的哪一頁添加水印,0的話則在全部頁面添加水印
waterMarkRequire.setWaterMarkPage(0);//0 means all age
waterMarkRequire.setWaterMarkPDFBase64(Base64FileTool.FileToBase64(new File("./data/watermark.pdf")));
pattern.setWaterMarkRequire(waterMarkRequire);
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.fileToFile("./data/test5.pdf", "./data/test5_with_mark01.pdf");//添加水印後的文件保存在./data/目錄下,名爲test5_with_mark01.pdf
boolean result = converter.convert(pattern.getParameter());
assertEquals(true, result);複製代碼
WorkableConverter converter = new WorkableConverter();
converter.setConverterType(WaterMarkConverterManager.getInstance());
ConvertPattern pattern = ConvertPatternManager.getInstance();
WaterMarkRequire waterMarkRequire = new WaterMarkRequire();
waterMarkRequire.setWaterMarkPage(0);//0 means all age
waterMarkRequire.setWaterMarkPDFBytes(FileUtils.readFileToByteArray(new File("./data/watermark.pdf")));
pattern.setWaterMarkRequire(waterMarkRequire);
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.streamToStream(new FileInputStream("./data/test5.pdf"), new FileOutputStream("./data/test5_with_mark02.pdf"));
boolean result = converter.convert(pattern.getParameter());
assertEquals(true, result);複製代碼
WorkableConverter converter = new WorkableConverter();
converter.setConverterType(WaterMarkConverterManager.getInstance());
ConvertPattern pattern = ConvertPatternManager.getInstance();
WaterMarkRequire waterMarkRequire = new WaterMarkRequire();
waterMarkRequire.setWaterMarkPage(0);//0 means all age
waterMarkRequire.setWaterMarkPDFBase64(Base64FileTool.FileToBase64(new File("./data/watermark.pdf")));
pattern.setWaterMarkRequire(waterMarkRequire);
pattern.setSrcFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.setDestFilePrefix(DefaultDocumentFormatRegistry.PDF);
pattern.base64ToBase64(Base64FileTool.FileToBase64(new File("./data/test5.pdf")));
boolean result = converter.convert(pattern.getParameter());
String base64Result = pattern.getBase64Result();
Base64FileTool.saveBase64File(base64Result, "./data/test5_with_mark03.pdf");
assertEquals(true, result);複製代碼