pdf文件上传 生成图片进行保存

该博客介绍了如何使用Apache PDFBox库将PDF文件转换为图片,并通过后端Controller和Service层实现文件上传及处理。文章详细展示了依赖引入、文件上传校验、PDF转图片的代码实现,并提供了上传后的信息返回。
摘要由CSDN通过智能技术生成
<!--PDF转图片-->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jempbox</artifactId>
            <version>1.8.11</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>xmpbox</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>preflight</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.0</version>
        </dependency>

Cotroller层

 @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, HttpServletRequest req) throws IOException {
        //判null,文件是空后续则不进行
        if (file.isEmpty() || file.getSize() <= 0) {
            return AjaxResult.error("文件上传失败");
        }
        //判断文件格式,改功能模块只允许上传PDF文件
        String extension = FileUploadUtils.getExtension(file);
        if (!"pdf".equals(extension)) {
            return AjaxResult.error("文件格式异常,只能上传PDF文件");
        }
        Map<String, Object> map = tMonitoringReportService.upload(file, req);
        if (map == null) {
            return AjaxResult.error("文件上传失败");
        }
        return AjaxResult.success(map);
    }

Service层

 @Override
    public Map<String, Object> upload(MultipartFile file, HttpServletRequest req) throws IOException {
        String userName = SecurityContextHolder.getContext().getAuthentication().getName();
        //获取文件名字
        String fileName = file.getOriginalFilename();
        String basePath = "/file";
        String fileUploadPath = "";
        try {
            fileUploadPath = FileUploadUtils.upload(项目相关配置.getProfile() + basePath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        } catch (InvalidExtensionException e) {
            e.printStackTrace();
            logger.error("上传文件保存本地失败");
            return null;
        }
        String fileUploadName = fileUploadPath.split("/")[fileUploadPath.split("/").length - 1];
        //保留基础上传路径
        String replace = fileUploadPath.replace("/profile", "").
                replace(fileUploadName, "");
        List<String> list = PdfUtil.getPdfs(replace, fileUploadName);
        if (list == null) {
            return null;
        }
        String images = StringUtils.strip(list.toString(), "[]");
        Map<String, Object> map = new HashMap<>();
        map.put("list", images);
        map.put("fileName", fileName);
        map.put("url", fileUploadPath);
        map.put("userName", userName);
        map.put("type", 1);
        map.put("suffixName", FileUploadUtils.getExtension(file));
        return map;
    }

工具类

public class PdfUtil {

    private static final Logger log = LoggerFactory.getLogger(PdfUtil.class);

    public static List<String> getPdfs(String basePath, String filename) throws IOException {
        PDDocument document = PDDocument.load(new File(LX5in1Config.getProfile() + basePath + filename));
        Splitter splitter = new Splitter();
        List<PDDocument> Pages = splitter.split(document);
        List<String> list = new ArrayList<>();
        try {
            int j = 1;
            for (PDDocument page : Pages) {
                PDFRenderer renderer = new PDFRenderer(page);
                BufferedImage image = renderer.renderImage(0);
                String report_images = LX5in1Config.getProfile() + basePath + System.currentTimeMillis() + j + ".jpg";
                ImageIO.write(image, "JPEG", new File(report_images));
                list.add(Constants.RESOURCE_PREFIX + basePath + System.currentTimeMillis() + j + ".jpg");
                j++;
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("PDF转图片失败");
            return null;
        }
        log.info("上传PDF已转换为图片");
        document.close();
        return list;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值