使用itextbox实现pdf的在线预览

功能实现背景:一个二手车项目,需要能够打印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;
}


 

猜你喜欢

转载自blog.csdn.net/qq_31247177/article/details/80819662