javascript
SpringBoot加itext实现PDF导出
場景
iText是一個能夠快速產(chǎn)生PDF文件的java類庫。iText的java類對于那些要產(chǎn)生包含文本,表格,圖形的只讀文檔是很有用的。它的類庫尤其與java Servlet有很好的給合。使用iText與PDF能夠使你正確的控制Servlet的輸出。
官網(wǎng):
The Leading PDF Library for Developers | iText
Itext實現(xiàn)導(dǎo)出PDF常用方法說明
Itext實現(xiàn)導(dǎo)出PDF常用方法說明_BADAO_LIUMANG_QIZHI的博客-CSDN博客
實現(xiàn)
打開pom.xml導(dǎo)入項目依賴
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.2.0</version> </dependency> <dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version> </dependency>前端實現(xiàn)
打印按鈕
<button id="printPdfBtn" class="btn btn-info " type="button"><i class="fa fa-trash-o"></i> pdf打印</button>js中按鈕的點擊事件
?$("#printPdfBtn").click(function () {var data = t.rows(['.selected']).data()[0];if(undefined===data){swal({type: 'warning',title: '提示:',text: '請首先選擇一行數(shù)據(jù)!',confirmButtonColor: "#1ab394",})}else{exportPdf(data.id);}});其中 var data = t.rows(['.selected']).data()[0];
是dataTables中獲取選中行的id屬性。
exportPdf(data.id)負責(zé)執(zhí)行具體的導(dǎo)出PDF請求
先判斷能不能執(zhí)行導(dǎo)出,如果能導(dǎo)出則跳轉(zhuǎn)Url并將參數(shù)id傳遞。
function? exportPdf(id){$.post("/wmsInOrder/isExport.do",{id:id}).done(function (res) {if(res.status){if(res.data ==true){??????????????????window.location.href="/wmsInOrder/exportPdf.html?orderId="+id;}else{Swal.fire('請選擇已完成的單',res.data,'warning')}}else{Swal.fire('導(dǎo)出失敗!',res.data,res.msg)}}).fail(function (err) {Swal.fire('異常提示','執(zhí)行導(dǎo)出操作失敗','error')});}來到url="/wmsInOrder/exportPdf.html?orderId="+id所對應(yīng)的后臺。
?@Description("pdf導(dǎo)出")@RequestMapping(value = "/exportPdf.html")public void excelPdf(long orderId,HttpServletRequest request, HttpServletResponse response) throws Exception {this.inOrderService.exportPdf(orderId, request, response);}來到service層
?void exportPdf(Long orderId, HttpServletRequest request, HttpServletResponse response) throws Exception ;來到serviceImpl層
具體方法作用參照代碼注解。
public void exportPdf(Long orderId, HttpServletRequest request, HttpServletResponse response) throws Exception {//設(shè)置響應(yīng)格式等response.setContentType("application/pdf");response.setHeader("Expires", "0");response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");response.setHeader("Pragma", "public");Map<String,Object> map = new HashMap<>();//設(shè)置紙張規(guī)格為A4紙Rectangle rect = new Rectangle(PageSize.A4);//創(chuàng)建文檔實例Document doc=new Document(rect);//添加中文字體BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//設(shè)置字體樣式Font textFont = new Font(bfChinese,11, Font.NORMAL); //正常//Font redTextFont = new Font(bfChinese,11,Font.NORMAL,Color.RED); //正常,紅色Font boldFont = new Font(bfChinese,11,Font.BOLD); //加粗//Font redBoldFont = new Font(bfChinese,11,Font.BOLD,Color.RED); //加粗,紅色Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一級標(biāo)題Font secondTitleFont = new Font(bfChinese,15,Font.BOLD, CMYKColor.BLUE); //二級標(biāo)題Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下劃線斜體//設(shè)置字體com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);//設(shè)置要導(dǎo)出的pdf的標(biāo)題String title = "霸道流氓氣質(zhì)";response.setHeader("Content-disposition","attachment; filename=".concat(String.valueOf(URLEncoder.encode(title + ".pdf", "UTF-8"))));OutputStream out = response.getOutputStream();PdfWriter.getInstance(doc,out);doc.open();doc.newPage();//新建段落//使用二級標(biāo)題 顏色為藍色Paragraph?? p1 = new Paragraph("二級標(biāo)題", secondTitleFont);//設(shè)置行高p1.setLeading(0);//設(shè)置標(biāo)題居中p1.setAlignment(Element.ALIGN_CENTER);//將段落添加到文檔上doc.add(p1);//設(shè)置一個空的段落,行高為18? 什么內(nèi)容都不顯示Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese11);doc.add(blankRow1);//新建表格 列數(shù)為2PdfPTable table1 = new PdfPTable(2);//給表格設(shè)置寬度int width1[] = {80,60};table1.setWidths(width1);//新建單元格String name="霸道";String gender="男";//給單元格賦值 每個單元格為一個段落,每個段落的字體為加粗PdfPCell cell11 = new PdfPCell(new Paragraph("姓名:? "+name,boldFont));PdfPCell cell12 = new PdfPCell(new Paragraph("性別:? "+gender,boldFont));//設(shè)置單元格邊框為0cell11.setBorder(0);cell12.setBorder(0);table1.addCell(cell11);table1.addCell(cell12);doc.add(table1);PdfPTable table3 = new PdfPTable(2);table3.setWidths(width1);PdfPCell cell15 = new PdfPCell(new Paragraph("博客主頁: https://me.csdn.net/BADAO_LIUMANG_QIZHI? ",boldFont));PdfPCell cell16 = new PdfPCell(new Paragraph("當(dāng)前時間:? "+DateConvert.formatDateToString(new Date(),DateStyle.YYYY_MM_DD),boldFont));cell15.setBorder(0);cell16.setBorder(0);table3.addCell(cell15);table3.addCell(cell16);doc.add(table3);doc.close();}效果
點擊按鈕
打開PDF
總結(jié)
以上是生活随笔為你收集整理的SpringBoot加itext实现PDF导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot+Thymeleaf
- 下一篇: gradle idea java ssm