功能实现背景:一个二手车项目,需要能够打印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); //标题水平居中 </