pdf模板定制技术调研
背景
使用pdf模板 按需求導出pdf報告
技術調研
常見技術
常見的使用itext pdf和spire.pdf操作編輯pdf,使用pdfbox用來讀取pdf
技術問題:
| 20220530 | 文本替換 | 文本替換,使用遮罩層功能,但是若替換的問題過長,需要再模板上做留白處理,否則會使文本疊加顯示 | 直接支持替換功能,但是僅會顯示替換字符占位的坐標位置,超出部分直接不顯示 |
| 表格替換 |
其他技術
1.借助模板技術:使用thymeleaf,freemarker或者word方式創建模板,然后將其生成pdf
2.使用pdfbox讀取pdf模板,然后使用itext創建新的pdf文件
最終技術:word模板-word-》pdf
word模板:poi-tl:https://gitee.com/luffyuan/poi-tl
word處理技術:poi
word轉pdf:xdocreport:fr.opensagres.poi.xwpf.converter.pdf
poi-tl介紹:
文檔:??????Poi-tl Documentation
poi-tl技術調研對比
| Poi-tl | Java跨平臺 | Word模板引擎,基于Apache POI,提供更友好的API | 低代碼,準備文檔模板和數據即可 |
| Apache POI | Java跨平臺 | Apache項目,封裝了常見的文檔操作,也可以操作底層XML結構 | 文檔不全,這里有一個教程:Apache POI Word快速入門 |
| Freemarker | XML跨平臺 | 僅支持文本,很大的局限性 | 不推薦,XML結構的代碼幾乎無法維護 |
| OpenOffice | 部署OpenOffice,移植性較差 | - | 需要了解OpenOffice的API |
| HTML瀏覽器導出 | 依賴瀏覽器的實現,移植性較差 | HTML不能很好的兼容Word的格式,樣式糟糕 | - |
| Jacob、winlib | Windows平臺 | - | 復雜,完全不推薦使用 |
基本語法
所有的語法結構都是以 {{ 開始,以 }} 結束。
從一個超級簡單的例子開始:把{{title}}替換成"Poi-tl 模板引擎"。
文本模板 {{var}}
TextRenderData或String數據模型,繼承模板樣式的同時,也可以自定義顏色、字體等樣式。
Map<String, Object> datas = new HashMap<String, Object>(); datas.put("author", new TextRenderData("00FF00", "Sayi卅一")); datas.put("introduce", "http://www.deepoove.com");圖片模板 {{@var}}
//本地圖片 put("localPicture", new PictureRenderData(120, 120, "src/test/resources/sayi.png")); //本地圖片byte數據 put("localBytePicture", new PictureRenderData(100, 120, ".png", BytePictureUtils.getLocalByteArray(new File("src/test/resources/logo.png"))));表格模板 {{#var}}
RowRenderData header = RowRenderData.build(new TextRenderData("FFFFFF", "姓名"), new TextRenderData("FFFFFF", "學歷")); RowRenderData row = RowRenderData.build(new TextRenderData("張三"), new TextRenderData("1E915D", "研究生")); put("table", new MiniTableRenderData(header, Arrays.asList(row)));列表模板 {{*var}}
put("feature", new NumbericRenderData(new ArrayList<TextRenderData>() {{add(new TextRenderData("Plug-in grammar, add new grammar by yourself"));add(new TextRenderData("Supports word text, header, footer..."));add(new TextRenderData("Templates, not just templates, but also style templates"));} }));xdocreport介紹
fr.opensagres.poi.xwpf.converter.pdf
xdocreport是一個java api,它能夠根據ms office(docx或者ppt)以及open office的odt創建的xml文檔模板,與java模型一起配合生成你想要的docx或者odt文檔報告。
2.同時你可以把生成的docx或者odt文檔轉換你想要的其它格式,例如pdf,xhtml等。
xdocreport根據模板生成合同(docx/pdf)神器:(一)介紹_熱水鐘的博客-CSDN博客_xdocreport
對比:word轉pdf技術有?org.jodconverter和jacob? ,這兩個技術對一般在window平臺上可用,對linux的支持一般,并且要求在os上安裝對應插件(office插件)輔助完成
實際代碼
maven引用
<!--poi word模板生成--> <dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.8.2</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>4.1.2</version> </dependency> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version> </dependency> <!--word 轉pdf--> <dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId><version>2.0.2</version><exclusions><exclusion><artifactId>poi-ooxml</artifactId><groupId>org.apache.poi</groupId></exclusion></exclusions> </dependency>代碼:
poi-tl/PaymentExample.java at dev-1.8.0 · Sayi/poi-tl · GitHub
我用的poi-tl是低版本的,若是感興趣可以用官網最新版本1.10
總結
以上是生活随笔為你收集整理的pdf模板定制技术调研的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 各滤镜处理效果记录,
- 下一篇: pytorch一直安装失败?