日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pdf模板定制技术调研

發布時間:2024/3/24 编程问答 68 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pdf模板定制技术调研 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

使用pdf模板 按需求導出pdf報告

技術調研

常見技術
常見的使用itext pdf和spire.pdf操作編輯pdf,使用pdfbox用來讀取pdf

技術問題:

問題1

itext

spire 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 模板引擎"。

  • 新建文檔template.docx,包含文本{{title}}
  • TDO模式:Template + data-model = output
  • //核心API采用了極簡設計,只需要一行代碼 XWPFTemplate template = XWPFTemplate.compile("~/template.docx").render(new HashMap<String, Object>(){{put("title", "Poi-tl 模板引擎"); }}); FileOutputStream out = new FileOutputStream("out_template.docx"); template.write(out); out.flush(); out.close(); template.close();

    文本模板 {{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模板定制技术调研的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。