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

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

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

添加依賴

        <!-- 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ī)號(hào)", 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();
    }
}

測(cè)試

記得添加請(qǐng)求頭

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

相關(guān)文章

最新評(píng)論

洪江市| 黎城县| 栖霞市| 怀仁县| 攀枝花市| 金寨县| 永靖县| 饶河县| 大足县| 天气| 桐庐县| 麻江县| 固原市| 棋牌| 准格尔旗| 丽水市| 聊城市| 二连浩特市| 新蔡县| 仙游县| 峡江县| 常山县| 长治县| 龙州县| 平顶山市| 彭水| 大竹县| 保德县| 株洲县| 延边| 浦江县| 钦州市| 盖州市| 常宁市| 沛县| 合江县| 房产| 亳州市| 济阳县| 镇安县| 景东|