亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

      
      

            <dl id="hur0q"><div id="hur0q"></div></dl>

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

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

                廢話不多說,先看效果圖

                示例重要功能點:

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

                技術(shù)棧:

                • Flying Saucer
                • thymeleaf

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

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

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

                2、修改 pom.xml 文件,添加將 html 頁面轉(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ù)需要的實體類 —— 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; } // 測試數(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、啟動項目

                訪問 http://localhost:9090/view 查看 html 頁面效果。訪問 http://localhost:9090/downloadPdf 下載 pdf 文件。

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

                相關(guān)推薦

                • 30個無加盟費的項目(茶顏悅色奶茶店加盟費多少)

                  茶顏悅色又爆了,8月18日,茶顏悅色南京門店正式開業(yè),開張不到半小時,門店就人滿為患,消費者的購買熱情十分高漲,而由于人流量過大造成擁堵,茶顏悅色也不得不暫停營業(yè)。 當(dāng)然,這里面排…

                  2022年11月27日
                • 凈利潤率越高越好嗎(凈利潤率多少合適)

                  一、持續(xù)增收不增利,平均凈利潤率首次跌入個位數(shù) 2021年,增收不增利依舊是行業(yè)主流。具體來看,大部分企業(yè)營業(yè)收入呈增長態(tài)勢,E50企業(yè)平均同比增速達到17.3%,但是利潤增速則明…

                  2022年11月26日
                • 世界杯海外直播在哪看(中國女籃世界杯直播)

                  最近一段時間最火爆的話題就是世界杯了,大家都想第一時間觀看直播比賽,一些海外小伙伴在問世界杯海外直播在哪看?2022年世界杯海外直播免費觀看平臺有哪些?下面小編為大家?guī)硎澜绫M狻?/p>

                  2022年11月25日
                • 規(guī)范透明促PPP高質(zhì)量發(fā)展——16萬億元大市場迎來新規(guī)

                  近日,財政部印發(fā)《關(guān)于進一步推動政府和社會資本合作(PPP)規(guī)范發(fā)展、陽光運行的通知》,從做好項目前期論證、推動項目規(guī)范運作、嚴(yán)防隱性債務(wù)風(fēng)險、保障項目陽光運行四個方面進一步規(guī)范P…

                  2022年11月25日
                • 拼多多百億補貼預(yù)售一般多久發(fā)貨(拼多多百億補貼預(yù)售)

                  拼多多里面有很多優(yōu)惠活動,其中百億補貼活動非?;鸨?,一些里面的東西價格比別的平臺便宜,質(zhì)量也有保障,還有預(yù)售的活動,那么拼多多百億補貼預(yù)售一般多久發(fā)貨?下面小編為大家?guī)砥炊喽喟賰|…

                  2022年11月25日
                • 推薦3種白手起家的賺錢項目(白手起家賺錢項目有哪些)

                  如今社會壓力非常的大,家有老少要養(yǎng)活,這些都加速了窮人想要創(chuàng)業(yè)的欲望,但是創(chuàng)業(yè)路總是那么的艱難,資金就是創(chuàng)業(yè)的重頭戲,所以選擇一個低成本又賺錢的項目是大多數(shù)人最期望的了,那么有哪些…

                  2022年11月25日
                • 百度關(guān)鍵詞快速排名的4大原理解析(百度怎么刷關(guān)鍵詞)

                  近期百度公告驚雷算法2.0,升級之快還是第一次吧,看來百度對于刷點擊行為是零容忍了。之前尹華峰SEO技術(shù)博客介紹過一篇如何使用刷點擊工具,其實市面上有很多這類SEO快速排名的軟件,…

                  2022年11月25日
                • 科比19歲女兒遭自稱與她生“科比式孩子”男子跟蹤騷擾

                  極目新聞記者王亮亮黃佳琪 據(jù)??怂剐侣劸W(wǎng)報道,當(dāng)?shù)貢r間11月21日,已故籃球巨星科比·布萊恩特的長女娜塔莉亞·布萊恩特21日向法院提交臨時限制令,聲稱這位32歲的前科從十幾歲起就騷…

                  2022年11月24日
                • 《金鏟鏟之戰(zhàn)》s8賽季羈絆有哪些 s8賽季羈絆大全

                  《金鏟鏟之戰(zhàn)》最近也快更新s8賽季了,增加了許多好玩有趣的新羈絆以及一些以前版本的羈絆也回歸了,那么新增加的羈絆都有些什么呢?下面讓我們一起來看看吧! 《金鏟鏟之戰(zhàn)》s8賽季羈絆大…

                  2022年11月24日
                • Steam秋季特賣開啟 為Steam大獎提名游戲

                  Steam秋季特賣開啟 為Steam大獎提名游戲 Steam秋季特賣活動現(xiàn)已正式開啟,時間從11月23日持續(xù)到11月30日(北京時間),新老游戲均有不錯的折扣,感興趣的玩家可以前往…

                  2022年11月24日

                聯(lián)系我們

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