如何在不形成文件的情况下以byte []获取PDF?

时间:2018-10-25 09:53:19

标签: java pdf stream pdf-generation xsl-fo

下面是我的代码段:

 try (OutputStream out = new FileOutputStream(PDF_NAME)) {
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));
            Result res = new SAXResult(fop.getDefaultHandler());
            transformer.transform(new StreamSource(IOUtils.toInputStream(xml, "UTF-8")), res);
        }
    byte[] inputFile = Files.readAllBytes(Paths.get(PDF_NAME));
    String encodedFile = Base64.getEncoder().encodeToString(inputFile);
    InventoryListSnapshot pojo = new InventoryListSnapshot(invList.getInventoryLayoutId(), invList.getProjectId(), invList.getAuthorUsername(), encodedFile);
    repository.save(pojo);

它使用xsl-fo在文件中形成PDF。我需要将此Base64编码的PDF作为BLOB放置到数据库中-因此我不使用文件本身。

如何在不形成文件的情况下将PDF保存到数据库中?

2 个答案:

答案 0 :(得分:0)

您将对此进行更改:

OutputStream out = new FileOutputStream(PDF_NAME)

OutputStream out = new ByteArrayOutputStream()

答案 1 :(得分:0)

谢谢,没问题。 新版本是:

productRepository()