所需依赖:
<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);
}
}