如何从响应中读取pdf内容并将其写入另一个新的pdf文件

发布于 2025-01-14 11:28:58 字数 451 浏览 2 评论 0原文

%PDF-1.4
%����
1 0 obj
<<
/Type /Catalog
/Pages 9 0 R
/Outlines 8 0 R
/Names 6 0 R

我正在尝试从 java 类中的其余端点读取上面的 pdf 内容响应,并尝试将其写入另一个文件 但文件已损坏,我无法查看生成的 pdf,

File file = new File("Data.pdf");-- trying to write data to this
FileOutputStream out = new FileOutputStream(file)
\\service call to download pdf document
out.write(response.getBody().getBytes());

如何将 pdf 内容写入另一个文件或以正确的方式生成新的 pdf?

%PDF-1.4
%����
1 0 obj
<<
/Type /Catalog
/Pages 9 0 R
/Outlines 8 0 R
/Names 6 0 R

i am trying to read above pdf content response from rest end point in java class and trying to write it to another file
but the file is getting corrupted and I could not view the pdf generated

File file = new File("Data.pdf");-- trying to write data to this
FileOutputStream out = new FileOutputStream(file)
\\service call to download pdf document
out.write(response.getBody().getBytes());

how to write the pdf content to another file or generate new pdf in a proper way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2025-01-21 11:28:58

基本上,您希望从 InputStream 读取数据,然后写入 OutputStream。这个问题已被回答多次,例如 此处此处这里有很多可能的解决方案。由于您还标记了 ioutils,一种可能的方法是:

File file = new File("Data.pdf");
FileOutputStream out = new FileOutputStream(file)
IOUtils.copy(response.getBody(), out);

这假定 response.getBody 返回一个 InputStream。如果您提供更多代码,我们可以确定。 (这取决于您使用的 Restclient 实现,例如 JAX-RS、Spring-Rest、Apache httpClient 或 HttpUrlConnection...

Basically you want to read from an InputStream and then write to an OutputStream. This question has been answered several times e.g. here, here and here and there are lots of possible solutions. Since you also tagged ioutils one possible way is to:

File file = new File("Data.pdf");
FileOutputStream out = new FileOutputStream(file)
IOUtils.copy(response.getBody(), out);

This presumes that response.getBody returns an InputStream. If you supply more code we can tell for sure. (This depends on your restclient implementation you are using like JAX-RS, Spring-Rest, Apache httpClient or HttpUrlConnection...

冬天旳寂寞 2025-01-21 11:28:58

iText 7 中的 PdfReader 类有一个重载版本,它采用 InputStream 作为参数。使用此方法,您基本上可以使用 ByteArrayInputStream 读取第一个输入 pdf 的字节。 iText 7 有一个 PDFWriter 类,也可以写入 OutputStream。请参考以下片段。然后,PdfDocument 类可以读取输入的 pdf 文件并使用 pdfWriter 将其写入新文件。

    //Pdf bytes returned by some rest API or method
    byte[] bytes = {};
    ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
    
    //File where you want to write the pdf and update some content
    File file = new File("Data.pdf");
    FileOutputStream out = new FileOutputStream(file);
    
    PdfDocument dd = new PdfDocument(new PdfReader(bin), new PdfWriter(out));

PdfReader class in iText 7 has got an overloaded version that takes InputStream as an argument. Using this method, you can basically read the bytes of your first input pdf using ByteArrayInputStream. iText 7 has got a PDFWriter class that can write to an OutputStream too. Please refer to the following snippet. PdfDocument class then can read the input pdf file and write it to a new file using pdfWriter.

    //Pdf bytes returned by some rest API or method
    byte[] bytes = {};
    ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
    
    //File where you want to write the pdf and update some content
    File file = new File("Data.pdf");
    FileOutputStream out = new FileOutputStream(file);
    
    PdfDocument dd = new PdfDocument(new PdfReader(bin), new PdfWriter(out));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文