• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Print pdf of a jsp page

 
rossella catalano
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a jsp page where there are some
graphics and other information.
Users can see these graphics and their information.
Now I want that users can print or save this page with graphics in pdf format, I'd like to add a button to print or save them.
I have tried with this code:

<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.StringWriter"%>
<%@page import="java.util.Scanner"%>
<%@page import="sun.misc.IOUtils"%>
<%@page import="java.io.InputStream"%>
<%@page import="com.itextpdf.text.Image"%>
<%@page import="java.net.URL"%>
<%@page import="com.itextpdf.text.PageSize"%>
<%@page import="com.itextpdf.text.Document"%>
<%@page import="com.itextpdf.text.Paragraph"%>
<%@page import="com.itextpdf.text.pdf.PdfWriter"%>

<%

response.setContentType("application/pdf");
Document document = new Document();
InputStream is = new URL("http://localhost:32381/tutorial1-war/ProvaCrosshair.jsp").openStream();

String inputStreamString = new Scanner(is,"UTF-8").useDelimiter("\\A").next();
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.addTitle("Document Title");
document.add(new Paragraph(inputStreamString));



document.close();
%>


But this code show me not the page with graphics but the source code of the jsp page.
How can I solve?
Can someone help me?
Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't just tell the browser that the document is a PDF and then give it plain text HTML. You need to either convert the HTML markup to PDF format (look for 3rd party libraries to do this), or generate PDF format on the fly (also libraries available).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And by the way, no matter what you are trying o do, putting Java code in a JSP is not acceptable.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing a PDF of a JSP page is trivial for me. I just use the File/Print menu option. But then again, I'm running Linux, and printing to PDFs is a stock part of the OS. Windows users have historically not had that ability and had to install third-party drivers.

Of course, the problem with that sort of PDF is that it lacks the absolute layout characteristics of a document that was pre-formatted as PDF. It reflects how my web browser laid out the document on my computer, based on resources available (such as what fonts I have installed).

Rendering a PDF on the server gives you the absolute formatting, but as Bear as said, JSPs are a horrible way to create them. And on Windows, you cannot display a PDF directly in Internet Explorer. Microsoft got sued for half a billion dollars for doing that and lost a long time ago, so they removed that ability many years ago. All you can do with a pre-formatted PDF is have if download and bring up a separate PDF viewer program.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic