最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成itextpdf實(shí)現(xiàn)根據(jù)模板動態(tài)生成PDF

 更新時間:2024年03月28日 10:32:46   作者:二進(jìn)制詩人  
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何集成itextpdf實(shí)現(xiàn)根據(jù)模板動態(tài)生成PDF,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下

需求說明

根據(jù)合同模板,將動態(tài)的合同標(biāo)簽,合同方以及合同簽約時間等動態(tài)的生成PDF,供用戶下載打印。

前期準(zhǔn)備

安裝 Adobe Acrobat DC

鏈接:https://pan.baidu.com/s/1T8wTCkxn0YcHjxZQ8G6WeQ

提取碼:j6b9

Spring Boot 集成

添加依賴

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.9</version>
            <scope>compile</scope>
        </dependency>
        <!--中文問題解決-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>       

構(gòu)建工具類

public class PDFUtils {
    private static final Logger log = LoggerFactory.getLogger(PDFUtils.class);

    /**
     * 根據(jù)pdf模板輸出流
     * @param templateFileName 模板文件名
     * @param resultMap 包含文件字段名和值的map
     * @return 生成的文件字節(jié)流
     */
    public static ByteArrayOutputStream createPdfStream(String templateFileName,
                                                        Map<String, String> resultMap){

        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        PdfStamper stamp =null;
        PdfReader reader = null;
        try {


            reader = new PdfReader(templateFileName);
            stamp = new PdfStamper(reader, ba);

            //使用字體
            BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            /* 獲取模版中的字段 */
            AcroFields form = stamp.getAcroFields();

            //填充表單
            if (resultMap != null) {
                for (Map.Entry<String, String> entry : resultMap.entrySet()) {

                    form.setFieldProperty(entry.getKey(), "textfont", bf, null);
                    form.setField(entry.getKey(), entry.getValue()!=null?entry.getValue():"");
                }
            }
            //不能編輯
            stamp.setFormFlattening(true);

        } catch (IOException e) {
            log.error("文檔構(gòu)建I/O異常",e);
        } catch (DocumentException e) {
            log.error("文檔構(gòu)建異常",e);
        }
        finally {
            if(stamp!=null){
                try {
                    stamp.close();
                } catch (DocumentException e) {
                    log.error("流關(guān)閉錯誤",e);
                } catch (IOException e) {
                    log.error("流關(guān)閉錯誤",e);
                }
            }
            if(reader!=null){
                reader.close();
            }
        }
        return ba;
    }
}

構(gòu)建MultipartFile

方便之后上傳OSS返回url

public UploadFileModel createUrl(String filePath, ByteArrayOutputStream byteArrayOutputStream) throws URISyntaxException {
        try{

            byte[] pdfBytes = byteArrayOutputStream.toByteArray();
            MultipartFile multipartFile = new MockMultipartFile(
                    "file",
                    filePath,
                    "application/pdf",
                    pdfBytes
            );

            return uploadFileUtil.upload(multipartFile);
        } catch (Exception e) {
            log.error("創(chuàng)建Url時出錯:" + e.getMessage());
        }
        return null;
    }

編輯PDF模板

Java代碼設(shè)置對應(yīng)form的key-value

pdf模板放在springboot 項(xiàng)目目錄resources/static 目錄下

 public String createContract(CreateContractRequest request) {
        HashMap<String, String> map = new HashMap<>();
        map.put("companyName",request.getCompanyName());
        map.put("phone",request.getPhone());
        UploadFileModel url = null;
        ByteArrayOutputStream pdfStream = PDFUtils.createPdfStream(UserApplication.class.getResource("/").getPath() + "static/contract.pdf", map);
        try {
            url = createUrl("合同.pdf", pdfStream);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        return url.getUrl();
    }

到此這篇關(guān)于SpringBoot集成itextpdf實(shí)現(xiàn)根據(jù)模板動態(tài)生成PDF的文章就介紹到這了,更多相關(guān)SpringBoot itextpdf生成PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

大港区| 新和县| 浮梁县| 宁武县| 镶黄旗| 城市| 龙川县| 山西省| 安远县| 辽阳县| 义乌市| 集安市| 永寿县| 宕昌县| 陇南市| 武平县| 屏东县| 独山县| 大足县| 巴林右旗| 临猗县| 东海县| 宝应县| 瓦房店市| 淮阳县| 松阳县| 淳安县| 海林市| 武乡县| 洛扎县| 扶风县| 龙江县| 阜新| 广丰县| 平顺县| 疏勒县| 屏边| 舞阳县| 六枝特区| 海兴县| 兴化市|