How to open PDF file in new window in browser? - java

I'm trying to open a PDF file by requesting it from Rest endpoint. But in this case, the file is getting downloaded. I tried opening it in another window. There also it just shows new window and downloads it there.
I used Chrome. So in Chrome by default it downloads the file while in Firefox it shows the dialog box whether to open it or save it. I don't want that dialog box. But want to display the file in new window with all the features like download, print, etc which a normal pdf viewer will show.
Is there some way through which I can avoid downloading of the file by default and just display that file in another window? Content-Disposition is attachment; filename="abc.pdf" when I see the properties of the URL. Also, its content-type is application/pdf;charset=utf-8.
<a target="_blank" data-content-type="application/pdf" onclick="open(this.href, this.target, 'fullscreen=yes'); return false;" data-type="downloadTenPointDocument" href="<c:out value="${resultItem.offer.programInfoUrl}"/>">View 10-point</a>

I changed the properties of pdf file from attachment to inline and set it in response:
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=" + fileName + ".pdf");
I am requesting the pdf file from server and then reading it and setting it in response, changing the properties of it in response, so that its default behavior of getting downloaded is overwritten.
Thank you everyone. The answer to change the property of pdf file was right.

You won't be able to do it directly from REST endpoint.
Use target="_blank" in anchor element on your page.
go

You should try to erase attachment; property from content disposition header.

i did like that and it worked: (it wont start to download, and it will open in a new tab (target _blank) if same tab, thenit is (target _self)
show pdf in a new tab

Related

Java - Send OutputStream to printer directly from servlet/Controller

Using my spring controller I want to directly open the printing view of generated pdf.By now I am generating a pdf using iTextPDF and put it to the OutputStream (HttpServletResponse.getOutputStream()).It is downloading the pdf. I can open it in browser and print using print button.
What I want is getting the print UI from the controller or without print UI send to the printer.
I have added some of my controller method,
String mimeType = "application/pdf";
System.out.println("MIME type: " + mimeType);
response.setContentType(mimeType);
String headerKey = "Content-Disposition";
String headerValue = String.format("theCoder379.PDF");
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
createPdf(outStream, theObject);
outStream.close();
In createPdf(outStream, theObject) method it is adding the generated iText pdf using 'theObject' to the 'outStream'.
How can I achieve this.
It is not possible the way you want (from the server side).
By now I am generating a pdf using iTextPDF and put it to the OutputStream
Good. That's effectively all you can do from the server side code.
I can open it in browser and print using print button
The browser loads an external application (pdf plugin/viewer) and let the plugin display the file you've generated. You have no control over it whatsoever.
However you may create your own page with embedded plugin.
<object data="./pdfServlet" type="application/pdf" width="100%" height="100%">
<p>Alternative text - include a link to the PDF!
</p>
</object>
and try to invoke the print event in the client-side javascript.
window.print();
However - I see some potential issues
it may not work on all browsers and all version you may want
some people (myself included) are sensitive if a web page tries to do thing the are not explicitly asked for (such as printing)

set Src of Iframe of uploaded PDF client side

I have uploaded a PDF on server by servlet , get that file on client side. Now I want to set the file to the src of Iframe. I have seen the examples but they are setting the src like src='http://www.tutorialspoint.com/java/java_tutorial.pdf?file=http://www.tutorialspoint.com/java/java_tutorial.pdf but I have the file with name like "file12314232343244"
I am setting the src like this
HTML pdf = new HTML("<iframe position='absolute'src='http://www.tutorialspoint.com/java/java_tutorial.pdf?file=http://www.tutorialspoint.com/java/java_tutorial.pdf' />");
what should I do for that ?
Use the type parameter:
type="application/pdf"
as shown https://stackoverflow.com/a/42908837/2979092

GWT attached pdf document in post response

I have an issue with http response.
I send some data and in response I get attached pdf document, and I need to open it document in a new browser tab.
I can't do it using Window.Open(String url, String title, String... parameters) because I dont have this url.
if I open it with right click on and tell firebug "Open in a new Tab" I got an url
In order to display pdf in your page you should have Content-Type: application/pdf in your response header. So, You should set in response header in your servlet which is responsible that given url.
After that you can put your pdf inside one of these html tags embed, object or iframe. I think gwt's Frame is a good way to do it. See this tutorial as well: 4 Ways To Stream Pdf and Some Tips

How to get all file paths while using html "file" input with multiple attribute

I am developing a web page in which I have to upload multiple files on
a single browse.
I am using html <input id="filelist" type="file" multiple=multiple>
This enables the multiple file selection and also retrieves the
full file path of all the selected file, which shows in file
upload text area.
<script language="JavaScript">
<!--
function showname(){
var filepath = document.form1.filelist.value ;
alert(filepath); //this shows only first filename among selected file
}
-->
</script>
But the problem is when I get the value of input, it returns only the first file
name among selected files.
Now how can I get the file paths which is shown in file upload text area.
Thanks!
This is browser specific. So you might be running this in a browser that doesn't support this. For example Firefox does. Here's an example of how to use this feature:
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/
I would consider using http://www.uploadify.com/about/ or http://www.fyneworks.com/jquery/multiple-file-upload/. They should help you out, and also add some cool features to your file upload form.
File paths are deliberately hidden from the page for security purposes. To look at the local filesystem you need to use Java, Active-x (meh), or Flash.

Output PDF file contents stored as BLOB using AJAX call without creating the file

I need to open a pdf file for the user who clicks on a download button.
The download button makes a ajax call to a servlet which gets data from a blob field in the database containing PDF contents, and returns it as a response.
What can I do in order to make the response to be downloaded as a PDF file to the user.
The servlet code is as below:
response.setContentType("application/pdf");
oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
I receive a long response containing characters like the ones below in AJAX when I checked the Fire bug for the response.
�a�J��㔎��ji�2K���y�2��q F�f�9�G��!%�kɂ��W��������mp){h̕�S���NJ_�A����'����2k��j���яR�>wB�e�|=w�p�%w��qǦ>�~�1o�㾙9j�B�;aNx3�`z��طc�O��ï��$�;�N|;�xۇ��;�-�c�f�M��c���(�f�M6K���
I don't want to submit the page or to popup a window for the servlet with the parameters sent for the query showing in the URL
I also don't want to create the file on the server.
Is there a way to take the response of servlet coming in ajax call and display it to a page or Iframe and the browser automatically downloads the file...
Its is NOT possible to download a file using Ajax. However, the same 'effect' can be achieved using a hidden IFRAME. Instead of using XMLHttpRequest, dynamically create a hidden iframe and submit it to the servlet with the proper parameters. When the response comes, the browser will automatically handle the content based on the content-type/extension. If you are using jQuery, then this plugin has this functionality in-built.
Sounds like Rahulmohan knows his stuff but it might be worth trying to add a filename header to your response stream. I do something similar in asp.net but not in an Ajax environment and the only difference between my code and yours is this line:
Response.AddHeader("Content-Disposition", "attachment; filename=" & Filename & ".pdf")
My page doesn't run in an Ajax environment though.

Categories