Getting the response as PDF in browser - java

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.

Related

How to write data to pdf file which contains html tags using itext lib in Java

I have String which contains some html tags and it is coming from database, i want to write that in PDF file with same styling present in the String in the form of HTML tag. I tried to use XMLWorkerHelper like this
String html = What is the equation of the line passing through the
point (2,-3) and making an angle of -45<sup>2</sup> with the positive
X-axis?
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new
StringReader(html));
but it only reads the data which is inside the html tag(in this case only 2) other string it simply ignores. But i want the entire String with HTML formating.
With HTMLWorker it works perfectly but that is deprecated so please let me know how to achieve this.
I am using iText 5 lib

Convert html to pdf automatically using ASPOSE in Java

I am trying to convert html to pdf using aspose,also i have to use PageSize A1,A2,A3,A4 .this is worked perfectly..but i dont want set pagesize for pdf generation.So far i have tried below code
HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
htmloptions.getPageInfo().setWidth(PageSize.getA2().getWidth());
htmloptions.getPageInfo().setHeight(PageSize.getA2().getHeight());
// Load HTML file
Document doc = new Document(basePath + "400010_DOC002_L_10_2508016.html", htmloptions);
// Save HTML file
doc.save("D:/Web+URL_output.pdf");
Can anyone suggest with out set page size i have convert html to pdf conversion ? or else please let me know what tools are available for this. Please let me know any other tools for this conversion.
#Shankar, you may use the below code sample in order to convert an HTML file to a PDF file without setting page size. By default, the page size of the rendered PDF file will be as of the A4 page size.
Simply omit the code which is setting a page size, else remains the same.
HtmlLoadOptions htmloptions = new HtmlLoadOptions(basePath);
// Load HTML file
Document doc = new Document(basePath + "400010_DOC002_L_10_2508016.html", htmloptions);
// Save HTML file
doc.save("D:/Web+URL_output.pdf");
Please let us know if you need any further assistance. I work with Aspose as Developer Evangelist.

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)

How to set pdf title in Servlet Response

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

Converting HTML files into PDF

I am using the following code to generate a PDF file of the HTML Report
String url = new File("Test.html").toURI().toURL().toString();
OutputStream os = new FileOutputStream("Test.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
I was able to use it on sample HTML files to convert to pdf. But when it comes to my real usage, the HTML content consists of various special symbols, like &,<,> that can't be parsed by XML.
I tried using CDATA, while generating HTML itself, but later found that the text around CDATA is not visible in HMTL.
Does anyone have a solution for this?
Have you tried to print to pdf from the browser? Google primo pdf for a program that we'll let you do it.
I don't know if this will help you, but you can use StringEscapeUtils from apache-commons. It has methods for escape and unescape HTML (you may use them to pre-process your HTML before PDF generation).

Categories