国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式

這篇具有很好參考價(jià)值的文章主要介紹了Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

使用 poi-tl 根據(jù)模板生成 word 文件。
使用 xdocreport 將 docx 文件轉(zhuǎn)換為 pdf 文件。

xdocreport 也支持根據(jù)模板導(dǎo)出 word ,但是 poi-tl 的功能更齊全,操作更簡單,文檔清晰。
poi-tl 、xdocreport 內(nèi)部均依賴了 poi ,要注意兩者中 poi 和 自身項(xiàng)目引用的 poi 的版本是否存在沖突。

Pom 依賴

使用 poi 5.2.2 ,poi-tl 1.12.1 ,xdocreport 2.0.3

<!--        poi依賴-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>
<!--        poi-tl依賴-->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.12.1</version>
        </dependency>
<!--        xdocreport依賴-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-full</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>

生成 DOCX 文件

創(chuàng)建 DOCX 模板

根據(jù)官方文檔按要求創(chuàng)建模板,并放在resources文件夾下。官方文檔 http://deepoove.com/poi-tl/。
Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式,spring boot,pdf,后端
Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式,spring boot,pdf,后端

生成 DOCX 文檔

創(chuàng)建模板填充類

其實(shí)也可以在生成文件時(shí)使用 Map 類型的方式填充文件內(nèi)容,如下

XWPFTemplate.compile(inputStream).render(
  new HashMap<String, Object>(){{
    put("title", "Hi, poi-tl Word模板引擎");
}});

但是使用實(shí)體類更規(guī)范一些。

@Data
public class ChildRoundsProvalDocxEntity {
    private String childName;//孩子姓名
    private String identify;//身份證號(hào)
    private String gender;//性別
    private String childRounds;//孩次
    private PictureRenderData avatar;//頭像地址
    private String entryDate;//入院時(shí)間
    private String gardenMonths;//孩次
    private String charge;//每月收費(fèi)
    private String fatherName;//父親姓名
    private String fatherIdentify;//父親身份證號(hào)
    private String motherName;//母親姓名
    private String motherIdentify;//母親身份證號(hào)
    private String address;//住址及聯(lián)系方式
    private String firstChildName;//第一個(gè)子女姓名
    private String firstChildIdentify;//第一個(gè)子女身份證號(hào)
    private String secondChildName;//第二個(gè)子女姓名
    private String secondChildIdentify;//第二個(gè)子女身份證號(hào)
    private String signDate;//簽署日期
}

生成文件(多種方式)

1、生成一個(gè)文件輸入流,方便用于再次編輯
不要在方法內(nèi)部將輸入流關(guān)閉

/**
 * 生成文件到輸入流中
 * @param templatePath
 * @param data
 * @return
 */
public InputStream createWordFile1(Object data){
    Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");
    XWPFTemplate template = null;
    InputStream resultStream = null;
    try {
        // word模板填充
        InputStream inputStream = templateFile.getInputStream();
        template = XWPFTemplate.compile(inputStream).render(data);
        resultStream = PoitlIOUtils.templateToInputStream(template);
        PoitlIOUtils.closeQuietlyMulti(template);
    } catch (Exception e) {
        log.error("導(dǎo)出失敗,異常原因:" + e.getMessage());
    } finally {
        try {
            if (template != null) {
                template.close();
            }
        } catch (Exception e) {
            log.error("流關(guān)閉失敗,異常原因:" + e.getMessage());
        }
    }

    return resultStream;
}

2、生成docx到輸出流中,一般是在網(wǎng)絡(luò)響應(yīng)中直接輸出,瀏覽器去下載

public void createWordFile2(Object data, HttpServletResponse httpServletResponse){
	//獲取模板信息
	Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");
	XWPFTemplate template = null;
	docName = URLEncoder.encode(docName, StandardCharsets.UTF_8);
	try {
		httpServletResponse.setContentType("application/octet-stream");
        httpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + docName + ".docx");
        httpServletResponse.addHeader("filename", docName);
	    // word模板內(nèi)容填充
	    InputStream inputStream = templateFile.getInputStream();
	    template = XWPFTemplate.compile(inputStream).render(data);
	    OutputStream out = httpServletResponse.getOutputStream();//要記得關(guān)閉
	    BufferedOutputStream bos = new BufferedOutputStream(out);//要記得關(guān)閉
	    template.write(bos);
	    bos.flush();
	    out.flush();
	    PoitlIOUtils.closeQuietlyMulti(template, bos, out);
	} catch (Exception e) {
	    log.error("導(dǎo)出失敗,異常原因:" + e.getMessage());
	    throw new BaseException("Word文檔生成失敗");
	} finally {
	    try {
	        if (template != null) {
	            template.close();
	        }
	    } catch (Exception e) {
	        log.error("流關(guān)閉失敗,異常原因:" + e.getMessage());
	    }
	}
}

3、直接生成文件到指定路徑

public void createWordFile3(Object data, String path){
	//獲取模板信息
	Resource templateFile = resourceLoader.getResource("classpath:wordtemplate/childRoundsProval.docx");
	XWPFTemplate template = null;
	try {
	    // word模板內(nèi)容填充
	    InputStream inputStream = templateFile.getInputStream();
	    template = XWPFTemplate.compile(inputStream).render(data);
	    template.writeToFile(path);//文件夾路徑必須存在 可以提前創(chuàng)建
	    PoitlIOUtils.closeQuietlyMulti(template);
	} catch (Exception e) {
	    log.error("導(dǎo)出失敗,異常原因:" + e.getMessage());
	    throw new BaseException("Word文檔生成失敗");
	} finally {
	    try {
	        if (template != null) {
	            template.close();
	        }
	    } catch (Exception e) {
	        log.error("流關(guān)閉失敗,異常原因:" + e.getMessage());
	    }
	}
}
生成效果

Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式,spring boot,pdf,后端

DOCX 轉(zhuǎn) PDF

初始化 XWPFDocument 需要一個(gè)輸入流,以下是直接使用文件輸入流去初始化。

FileInputStream inputStream1 = new FileInputStream("docx文件位置.docx");
XWPFDocument xwpfDocument = new XWPFDocument(inputStream1);
PdfOptions options = PdfOptions.create();
try (OutputStream outPDF = Files.newOutputStream(Paths.get("要生成的pdf位置.pdf"))) {
    PdfConverter.getInstance().convert(xwpfDocument.getXWPFDocument(), outPDF, options);
} catch (IOException e) {
    log.error("PDF轉(zhuǎn)換失敗",e);
}
PDF 生成效果

Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式,spring boot,pdf,后端

注意

DOCX 模板中如果表格元素中放了圖片(如上圖中的頭像),要確保生成的文件中的圖片大小不超過模板中單元格大小。
即圖片所在單元格不能被圖片撐大,否則圖片在轉(zhuǎn)換成PDF時(shí)無法展示。

。。。。。。。。。。。。。。。。。。。。。。
此方法轉(zhuǎn)換出來的PDF格式可能有點(diǎn)問題,所以換了個(gè)新的文檔轉(zhuǎn)換工具類,點(diǎn)擊這里查看文章來源地址http://www.zghlxwxcb.cn/news/detail-615317.html

到了這里,關(guān)于Springboot -- 按照模板生成docx、pdf文件,docx轉(zhuǎn)pdf格式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包