首页 > 解决方案 > Apache PDFBox:有没有办法只使用流?

问题描述

我的代码需要从 S3 InputStream 中提取图像,创建 PDF,将其添加到 ZipOutputStream,最后使用 OutputStream 将其发送回用户。我真的很想在不将任何字节读入内存的情况下完成此操作。目前我正在使用以下代码执行上述所有操作(减去 PDF 转换):

    public void myFunction(List<String> fileKeys, OutputStream responseStream) throws Exception {
        ZipOutputStream zipOutput = new ZipOutputStream(responseStream);

        for (String fileKey : fileKeys) {
            S3Object s3Obj = getS3Object(fileKey);
            try (InputStream data = s3Obj.getObjectContent()) {
                ZipEntry entry = new ZipEntry(fileKey);
                zipOutput.putNextEntry(entry);
                IOUtils.copy(data, zipOutput);
                zipOutput.closeEntry();
            } catch (Exception e) {
                throw e;
            }
        }

        zipOutput.close();
    }

但是,我一生都无法弄清楚如何让 PDFBox 不将图像字节读入内存。也许这是不可能的?我现在有一些工作代码,但监视 JVM 堆显示有很多东西进入内存。

    public void myFunction(List<String> fileKeys, OutputStream responseStream) throws Exception {
        ZipOutputStream zipOutput = new ZipOutputStream(responseStream);

        for (String fileKey : fileKeys) {
            S3Object s3Obj = getS3Object(fileKey);
            try (InputStream data = s3Obj.getObjectContent()) {
                ZipEntry entry = new ZipEntry(fileKey);
                zipOutput.putNextEntry(entry);

                BufferedImage bim = ImageIO.read(data);
                PDDocument doc = new PDDocument();
                PDPage page = new PDPage();
                doc.addPage(page);
                PDImageXObject pdImage = JPEGFactory.createFromImage(doc, bim);
                PDPageContentStream contents = new PDPageContentStream(doc, page);
                contents.drawImage(pdImage, 50, 50);

                contents.beginText();
                contents.setFont(PDType1Font.HELVETICA, 8);
                contents.showText("Document: " + fileKey);
                contents.endText();

                contents.close();
                COSWriter writer = new COSWriter(zipOutput);
                writer.write(doc);
                doc.close();

                zipOutput.closeEntry();
            } catch (Exception e) {
                throw e;
            }
        }

        zipOutput.close();
    }

标签: javapdfbox

解决方案


推荐阅读