Chandler E.
Chandler E.

Reputation: 113

Have PDF appear in HTML rather than loading with Adobe or into separate window

I have a java web application that in theory, when working correctly, will display a PDF in the browser window next to some input text boxes so that an employee could index the document and store it away.

JSP URL: localhost:1234/Application

What is currently happening is this:

->User tells JSP to tell servlet to go into filesystem and grab a batch of PDFs

-->JSP displays which batch(folder) was grabbed, and the files contained inside the folder to the screen

EXAMPLE:

Folder1:

-document1 (button)

-document2 (button)

-document3 (button)

--->Person clicks on button that appears next to document name on the JSP and the servlet serves up the PDF data in a byte array as follows:

File file = new File(Dir + "\\" + batchName + "\\" + fileName);
byte[] by = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(by);
fis.close();

response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=TheDocument." + "pdf");
response.getOutputStream().write(by);
response.getOutputStream().flush();
response.getOutputStream().close();

The PDF then opens in a SEPARATE WINDOW than what the application is running on, and the PDF file is downloaded to the machine into the downloads folder

What happens after this does not matter in the scope of this question.

What I WANT to happen is this:

  1. User tells JSP to tell servlet to go into filesystem and grab a batch of PDFs
  2. JSP displays which batch(folder) was grabbed, and the files contained inside the folder to the screen
  3. Person clicks on button next to document name in the JSP and the servlet serves up the PDF document and loads it into the html page so that the person can view it in real time on the same window and do their indexing.

Changing the header to inline does not give me the desired effect as it loads the PDF in the same browser window, but only by clicking back can I get back to my JSP. It essentially loads up a whole new HTML/JSP window. The browser actually points right to my servlet in this case:

localhost:1234/Application/Servlet

Below is what I want the application to look like:

ApplicationTemplate

I've looked into several jquery plugins such as PDFObject, Colorbox and many others. I do not seem to get the desired effect that I want.

Upvotes: 0

Views: 642

Answers (3)

src
src

Reputation: 215

You could use pdf.js to render the pdf, with a Flash fallback for browser that aren't supported by pdf.js.

The helloworld example renders a pdf in a <canvas> element.

Upvotes: 1

isaac weathers
isaac weathers

Reputation: 1472

Not sure if this is what you are looking for but typically you can put a PDF in an iframe and have it opened up that way. The PDF just needs to be published on your server.

This answer may help you:

How to open a PDF file in an <iframe>?

Upvotes: 0

developerwjk
developerwjk

Reputation: 8659

Whether a PDF opens in a separate application or in the browser is a setting in the browser, in Adobe, and in Windows Explorer (assuming Windows). You have no control over it, short of displaying your PDF in a Flash app, and then the user might not have Flash.

Upvotes: 0

Related Questions