显示存储在ByteArrayOutputStream中的PDF文档(不在文件中) [英] Display PDF document stored in a ByteArrayOutputStream (not in a file)

查看:309
本文介绍了显示存储在ByteArrayOutputStream中的PDF文档(不在文件中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在文件中创建了PDF文档(带有iText),并可以在屏幕上显示以下内容:

I've created a PDF document (with iText) in a file and can diplay it on the screen with:

Document document = new Document();       
PdfWriter.getInstance(document, filename);
document.open();
// ... write something to document
document.close();
Desktop.getDesktop().open(new File(filename));  // works fine  :-)

但是在客户的机器上,我的程序无法访问文件系统,因此我尝试了以下操作:

But on the machine of the customer my program will not have access to the filesystem, so I tried this:

Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document,baos);
document.open();
// ... write something to document
document.close();

哪个工作,但是(当然)然后

Which works, BUT then (of course)

Desktop.getDesktop().open(new File(baos));  //doesn't work :-(

无法显示<$ c $的PDF c> Desktop.getDesktop()。open 。

是否有办法显示存储在ByteArrayOutputStream中的PDF?

Is there a way to display the PDF stored in the ByteArrayOutputStream anyway?

推荐答案

PDF查看器(如Adobe Reader)需要文件系统上的PDF,即使通过Web服务器提供PDF,Adobe Reader也会下载

PDF Viewers such as Adobe Reader need the PDF on the file system. Even if the PDF is served through a web server, Adobe Reader will download a local version to the client machine.

PDF查看器(例如Adobe Reader)不接受字节流。您无法打开Adobe Reader并提供字节流以供您必须始终传递文件路径。

PDF Viewers such as Adobe Reader do not accept byte streams. You can't open Adobe Reader and "serve" a byte stream to it. You must always pass a path to a file.

您可以通过将PDF通过网络服务器提供给浏览器来解决此问题。您可以使用 ServerSocket 用Java创建自己的Web服务器,但是:

You could work around this by serving a PDF through a web server to a browser. As indicated in the comments, you could create your own web server in Java using a ServerSocket, however:


  1. 防火墙通常会对此抱怨,认为突然开始充当Web服务器的应用程序是可疑的

  2. Adob​​e Reader的EULA禁止在创建和提供PDF的同一台计算机上使用Adobe Reader。因此,您必须确保您的用户不使用Adobe Reader,否则将违反Adobe Reader的EULA。

尤其是最后一个限制使 Joop Eggen 的评论无效。请参见用于Adobe Reader DC的EULA

Especially that last limitation makes the comment by Joop Eggen void. See section 3.2 of the EULA for Adobe Reader DC:


3.2服务器使用。此协议不允许您在计算机文件服务器上安装或使用该软件。

3.2 Server Use. This agreement does not permit you to install or Use the Software on a computer file server.

此子句是在Adobe发现人们正在构建本地服务器产品,从而允许人们使用免费的Adobe Reader提供仅在Adobe Professional中提供的功能(这是免费的)。

This clause was added after Adobe found out that people were building local server products that allowed people to use the free Adobe Reader to provide functionality that was only available in Adobe Professional (which is not free).

长话短说:您正在尝试执行最常见的PDF查看器无法完成的操作。您必须将应用程序与自定义PDF查看器一起运送。

Long story short: you are trying to do something that isn't possible with the most common PDF viewer. You'll have to ship your application with a custom PDF viewer.

这篇关于显示存储在ByteArrayOutputStream中的PDF文档(不在文件中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆