目录
-
配依赖
-
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox --> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.24</version> </dependency>
pdf工具包
-
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency>
该包里面有所需的类型
-
-
代码部分
-
public String batchDownload(List<String> list) { try { PDDocument cpdDocument = new PDDocument(); for (String url : list) { PDDocument doc = PDDocument.load(new URL(url).openStream()); for (PDPage page : doc.getPages()) { cpdDocument.addPage(page); } } ByteArrayOutputStream bytes = new ByteArrayOutputStream(); cpdDocument.save(bytes); cpdDocument.close(); MultipartFile multipartFile = convert(bytes, getCurrentDateTime() + ".pdf"); bytes.close(); //这里的ossClient是我的oss服务调用,换成你自己的就行了,最后上传成功后获取url R<ImgVideoFileVo> ossRes = ossClient.putFile(multipartFile); return ossRes.getData().getLink(); } catch (Exception e) { throw new RuntimeException(e); } } private MultipartFile convert(ByteArrayOutputStream baos, String filename) { return new MockMultipartFile(filename, filename, "text/plain", baos.toByteArray()); } private String getCurrentDateTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); return sdf.format(new Date()); }
(new URL是因为我的pdf都是动态url,如果是本地文件用new File)
-