PDF转图片,并完成图片存储

public MediaRecordDTO queryOrderPrintPictureName(String mailNo, String filePath) {
        //链接
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        try {
            URL url = new URL(orderPrintUrl + "/api/v2/orderPrint/print?account=E9&mailNo=" + AESUtil.encrypt(mailNo, AESUtil.ORDER_PRINT_KEY));
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(2000);
            connection.setReadTimeout(2000);
            connection.setRequestMethod(HttpMethod.GET.name());
            connection.connect();
            if (connection.getResponseCode() == HttpStatus.OK.value()) {
                inputStream = connection.getInputStream();
                PDDocument doc = PDDocument.load(inputStream);
                PDFRenderer renderer = new PDFRenderer(doc);
                //获取PDF的第一页pageIndex = 0;dpi越大转换后越清晰,相对转换速度越慢
                log.info("PDF转图片开始,mailNo:{}", mailNo);
                BufferedImage image = renderer.renderImageWithDPI(0, 128);
                log.info("PDF转图片结束,mailNo:{}", mailNo);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
                ImageIO.write(image, "jpg", imgOut);
                inputStream = new ByteArrayInputStream(bs.toByteArray());
                //单位是byte
                int fileSize = inputStream.available();
                imgOut.flush();
                bs.close();
                String fileName = mailNo + ".jpg";
                minioManager.putObject(MinioManager.BUCKET_NAME, filePath, inputStream);
                inputStream.close();
                doc.close();
                MediaRecordDTO mediaRecordDTO = new MediaRecordDTO();
                mediaRecordDTO.setMediaName(fileName);
                mediaRecordDTO.assigSize(fileSize);
                return mediaRecordDTO;
            } else {
                log.error("返回异常,单号:{},connection:{}", mailNo, connection);
                throw new BizException("请返回异常,单号:" + mailNo);
            }
        } catch (Exception e) {
            log.error("异常", e);
            throw new BizException("异常:" + e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("关闭InputStream异常,单号:{},异常:{}", mailNo, e);
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <!-- 不需要编译的资源文件 -->
                        <nonFilteredFileExtensions>
                            <nonFilteredFileExtension>
                                ttf
                            </nonFilteredFileExtension>
                        </nonFilteredFileExtensions>
                    </configuration>
                </plugin>

 

在部署到linux服务器上遇到了很麻烦的问题,因为linux服务器没有中文语言包,所以出现了中文乱码的问题,踩坑许久,终于解决了。

解决使用pdfbox转图片中文乱码问题

PDFbox的head is mandatory问题

nonFilteredFileExtension的使用和exclude的区别---解决maven打包后文件不可用问题

下面这篇应该可以不用,不用这么复杂

使用pdfbox2.0.15版本,将pdf文件转为图片时,中文显示乱码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值