使用itextbox实现pdf的在线预览

本文介绍了在二手车项目中如何利用itextbox将PDF转换为jpg图片,以便实现在线预览功能。首先,通过itextbox生成PDF后,将PDF转换为图像。接着,将转换后的图片上传到公司图片服务器,这里涉及到将File类型转换为MultipartFile类型,借助spring-test的MockMultipartFile完成此转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值