SpringBoot+Vue+Itext实现前端请求文件流的方式导出PDF时在指定位置添加照片
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot+Vue+Itext实现前端请求文件流的方式导出PDF时在指定位置添加照片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
SpringBoot+Vue+Itext實現前端請求文件流的方式下載PDF:
SpringBoot+Vue+Itext實現前端請求文件流的方式下載PDF_BADAO_LIUMANG_QIZHI的博客-CSDN博客
在上面的基礎上怎樣實現在導出pdf時在指定的位置插入照片,并設置照片大小。
注:
博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓氣質_CSDN博客-C#,SpringBoot,架構之路領域博主
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
1、前后端請求的流程同上面一樣,準備一張照片,這里路徑是D:\badao.jpg
2、然后后臺在doc對象上繼續添加Image對象
??????? //添加照片Image jpg = Image.getInstance("D:\\badao.jpg");//設置圖片坐標位置jpg.setAbsolutePosition(400,100);//設置圖片長和寬的縮放,這里都縮放到50%jpg.scalePercent(50f,50f);doc.add(jpg);注意這里的Image是com.itextpdf.text包下的。
然后可以通過setAbsolutePosition設置圖片的位置。
還可以通過scalePercent設置長和寬的縮放百分比
3、完整后臺Controller代碼
@RequestMapping("/getPdfWithImage")public void getPdfWithImage(HttpServletRequest request, HttpServletResponse response) throws Exception {//設置響應格式等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<>();//設置紙張規格為A4紙Rectangle rect = new Rectangle(PageSize.A4);//創建文檔實例Document doc=new Document(rect);//添加中文字體BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//設置字體樣式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); //一級標題Font secondTitleFont = new Font(bfChinese,15,Font.BOLD, CMYKColor.BLUE); //二級標題Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下劃線斜體//設置字體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);//設置要導出的pdf的標題String title = "霸道流氓氣質";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();//新建段落//使用二級標題 顏色為藍色Paragraph p1 = new Paragraph("二級標題", secondTitleFont);//設置行高p1.setLeading(0);//設置標題居中p1.setAlignment(Element.ALIGN_CENTER);//將段落添加到文檔上doc.add(p1);//設置一個空的段落,行高為18 什么內容都不顯示Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese11);doc.add(blankRow1);//新建表格 列數為2PdfPTable table1 = new PdfPTable(2);//給表格設置寬度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));//設置單元格邊框為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://blog.csdn.net/BADAO_LIUMANG_QIZHI ",boldFont));PdfPCell cell16 = new PdfPCell(new Paragraph("當前時間: "+new Date().toString(),boldFont));cell15.setBorder(0);cell16.setBorder(0);table3.addCell(cell15);table3.addCell(cell16);doc.add(table3);//添加照片Image jpg = Image.getInstance("D:\\badao.jpg");//設置圖片坐標位置jpg.setAbsolutePosition(400,100);//設置圖片長和寬的縮放,這里都縮放到50%jpg.scalePercent(50f,50f);doc.add(jpg);doc.close();}總結
以上是生活随笔為你收集整理的SpringBoot+Vue+Itext实现前端请求文件流的方式导出PDF时在指定位置添加照片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot+Vue+Itext
- 下一篇: el-table中设置max-heigh