匯出為PDF系列(一)__java後臺傳PDF檔案
阿新 • • 發佈:2018-12-10
寫在之前:匯出PDF系列會有三篇,方法思路都是查了大量資料,參考了很多部落格文章,連結全部貼上來有點不現實,如果有冒犯之處還請指出,馬上改正。如果有疑問,歡迎留言評論,必定竭盡全力答覆,接下來就開始吧。
用PDF將模板寫死,內容動態填充
這裡以匯出以下頁面為例
(1)新建一個word文件,設計以上內容,檔案另存為PDF
(2)用adobe acrobat pro dc開啟剛剛儲存的pdf檔案(注意:沒有這個軟體的話,要下載),然後點選“準備表單”
(3)在照片的下方新增文字域
(4)此時頁面上有三個文字域,後期的資料就是填充到這三個文字域裡面,雙擊文字域編輯名稱,方便後臺程式碼向裡面傳值
(5)都設定好之後儲存檔案到本地,並且將此PDF檔案放到資源路徑下:
(6)終於開始寫程式碼了
引入jar
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>core-renderer</groupId> <artifactId>core-renderer</artifactId> <version>R8RC2</version> </dependency> <dependency> <groupId>itext</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency>
@RestController public class PdfController { /** * 匯出pdf * @param response * @return * @throws UnsupportedEncodingException */ @RequestMapping(value={"/exportpdf"}) public String exportPdf(HttpServletResponse response) throws UnsupportedEncodingException { // 指定解析器 System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); String filename="練習1.pdf"; String path = this.getClass().getClassLoader().getResource("練習1.pdf").getPath(); response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename, "UTF-8")); OutputStream os = null; PdfStamper ps = null; PdfReader reader = null; try { os = response.getOutputStream(); // 2 讀入pdf表單 reader = new PdfReader(path); // 3 根據表單生成一個新的pdf ps = new PdfStamper(reader, os); // 4 獲取pdf表單 AcroFields form = ps.getAcroFields(); // 5給表單新增中文字型 這裡採用系統字型。不設定的話,中文可能無法顯示 // 字型可以根據個人喜好上傳到resources目錄下 BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); form.addSubstitutionFont(bf); // 6查詢資料================================================ Map<String, Object> data = new HashMap<String, Object>(); data.put("name", "小帥哥"); data.put("like", "大美女"); // 7遍歷data 給pdf表單表格賦值 for (String key : data.keySet()) { form.setField(key,data.get(key).toString()); } ps.setFormFlattening(true); //-----------------------------pdf 新增圖片---------------------------------- // 通過域名獲取所在頁和座標,左下角為起點 System.out.println("pdf 新增圖片"); String imgpath="E:/匯出PDF/美女.png"; int pageNo = form.getFieldPositions("img").get(0).page; Rectangle signRect = form.getFieldPositions("img").get(0).position; float x = signRect.getLeft(); float y = signRect.getBottom(); // 讀圖片 Image image = Image.getInstance(imgpath); //若圖片路徑不是本地,而是從伺服器上獲取的,此時new image物件的方式為如下: // Image image = Image.getInstance(new URL(imgpath)); // 獲取操作的頁面 PdfContentByte under = ps.getOverContent(pageNo); // 根據域的大小縮放圖片 image.scaleToFit(signRect.getWidth(), signRect.getHeight()); // 新增圖片 image.setAbsolutePosition(x, y); under.addImage(image); } catch (Exception e) { e.printStackTrace(); } finally { try { ps.close(); reader.close(); os.close(); } catch (Exception e) { e.printStackTrace(); } } return null; } }
(7)接下來本地起服務,訪問請求http://localhost:8080/exportpdf就匯出pdf啦