欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成itext導(dǎo)出PDF的過程

 更新時(shí)間:2024年11月15日 09:45:16   作者:宇宙超級勇猛無敵暴龍戰(zhàn)神  
本文介紹了如何在Spring Boot中集成iText庫導(dǎo)出PDF文件,并解決中文亂碼問題,步驟包括添加依賴、準(zhǔn)備字體、打開系統(tǒng)字體目錄選擇字體、在控制器中新增方法、創(chuàng)建并測試UserPdfExportService類,以及添加請求頭,感興趣的朋友一起看看吧

添加依賴

        <!-- PDF導(dǎo)出 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.11</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

 準(zhǔn)備字體

因?yàn)檗D(zhuǎn)成pdf文件可能出現(xiàn)亂碼或者不展示中文,所以需要自定義字體

打開目錄       C:\Windows\Fonts

挑一個(gè)自己喜歡的字體,然后CV大法

代碼

controller新增方法 

    // 導(dǎo)出pdf
    @GetMapping("/exportPdf")
    public void exportPdf(HttpServletResponse response) throws DocumentException, IOException {
        byte[] pdfBytes = userPdfExportService.exportUsersToPdf();
        String filename = "用戶信息.pdf";
        String encodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()).replace("+", "%20");
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=" + encodedFilename);
        response.setContentLength(pdfBytes.length);
        response.getOutputStream().write(pdfBytes);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

 新增UserPdfExportService類

@Service
public class UserPdfExportService {
    @Autowired
    private ISysUserService sysUserService;
    public byte[] exportUsersToPdf() throws DocumentException, IOException {
        //查詢要導(dǎo)出的數(shù)據(jù)
        List<SysUser> users = sysUserService.selectUserList(new SysUser());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();
        PdfWriter.getInstance(document, baos);
        document.open();
        // 添加標(biāo)題
        document.add(new Paragraph("用戶列表"));
        // 加載自定義字體
        InputStream is = getClass().getResourceAsStream("/static/fonts/simfang.ttf");
        if (is == null) {
            throw new IOException("字體文件未找到");
        }
        byte[] fontBytes = toByteArray(is);
        BaseFont baseFont = BaseFont.createFont("simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, true, fontBytes, null);
        Font font = new Font(baseFont, 12);
        // 創(chuàng)建表格
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100);
        // 在表格單元格中也應(yīng)使用相同的字體
        table.addCell(new PdfPCell(new Phrase("用戶名", font)));
        table.addCell(new PdfPCell(new Phrase("姓名", font)));
        table.addCell(new PdfPCell(new Phrase("郵箱", font)));
        table.addCell(new PdfPCell(new Phrase("手機(jī)號", font)));
        for (SysUser user : users) {
            table.addCell(new PdfPCell(new Phrase(user.getUserName(), font)));
            table.addCell(new PdfPCell(new Phrase(user.getNickName(), font)));
            table.addCell(new PdfPCell(new Phrase(user.getEmail(), font)));
            table.addCell(new PdfPCell(new Phrase(user.getPhonenumber(), font)));
        }
        document.add(table);
        document.close();
        return baos.toByteArray();
    }
    /**
     * 流轉(zhuǎn)byte字節(jié)
     * @param is
     * @return
     * @throws IOException
     */
    private byte[] toByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toByteArray();
    }
}

測試

記得添加請求頭

到此這篇關(guān)于SpringBoot集成itext導(dǎo)出PDF的文章就介紹到這了,更多相關(guān)SpringBoot導(dǎo)出PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring AOP與代理類的執(zhí)行順序級別淺析

    Spring AOP與代理類的執(zhí)行順序級別淺析

    這篇文章主要介紹了Spring AOP與代理類的執(zhí)行順序級別,關(guān)于 Spring AOP和Aspectj的關(guān)系,兩個(gè)都實(shí)現(xiàn)了切面編程,Spring AOP更多地是為了Spring框架本身服務(wù)的,而Aspectj具有更強(qiáng)大、更完善的切面功能
    2023-03-03
  • @RefreshScope(nacos配置熱更新方式)

    @RefreshScope(nacos配置熱更新方式)

    文章主要介紹了Spring和Nacos對`@RefreshScope`注解的處理方式,Spring在每次調(diào)用被`@RefreshScope`注解的bean的屬性時(shí),會(huì)先從本地緩存獲取,如果緩存不存在則重新創(chuàng)建并獲取最新環(huán)境配置
    2024-12-12
  • Maven生命周期和及插件原理用法詳解

    Maven生命周期和及插件原理用法詳解

    這篇文章主要介紹了Maven生命周期和及插件原理用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值的相關(guān)資料
    2020-08-08
  • 三分鐘教你如何在IDEA中快速創(chuàng)建工程的方法

    三分鐘教你如何在IDEA中快速創(chuàng)建工程的方法

    這篇文章主要介紹了三分鐘教你如何在IDEA中快速創(chuàng)建工程的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Spring輕松解決循環(huán)依賴

    Spring輕松解決循環(huán)依賴

    Spring的解決循環(huán)依賴是有前置條件的,要解決循環(huán)依賴我們首先要了解Spring Bean對象的創(chuàng)建過程和依賴注入的方式。依賴注入方式,我之前的博客有所分享,大家可以在看本篇文章之前進(jìn)行一下小小的回顧
    2023-04-04
  • Java Swing仿QQ登錄界面效果

    Java Swing仿QQ登錄界面效果

    這篇文章主要為大家詳細(xì)介紹了Java Swing仿QQ登錄界面效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 解決springboot啟動(dòng)成功,但訪問404的問題

    解決springboot啟動(dòng)成功,但訪問404的問題

    這篇文章主要介紹了解決springboot啟動(dòng)成功,但訪問404的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java null轉(zhuǎn)換為字符串的三種方法

    java null轉(zhuǎn)換為字符串的三種方法

    在Java開發(fā)中,正確處理null值至關(guān)重要,以避免空指針異常,本文介紹了三種常見的null值轉(zhuǎn)字符串方法:三元運(yùn)算符、Objects.toString方法、String.valueOf方法,感興趣的可以了解一下
    2024-10-10
  • Java從控制臺接受輸入字符的簡單方法

    Java從控制臺接受輸入字符的簡單方法

    這篇文章主要介紹了Java從控制臺接受輸入字符的簡單方法,需要的朋友可以參考下
    2014-02-02
  • springboot如何使用@Value獲取配置文件的值

    springboot如何使用@Value獲取配置文件的值

    這篇文章主要介紹了springboot如何使用@Value獲取配置文件的值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評論