在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    Spring Boot – 將 html 頁(yè)面轉(zhuǎn)化為 pdf 文件

    Spring Boot - 將 html 頁(yè)面轉(zhuǎn)化為 pdf 文件

    廢話不多說(shuō),先看效果圖。

    示例重要功能點(diǎn):

    • 訪問(wèn) url 直接下載 pdf 文件,前后端分離的項(xiàng)目可能通過(guò)這種方式下載 pdf 文件;
    • 將 html 頁(yè)面(含中文、圖片)轉(zhuǎn)換成 pdf 文件

    技術(shù)棧:

    • Flying Saucer
    • thymeleaf

    1、創(chuàng)建 Spring Boot 項(xiàng)目

    進(jìn)入 http://start.spring.io 創(chuàng)建 Spring Boot 項(xiàng)目,Spring Boot 版本為 2.7.0,選擇如下依賴:

    • Starter:spring-boot-starter-web
    • spring-boot-starter-thymeleaf
    • lombok

    2、修改 pom.xml 文件,添加將 html 頁(yè)面轉(zhuǎn)換成 pdf 文件需要的依賴:

    org.xhtmlrenderer flying-saucer-pdf 9.1.22 commons-io commons-io 2.11.0 ognl ognl 3.1.29 com.github.jtidy jtidy 1.0.2

    3、創(chuàng)建演示數(shù)據(jù)需要的實(shí)體類 —— Student.java

    import lombok.Builder;import lombok.Data;@Data@Builderpublic class Student { private Integer id; private String name; private String gender; private Integer age;}

    4、創(chuàng)建具體的業(yè)務(wù)處理類 —— PdfService.java

    import com.lowagie.text.DocumentException;import com.lowagie.text.pdf.BaseFont;import com.wangc.downloadpdf.entity.Student;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;import org.thymeleaf.TemplateEngine;import org.thymeleaf.context.Context;import org.w3c.tidy.Tidy;import org.xhtmlrenderer.pdf.ITextRenderer;import javax.annotation.Resource;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.nio.charset.StandardCharsets;import java.util.List;@Slf4j@Servicepublic class PdfService { @Resource private TemplateEngine templateEngine; public ByteArrayInputStream exportPdf(String template, List students) throws Exception { Context context = new Context(); context.setVariable(“students”, students); String content = convertToXhtml(templateEngine.process(template, context)); ByteArrayInputStream byteArrayInputStream = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ITextRenderer renderer = new ITextRenderer(); renderer.getFontResolver().addFont(“c:/Windows/Fonts/simsun.ttc”, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); renderer.setDocumentFromString(content); renderer.layout(); renderer.createPDF(byteArrayOutputStream, false); renderer.finishPDF(); byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); } catch (DocumentException e) { log.error(e.getMessage(), e); } return byteArrayInputStream; } private String convertToXhtml(String htmlContent) throws UnsupportedEncodingException { Tidy tidy = new Tidy(); tidy.setInputEncoding(“UTF-8”); tidy.setOutputEncoding(“UTF-8”); tidy.setXHTML(true); ByteArrayInputStream inputStream = new ByteArrayInputStream(htmlContent.getBytes(StandardCharsets.UTF_8)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); tidy.parseDOM(inputStream, outputStream); return outputStream.toString(“UTF-8”); }}

    5、創(chuàng)建控制器 —— PdfController.java

    import com.wangc.downloadpdf.entity.Student;import com.wangc.downloadpdf.service.PdfService;import org.apache.commons.io.IOUtils;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import java.io.ByteArrayInputStream;import java.util.List;import java.util.stream.Collectors;import java.util.stream.IntStream;@Controllerpublic class PdfController { @Resource private PdfService pdfService; @GetMapping(“/downloadPdf”) public void downloadPdf(HttpServletResponse response) throws Exception { List students = createTestData(); ByteArrayInputStream byteArrayInputStream = pdfService.exportPdf(“students”, students); response.setContentType(“application/octet-stream”); response.setHeader(“Content-Disposition”, “attachment; filename=receipt.pdf”); IOUtils.copy(byteArrayInputStream, response.getOutputStream()); } // 效果預(yù)覽 @GetMapping(“/view”) public ModelAndView view() throws Exception { List students = createTestData(); ModelAndView mv = new ModelAndView(); mv.setViewName(“students”); mv.addObject(“students”, students); return mv; } // 測(cè)試數(shù)據(jù) private List createTestData() { final List students = IntStream.range(1, 10) .mapToObj(v -> Student.builder() .id(v) .name(“學(xué)生” + v) .age(16) .gender(v % 2 == 0 ? “男” : “女”) .build()) .collect(Collectors.toList()); return students; }}

    6、創(chuàng)建 html 模板 —— students.html

    Students View body { /* 宋體 */ font-family:SimSun; font-size: 12px; }

    Id 姓名 性別 年齡

    7、啟動(dòng)項(xiàng)目

    訪問(wèn) http://localhost:9090/view 查看 html 頁(yè)面效果。訪問(wèn) http://localhost:9090/downloadPdf 下載 pdf 文件。

    鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年6月16日 06:18
    下一篇 2022年6月16日 06:19

    相關(guān)推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息