网络PDF文件转图片并通过nginx代理预览

一、网络PDF转图片

javascript 复制代码
/**
     * @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;
                //文件名存入list
                fileList.add(shortPath);
            }
            return Pair.of(true, fileList);
        } catch (Exception e) {
            e.printStackTrace();
            return Pair.of(false, "PDF转化图片异常");
        }
    }

二、配置nginx预览

相关推荐
ping某4 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
weixin_397574096 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
Metaphor6926 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
2601_961845156 天前
粉笔行测5000题电子版|pdf|解析
pdf·新媒体运营·github·个人开发·内容运营·规格说明书·极限编程
Sour6 天前
PDF翻译卡住不动怎么办?扫描件、OCR 和大文件排查清单
前端·pdf·ocr
難釋懷6 天前
Nginx反向代理中的容错机制
运维·nginx
bloglin999996 天前
Nginx高危漏洞CVE-2021-23017及配置样例
运维·nginx
进阶的小名6 天前
Spring Boot SSE + Nginx 配置:解决 EventSource 不实时返回、连接超时、流式响应被缓冲问题
spring boot·后端·nginx
難釋懷6 天前
Nginx获取客户端真实IP
服务器·前端·nginx
qq_谁赞成_谁反对6 天前
甲方IT的成长之路--nginx实战--2604
服务器·数据库·nginx