java中将外部上传的ofd格式文件转换为pdf格式文件,并以blob的格式返回给前端

所需依赖:

<dependency>
    <groupId>org.ofdrw</groupId>
    <artifactId>ofdrw-full</artifactId>
    <version>1.20.2</version>
    <exclusions>
        <exclusion>
            <artifactId>log4j-slf4j-impl</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
    </exclusions>
</dependency>

在springboot项目中,会出现log4j的依赖冲突,需要解决依赖冲突的问题

代码:

import io.swagger.annotations.ApiOperation;
import org.ofdrw.converter.ConvertHelper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



import java.io.*;

@RestController
public class OdfConversionController {

    @ApiOperation("文件转换,ofd转docx")
    @RequestMapping("/uploadOfdConvert")
    public ResponseEntity<byte[]> convertOfdDocx(@RequestParam("file") MultipartFile uploadedFile) throws IOException {

            // 使用OFD转换工厂将OFD文件转换为PDF字节数组
        InputStream ofdInputStream = uploadedFile.getInputStream();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ConvertHelper.toPdf(ofdInputStream,outputStream);

        byte[] pdfBytes = outputStream.toByteArray();

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=converted.pdf");

            // 构建并返回响应
            return ResponseEntity.ok()
                    .headers(headers)
                    .contentLength(pdfBytes.length)
                    .contentType(MediaType.APPLICATION_PDF)
                    .body(pdfBytes);

    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值