後端Itext生成pdf到傳到前端,支票列印A4紙
阿新 • • 發佈:2020-08-05
@GetMapping(value = "/zppdf/{id}", produces = MediaType.APPLICATION_PDF_VALUE) public ResponseEntity<InputStreamResource> index(@Valid @PathVariable("id") int id) throws DocumentException, IOException { var headers = new HttpHeaders(); headers.add("Content-Disposition", "inline; filename=demo.pdf"); Lrxgzp it= lrxgzpRepository.findById(id).orElseThrow(); var bis = createDocument(toDyView(it)); return ResponseEntity .ok() .headers(headers) .contentType(MediaType.APPLICATION_PDF) .body(new InputStreamResource(bis)); } privateByteArrayInputStream createDocument(LrxgzpDyView view) throws DocumentException, IOException { /* Rectangle rectangle = new Rectangle(637.8f, 226.77f); var document = new Document(rectangle);*/ var document = new Document(PageSize.A4); var out = new ByteArrayOutputStream(); document.setMargins(0, 0, 0, 0); //document.setPageSize(PageSize.A4); PdfWriter.getInstance(document, out); document.open(); addContent(document, view); document.close(); return new ByteArrayInputStream(out.toByteArray()); } private void addContent(Document document, LrxgzpDyView view) throws DocumentException, IOException { /* var preface = new Paragraph("零一二三四五六七八九十".repeat(20), pdfFontHelper.getOrange()); var table = new PdfPTable(1); table.setHorizontalAlignment(0); //可列印寬度為:595.27f - 8.5f table.setTotalWidth(595.27f); table.setWidths(new int[]{1}); table.setLockedWidth(true); PdfPCell cell = new PdfPCell(); cell.addElement(preface); cell.setPaddingLeft(8.5f); cell.disableBorderSide(15); table.addCell(cell);*/ var preface = new Paragraph(); // add a table createTable(preface, view); // now add all this to the document document.add(preface); } private void createTable(Paragraph preface, LrxgzpDyView view) throws DocumentException, IOException { //自己封裝好的字型以及大小 new Font(simsun, 9, Font.NORMAL, BaseColor.BLACK); Font font1 = pdfFontHelper.getOrange(); Font font2 = pdfFontHelper.getWatermelon(); var table = new PdfPTable(2); table.setHorizontalAlignment(0); table.setTotalWidth(595.27f); table.setWidths(new int[]{13, 50}); table.setLockedWidth(true); //左邊 var tableLeft = new PdfPTable(3); tableLeft.setHorizontalAlignment(0); tableLeft.setTotalWidth(102.05f); tableLeft.setWidths(new int[]{20, 7, 9}); tableLeft.setLockedWidth(true); //左邊附加資訊 //設定Paragraph的樣式需要在新增進單元格之前設定好 Paragraph p1 = new Paragraph(view.getZpfjxx(), font1); p1.setFirstLineIndent(25.51f); p1.setLeading(0f, 1.3f); PdfPCell cell = new PdfPCell(); cell.addElement(p1); cell.setColspan(3); cell.setFixedHeight(42.52f); cell.setMinimumHeight(42.52f); //隱藏單元格全部邊框 cell.disableBorderSide(15); tableLeft.addCell(cell); //左邊出票日期 Paragraph cprq = new Paragraph(view.getCpYear(), font1); //28.35f cprq.setFirstLineIndent(29f); cprq.setLeading(13f); cell = new PdfPCell(); cell.addElement(cprq); cell.setFixedHeight(21.25f); cell.setMinimumHeight(21.25f); cell.disableBorderSide(15); tableLeft.addCell(cell); Paragraph month = new Paragraph(view.getCpMonth(), font1); month.setFirstLineIndent(1.5f); month.setLeading(13f); cell = new PdfPCell(); cell.addElement(month); cell.setFixedHeight(21.25f); cell.setMinimumHeight(21.25f); cell.disableBorderSide(15); tableLeft.addCell(cell); Paragraph day = new Paragraph(view.getCpDay(), font1); day.setFirstLineIndent(1.5f); day.setLeading(13f); cell = new PdfPCell(); cell.addElement(day); cell.setFixedHeight(21.25f); cell.setMinimumHeight(21.25f); cell.disableBorderSide(15); tableLeft.addCell(cell); //左邊收款人 Paragraph p2 = new Paragraph(view.getSkrName(), font1); //22.68f p2.setFirstLineIndent(23.5f); p2.setLeading(11f); cell = new PdfPCell(); cell.addElement(p2); cell.setColspan(3); cell.setFixedHeight(28.35f); cell.setMinimumHeight(28.35f); cell.disableBorderSide(15); tableLeft.addCell(cell); //左邊金額 Paragraph je = new Paragraph(view.getJe(), font1); je.setFirstLineIndent(23.5f); je.setLeading(8f); cell = new PdfPCell(); cell.addElement(je); cell.setColspan(3); cell.setFixedHeight(14.17f); cell.setMinimumHeight(14.17f); cell.disableBorderSide(15); tableLeft.addCell(cell); //左邊用途 Paragraph yt = new Paragraph(view.getZpyt(), font1); yt.setFirstLineIndent(23.5f); yt.setLeading(8f); cell = new PdfPCell(); cell.addElement(yt); cell.setColspan(3); cell.setFixedHeight(14.17f); cell.setMinimumHeight(14.17f); cell.disableBorderSide(15); tableLeft.addCell(cell); //左邊最大單元格 var c1 = new PdfPCell(tableLeft); c1.setPaddingLeft(8.5f); c1.setPaddingTop(75.12f); c1.setPaddingRight(8.5f); c1.disableBorderSide(15); c1.setUseVariableBorders(true); table.addCell(c1); //右邊大單元格 PdfPCell c2 = new PdfPCell(); c2.setPaddingLeft(49.6f); //42.52f c2.setPaddingTop(41.1f); c2.disableBorderSide(15); c2.setHorizontalAlignment(Element.ALIGN_LEFT); //右邊內容定位表格 var tablePos = new PdfPTable(1); tablePos.setTotalWidth(413.85f); tablePos.setLockedWidth(true); PdfPCell tablePosCell = new PdfPCell(); tablePosCell.setPaddingLeft(0f); tablePosCell.setPaddingTop(0f); tablePosCell.disableBorderSide(15); tablePosCell.setVerticalAlignment(Element.ALIGN_MIDDLE); tablePosCell.setHorizontalAlignment(Element.ALIGN_LEFT); //右邊出票日期 var table2 = new PdfPTable(8); table2.setTotalWidth(413.85f); table2.setWidths(new int[]{23, 18, 3, 13, 3, 13, 3, 70}); table2.setLockedWidth(true); PdfPCell ce = new PdfPCell(); ce.disableBorderSide(15); table2.addCell(ce); //右邊出票日期大寫年 Paragraph yearDx = new Paragraph(view.getCpYearDx(), font1); yearDx.setLeading(9f); yearDx.setAlignment(Element.ALIGN_CENTER); ce = new PdfPCell(); ce.disableBorderSide(15); ce.addElement(yearDx); ce.setFixedHeight(14.17f); ce.setMinimumHeight(14.17f); table2.addCell(ce); ce = new PdfPCell(); ce.disableBorderSide(15); table2.addCell(ce); //右邊出票日期大寫月 Paragraph monthDx = new Paragraph(view.getCpMonthDx(), font1); monthDx.setLeading(9f); monthDx.setAlignment(Element.ALIGN_CENTER); ce = new PdfPCell(); ce.disableBorderSide(15); ce.addElement(monthDx); ce.setFixedHeight(14.17f); ce.setMinimumHeight(14.17f); table2.addCell(ce); ce = new PdfPCell(); ce.disableBorderSide(15); table2.addCell(ce); //右邊出票日期大寫日 Paragraph dayDx = new Paragraph(view.getCpDayDx(), font1); dayDx.setLeading(9f); dayDx.setAlignment(Element.ALIGN_CENTER); ce = new PdfPCell(); ce.disableBorderSide(15); ce.addElement(dayDx); ce.setFixedHeight(14.17f); ce.setMinimumHeight(14.17f); table2.addCell(ce); ce= new PdfPCell(); ce.disableBorderSide(15); table2.addCell(ce); ce = new PdfPCell(); ce.disableBorderSide(15); table2.addCell(ce); tablePosCell.addElement(table2); //右邊收款人 var rSkrTable = new PdfPTable(1); rSkrTable.setTotalWidth(413.85f); rSkrTable.setWidths(new int[]{1}); rSkrTable.setLockedWidth(true); Paragraph pskr = new Paragraph(14.17f, view.getSkrName(), font1); pskr.setFirstLineIndent(31.18f); pskr.setLeading(10f); PdfPCell rSkrTablecell = new PdfPCell(); rSkrTablecell.disableBorderSide(15); rSkrTablecell.setFixedHeight(17.01f); rSkrTablecell.addElement(pskr); rSkrTable.addCell(rSkrTablecell); tablePosCell.addElement(rSkrTable); //右邊金額 var table3 = new PdfPTable(13); table3.setTotalWidth(413.85f); table3.setWidths(new int[]{13, 89, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}); table3.setLockedWidth(true); PdfPCell table3cell1 = new PdfPCell(); table3cell1.disableBorderSide(15); table3cell1.setRowspan(2); table3.addCell(table3cell1); PdfPCell table3cell2 = new PdfPCell(); table3cell2.disableBorderSide(15); table3cell2.setRowspan(2); table3cell2.setFixedHeight(25.51f); //右邊大寫金額 Paragraph jeDx = new Paragraph(view.getJeDx(), font2); jeDx.setLeading(16f); table3cell2.addElement(jeDx); table3.addCell(table3cell2); for (int i = 0; i < 11; i++) { PdfPCell cell2 = new PdfPCell(); cell2.disableBorderSide(15); cell2.setFixedHeight(11.34f); table3.addCell(cell2); } PdfPCell table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je1 = new Paragraph(view.getYw(), font1); je1.setLeading(10f); table3c3.addElement(je1); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je11 = new Paragraph(view.getQww(), font1); je11.setLeading(10f); table3c3.addElement(je11); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je2 = new Paragraph(view.getBww(), font1); je2.setLeading(10f); table3c3.addElement(je2); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je3 = new Paragraph(view.getSww(), font1); je3.setLeading(10f); table3c3.addElement(je3); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je4 = new Paragraph(view.getWw(), font1); je4.setLeading(10f); table3c3.addElement(je4); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je5 = new Paragraph(view.getQw(), font1); je5.setLeading(10f); table3c3.addElement(je5); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je6 = new Paragraph(view.getBw(), font1); je6.setLeading(10f); table3c3.addElement(je6); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je7 = new Paragraph(view.getSw(), font1); je7.setLeading(10f); table3c3.addElement(je7); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je8 = new Paragraph(view.getGw(), font1); je8.setLeading(10f); table3c3.addElement(je8); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je9 = new Paragraph(view.getSfw(), font1); je9.setLeading(10f); table3c3.addElement(je9); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); table3c3 = new PdfPCell(); table3c3.disableBorderSide(15); Paragraph je10 = new Paragraph(view.getBfw(), font1); je10.setLeading(10f); table3c3.addElement(je10); table3c3.setFixedHeight(14.17f); table3.addCell(table3c3); tablePosCell.addElement(table3); //用途密碼 var table4 = new PdfPTable(2); table4.setTotalWidth(413.85f); table4.setWidths(new int[]{1, 1}); table4.setLockedWidth(true); PdfPCell table4cell = new PdfPCell(); table4cell.disableBorderSide(15); Paragraph table4p1 = new Paragraph(view.getZpyt(), font1); table4p1.setFirstLineIndent(19.84f); table4cell.addElement(table4p1); table4cell.setPaddingTop(3f); table4.addCell(table4cell); table4cell = new PdfPCell(); table4cell.disableBorderSide(15); Paragraph table4p2 = new Paragraph(view.getZpmm(), font1); table4p2.setFirstLineIndent(56.69f); table4cell.addElement(table4p2); table4cell.setPaddingTop(3f); table4.addCell(table4cell); tablePosCell.addElement(table4); tablePos.addCell(tablePosCell); c2.addElement(tablePos); table.addCell(c2); preface.add(table); }
1毫米 對應程式碼中 72/25.4 f,即想要寬度為1mm,則需要在程式碼中設定 2.83f (浮點數)
前端我用vue+elementUI
<el-dialog title :visible.sync="dialogVisible" width="30%" :before-close="handleClose"> <div align="center"> <span>選擇支票模板</span> <el-select v-model="zpmb" placeholder="請選擇支票模板" @change="changeZpmb"> <el-option v-for="it in zpmbs" :key="it.num" :label="it.name" :value="it.num" /> </el-select> </div> <div align="center" style="margin-top:20px;"> <span> <el-button @click="handleClose">取 消</el-button> <el-link :href="ahref" target="_blank" :disabled="disableYl" @click="dialogVisible=false" > <el-button type="primary" :disabled="disableYl" @click="dialogVisible=false">預覽</el-button> </el-link> </span> </div> </el-dialog>
也可以直接用a標籤
this.ahref = `/api/xxxx/lrxgzp/zppdf/${this.selectllist[0].id}`;
效果如下圖
列印到支票上效果圖