1

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 :-(

It's not possible to show the PDF with Desktop.getDesktop().open.

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

TT.
  • 15,774
  • 6
  • 47
  • 88
Cyberbunny
  • 145
  • 1
  • 2
  • 5
  • You could play web server, having a ServerSocket listening say on port 8080, and `Desktop.getDesktop.browse("http://localhost:8080/my.pdf")`. You have to deal with request header lines, and send your own header lines, but browser dev tools will show example data. – Joop Eggen Apr 01 '16 at 18:33
  • a web server will not run on the customers machine :-( – Cyberbunny Apr 01 '16 at 18:55
  • You cannot open a ServerSocket in java on a port? I meant playing oneself as webserver and let the browser open the pdf. – Joop Eggen Apr 01 '16 at 19:10
  • @JoopEggen Just FYI: your proposal is in violation with the [EULA of Adobe Reader DC](http://wwwimages.adobe.com/content/dam/acom/en/legal/licenses-terms/pdf/PlatformClients_PC_WWEULA-en_US-20150407_1357.pdf), more specifically with claus 3.2 Server Use: "This agreement does not permit you to install or Use the Software on a computer file server." If the OP uses your suggestion, he'll have to make sure that his users don't use Adobe Reader. See my answer to find out why that clause is in the EULA. – Bruno Lowagie Apr 02 '16 at 11:41
  • @BrunoLowagie it is sad hearing that, but if someone is a name in the field, then it is you. – Joop Eggen Apr 02 '16 at 17:31

1 Answers1

2

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 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.

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. Firewalls will usually complain about this. An application that suddenly starts acting as a web server is considered suspicious.
  2. The EULA of Adobe Reader prohibits the use of Adobe Reader on the same machine that creates and serves a PDF. Hence you'll have to make sure that your users don't use Adobe Reader or they'll be in violation with the EULA of Adobe Reader.

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

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

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).

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.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165