功能實現背景:一個二手車項目,需要能夠打印pdf並且實現在線預覽。
分析:
(1)在線預覽預覽的是一張圖片,所以我需要在生成pdf之後將pdf轉換爲jpg格式圖片,這裏使用itextbox可以實現。
(2)我在轉換出圖片後,需要將圖片上傳到公司的圖片服務器,這就需要調用公司的接口,但是公司的上傳接口需要的參數是MultipartFile類型,因此,我需要將File類型的文件轉換成MultipartFile類型,這通過添加spring-test依賴,使用MockMultipartFile的構造方法可以實現。
代碼實現如下:
/** * @Author: zy * @Description: 打印過戶單據Service * @Date: 2018/6/20_19:41 **/ public PdfOutput printTransferInfo(PrintTransferInfo printTransferInfo) throws Exception{ Document document = new Document(PageSize.A4); /*ByteArrayOutputStream ba=new ByteArrayOutputStream();*/ //生成文件路徑 String serialNo = Util.getNowYYYYMMDDHHMMSS(); String fileNewName = serialNo + UUID.randomUUID().toString().replaceAll("-", "")+".pdf"; String path = PrintPdfUtil.pdfdir+serialNo+ DataUtil.getCurrent().getStoreid(); //沒有文件夾就創建 File existfile =new File(path); if(!existfile.exists()){ existfile.mkdirs(); //創建文件夾 } String pdfPath = path+File.separator+fileNewName; File record = new File(pdfPath); FileOutputStream fos=new FileOutputStream(record); PdfWriter writer = PdfWriter.getInstance(document, fos); document.open(); Paragraph titleValue=new Paragraph("電子流程辦理單",PrintPdfUtil.title1); titleValue.setAlignment(Element.ALIGN_CENTER); //標題水平居中 document.add(titleValue); //審覈日期,流水號 PdfPTable table1=new PdfPTable(1); table1.setSpacingBefore(24f); table1.setTotalWidth(new float[]{100f}); table1.addCell(getPDFCellNoBorder(printTransferInfo.getUctfexaminetime()==null?" ":printTransferInfo.getUctfexaminetime(),PrintPdfUtil.content1)); table1.addCell(getPDFCellNoBorder("流水號 "+(printTransferInfo.getUctftransferno()==null?" ":printTransferInfo.getUctftransferno()),PrintPdfUtil.content1)); document.add(table1); //二維碼圖片 BarcodeQRCode barcodeQRCode = new BarcodeQRCode(printTransferInfo.getUctftransferno()==null?" ":printTransferInfo.getUctftransferno(), 100, 100, null); Image codeQrImage = barcodeQRCode.getImage(); /*codeQrImage.scaleAbsolute(100, 100);*/ codeQrImage.setAbsolutePosition(470,670); document.add(codeQrImage); //表2 PdfPTable table2=new PdfPTable(2); table2.setSpacingBefore(50f); table2.setTotalWidth(new float[]{100f,100f}); table2.addCell(getPDFCellNoBorder("車牌號:"+(printTransferInfo.getUctlicenseplate()==null?" ":printTransferInfo.getUctlicenseplate()),PrintPdfUtil.content1)); table2.addCell(getPDFCellNoBorder("車架號:"+(printTransferInfo.getUctfcarvin()==null?" ":printTransferInfo.getUctfcarvin()),PrintPdfUtil.content1)); table2.addCell(getPDFCellNoBorder("廠牌型號:"+(printTransferInfo.getUctfcarmodel()==null?" ":printTransferInfo.getUctfcarmodel()),PrintPdfUtil.content1)); table2.addCell(getPDFCellNoBorder("車輛類型:"+(printTransferInfo.getUctfcarsize()==null?" ":printTransferInfo.getUctfcarsize()),PrintPdfUtil.content1)); document.add(table2); //買方流程圖及所需證件 Paragraph titleValue2=new Paragraph("買方流程圖及所需證件",PrintPdfUtil.title2); titleValue2.setAlignment(Element.ALIGN_CENTER); //標題水平居中 titleValue2.setSpacingBefore(20f); document.add(titleValue2); // 讀圖片 Image image = Image.getInstance(PrintPdfUtil.imgpath+"liucheng.png"); // 根據域的大小縮放圖片 /*image.scaleToFit(signRect.getWidth(),signRect.getHeight());*/ // 添加圖片 image.setAbsolutePosition(0, 200); document.add(image); //表3 PdfPTable table3=new PdfPTable(1); table3.setSpacingBefore(50f); table3.setTotalWidth(new float[]{500f}); table3.addCell(getPDFCellNoBorder("1.身份證或單位組織機構代碼證原件",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder(" ",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder(" ",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder(" ",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder(" ",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder("賣方:"+(printTransferInfo.getUctfsailername()==null?" ":printTransferInfo.getUctfsailername()),PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder("買方:"+(printTransferInfo.getUctfbuyername()==null?" ":printTransferInfo.getUctfbuyername()),PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder(" ",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder("代辦公司:保定市騰達舊車交易市場" + "xx汽車信息服務部",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder(" ",PrintPdfUtil.content1)); table3.addCell(getPDFCellNoBorder("錄入員:"+(printTransferInfo.getUcsname()==null?" ":printTransferInfo.getUcsname()),PrintPdfUtil.content1)); PdfContentByte tb3= writer.getDirectContent(); ColumnText ct3=new ColumnText(tb3); ct3.setSimpleColumn(300,100,600,470,25,Element.ALIGN_LEFT); ct3.addElement(table3); ct3.go(); //審覈蓋章、買家簽字 PdfContentByte tb4= writer.getDirectContent(); ColumnText ct4=new ColumnText(tb4); ct4.setSimpleColumn(70,70,170,90); Phrase phrase4=new Phrase("審覈蓋章",PrintPdfUtil.content1); ct4.addText(phrase4); ct4.setAlignment(0); ct4.go(); PdfContentByte tb5= writer.getDirectContent(); ColumnText ct5=new ColumnText(tb5); ct5.setSimpleColumn(350,70,450,90); Phrase phrase5=new Phrase("買家簽字:",PrintPdfUtil.content1); ct5.addText(phrase5); ct5.setAlignment(0); ct5.go(); document.close(); //將pdf轉換爲img List<String> imgs=extractPdfImg(record.getAbsolutePath(),PrintPdfUtil.imgdir); //將file轉換爲MultipartFile FileInputStream input = new FileInputStream(imgs.get(0)); MultipartFile multipartFile = new MockMultipartFile("file", record.getName(), "multipart/form-data", IOUtils.toByteArray(input)); int picid=fileService.uploadCarImg(multipartFile); fos.close(); writer.close(); PdfOutput output=new PdfOutput(); output.setPicid(picid); output.setPdfPath(pdfPath); return output; }