如何从响应中读取pdf内容并将其写入另一个新的pdf文件
%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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,您希望从
InputStream
读取数据,然后写入OutputStream
。这个问题已被回答多次,例如 此处、此处和这里有很多可能的解决方案。由于您还标记了 ioutils,一种可能的方法是:这假定
response.getBody
返回一个InputStream
。如果您提供更多代码,我们可以确定。 (这取决于您使用的 Restclient 实现,例如 JAX-RS、Spring-Rest、Apache httpClient 或 HttpUrlConnection...Basically you want to read from an
InputStream
and then write to anOutputStream
. 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:This presumes that
response.getBody
returns anInputStream
. 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...iText 7 中的 PdfReader 类有一个重载版本,它采用 InputStream 作为参数。使用此方法,您基本上可以使用 ByteArrayInputStream 读取第一个输入 pdf 的字节。 iText 7 有一个 PDFWriter 类,也可以写入 OutputStream。请参考以下片段。然后,PdfDocument 类可以读取输入的 pdf 文件并使用 pdfWriter 将其写入新文件。
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.