编辑
2024-01-09
学习记录
00
请注意,本文编写于 450 天前,最后修改于 447 天前,其中某些信息可能已经过时。

目录

依赖
代码

依赖

xml
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.23</version> </dependency>

代码

java
//单页pdf public static String pdfToImage(String pdfBase64) throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(pdfBase64)); PDDocument document = PDDocument.load(inputStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PDFRenderer renderer = new PDFRenderer(document); for(int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = renderer.renderImageWithDPI(i, 300); ImageIO.write(image, "png", outputStream); } byte[] imageBytes = outputStream.toByteArray(); String imageBase64 = Base64.getEncoder().encodeToString(imageBytes); document.close(); return imageBase64; }
java
//多页pdf public static String[] pdfToImages(String pdfBase64) throws IOException { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(pdfBase64)); PDDocument document = PDDocument.load(inputStream); String imageBase64[] = new String[document.getNumberOfPages()]; for(int i = 0; i < document.getNumberOfPages(); i++) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PDFRenderer renderer = new PDFRenderer(document); BufferedImage image = renderer.renderImageWithDPI(i, 300); ImageIO.write(image, "png", outputStream); byte[] imageBytes = outputStream.toByteArray(); imageBase64[i] = Base64.getEncoder().encodeToString(imageBytes); } document.close(); return imageBase64; }
java
//多线程的方式 public static String[] pdfToImagesByMultiThreading(String pdfBase64) throws IOException, ExecutionException, InterruptedException { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(pdfBase64)); PDDocument document = PDDocument.load(inputStream); int pages = document.getNumberOfPages(); int core=Runtime.getRuntime().availableProcessors(); // 创建一个固定大小的线程池 ExecutorService executorService = Executors.newFixedThreadPool(core>pages?pages:core); List<CompletableFuture<String>> futures = new ArrayList<>(); for (int i = 0; i < pages; i++) { final int pageIndex = i; CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PDFRenderer renderer = new PDFRenderer(document); try { BufferedImage image = renderer.renderImageWithDPI(pageIndex, 300); ImageIO.write(image, "jpg", outputStream); } catch (IOException e) { e.printStackTrace(); throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(),"pdf转换出错"+e.getMessage()); } byte[] imageBytes = outputStream.toByteArray(); return Base64.getEncoder().encodeToString(imageBytes); }, executorService); futures.add(future); } // 等待所有任务完成并收集结果 CompletableFuture<String>[] arrayFutures = futures.toArray(new CompletableFuture[0]); CompletableFuture.allOf(arrayFutures).join(); // 等待所有任务完成 // 关闭线程池(在实际应用中考虑在服务停止时关闭) executorService.shutdown(); document.close(); // 等待所有任务完成并收集结果 String[] imageBase64 = new String[pages]; for (int i = 0; i < pages; i++) { imageBase64[i] = futures.get(i).get(); } return imageBase64; }

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!