一、网络PDF转图片
/*** @param pdfOss 原文件* @param desFilePath 生成图片的路径* @param desFileName 生成图片的名称(多页文档时会变成:名称+下划线+从1开始的数字)* @param imageType 图片类型* @return*/public static Pair<Boolean, Object> pdfToImage(String pdfOss, String desFilePath, String desFileName, String imageType) {File destination = new File(desFilePath);if (!destination.exists()) {destination.mkdirs();}try {URL url = new URL(pdfOss);PDDocument document = PDDocument.load(url.openStream());PDFRenderer pdfRenderer = new PDFRenderer(document);//获取PDF文档的页数int pageCount = document.getNumberOfPages();System.out.println("文档一共" + pageCount + "页");List<String> fileList = new ArrayList<>();for (int i = 0; i < pageCount; i++) {//只有一页的时候文件名为传入的文件名,大于一页的文件名为:文件名_自增加数字(从1开始)String realFileName = pageCount > 1 ? desFileName + "_" + (i + 1) : desFileName;//每一页通过分辨率和颜色值进行转化BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(i, 400, ImageType.RGB);String filePath = desFilePath + File.separator + realFileName + "." + imageType;//写入文件ImageIO.write(bufferedImage, imageType, new File(filePath));String shortPath = "/pdf2img/"+ realFileName + "." + imageType;//文件名存入listfileList.add(shortPath);}return Pair.of(true, fileList);} catch (Exception e) {e.printStackTrace();return Pair.of(false, "PDF转化图片异常");}}
二、配置nginx预览