I have an export to excel link which when clicked Javascript calls action class.
JS looks like this:
function exportFt()
{
alert("Export Ft");
var url = "<c:out value='${model.contextPath}'/>/CaseIdsExport.do?";
document.forms[0].action=url;
document.forms[0].submit();
}
Struts-config looks like this:
type="com.avt.CaseExportToExcelAction"
name="CaseExportToExcelForm"
parameter="operation">
</action>
CaseExportToExcelAction open the excel and displays report. But what happens is my browsers itself opens excel. But i want my browser to stay and excel open up seperately. How do i do that?
try add this in your action / servlet:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="
+ manager.getReportName() + ".xlsx");
Related
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
I am trying to set the Title of the Browser as the PDF document Title.
Though the pdf generates correctly, i get the Title randomly.
response.setDateHeader("Expires", 0L);
response.setHeader("Content-disposition", "inline;filename=" + title + ".pdf");
response.setContentType("application/pdf");
response.setContentLength(bArray.length);
try {
response.getOutputStream().write(bArray);
response.getOutputStream().close();}
Could someone please help me out here to override the pdf title
You cannot set the title while at the same time sending a file down to the browser. For the browser to change the "page" title, it needs to actually render an HTML document (ie. it needs to render a page).
But there is a workaround to achieve it.
Redirect it to a page that renders out the title, . And from that page trigger the browser over to the download.
Try this !!!!
In html/jsp page:
<a href="pdf/filename.pdf">
In servlet:
String filename = request.getPathInfo().substring(1);//filename.pdf
Also you cen remove .pdf from title by simple java operation.
content-disposition: attachment
Similar question found here
Replace your code with
response.setHeader("Content-disposition", "attachment;filename=" + title + ".pdf");
response.setContentType("application/octet-stream");
response.setContentLength(bArray.length);
try {
response.getOutputStream().write(bArray);
response.getOutputStream().close();}
Read answer description here
set metadata response
I want to setup response for PDF output. How do I achieve this?
I can setup for Excel output and successfully get the desired excel sheet from the browser, but for PDF I could not get the desired output.
For Excel the following code works fine
String excelContent = "an html table..";
getServletResponse().setContentType("“application/vnd.ms-excel");
getServletResponse().setHeader("Content-Disposition","inline; filename=" + pageTitle + ".xls");
PrintWriter ps = getServletResponse().getWriter();
ps.println(excelContent);
But for PDF I tried setting the content type to PDF, but could not get it properly (no content gets displayed even though a PDF file is opened in the browser)
String excelContent = "an html table..";
getServletResponse().setContentType("“application/pdf");
getServletResponse().setHeader("Content-Disposition","inline; filename=" + pageTitle + ".pdf");
PrintWriter ps = getServletResponse().getWriter();
ps.println(excelContent);
Do html tables cannot be displayed as such in PDF?
It's very simple with the Flying Saucer renderer. It takes HTML input and returns PDF. What you have to do is to declare content type as PDF, generate HTML to a string, and call a method from Flying Saucer.
Here is an example
xhtml to pdf servlet with flyingsaucer
You are writing html context to the response stream. You need to write pdf format to the response.
You can use libraries like iText or Apache FOP to create PDF format.
I am exporting to JSP page using jasperexportmanager as below.
jasperPrint=JasperFillManager.fillReport(statReportSummaryJasper, param,jre);
ServletOutputStream servletOutputStream = res.getOutputStream();
res.setContentType("application/PDF");
JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
servletOutputStream.flush();
servletOutputStream.close();
This gets rendered in JSP page. When I try to download, It gives me option to save as html and not as PDF. How do I save this as PDF.
Any help or direction will be of great help. Thank you.
#Nagarajan....
add this code and try once again...
response.setHeader("Content-Disposition", "attachment; filename="
+ ReportName + ".pdf");
I just changed the code as below response.setHeader("Content-Disposition", "inline; filename=" + ReportName + ".pdf"); And it opened the PDF in the browser and the user can save the PDF if he wants. Thank you all again. Objective: Accomplised
This is a "non-programmers" answer!
Our bank generates statements in jsp which I wanted to save as pdfs (in order to email them to a person even less technical than I am). In the end, I just opened the downloaded jsp in my browser (Chrome) and printed it to a pdf. That was satisfactory for me.
I'm creating a workbook object(excel object "org.apache.poi.ss.usermodel.Workbook") on an ajax request.
How can I send this file from servlet to browser and let the browser pops up a download box.
You will have to set Content-Type to application/x-msdownload and Header to Content-Disposition
For example:
String fileName = URLDecoder.decode("MyBook.xlsx", "ISO8859_1");//Change if required
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment; filename="+ fileName);
//And just write output stream here
Since you have changed the response headers so a Browser will automatically display a popup message to download the file.
After a lot of googling I get to know its difficult to do with an ajax request.
So I decided to go with hidden form.
In javascript I created a hidden form and submitted the form So I am getting the excel download pop-up.
my javascript is given below.
var form = document.createElement("form"); //created dummy form for submitting.
var element1 = document.createElement("input");
form.method = "POST";
form.action = "/my/servlet/path";
element1.value=values; //its a json string I need to pass to server.
element1.name="data";
element1.type = 'hidden'
form.appendChild(element1);
document.body.appendChild(form);
form.submit();