Title: JSP, PDF, taglibs
Fredrik,
 
When you run my example are you forwarding from a Servlet to get to that page, or are you just entering the url to the jsp into the address line?  Because when I view the JSP by typing into the address it comes up fine.  But when I forward to the JSP, it doesn't work.  I keep getting that Illegal State Exception for calling both the getWriter and getOutputStream.  If I could get away with using the writer to print the PDF, then thats fine, but I don't know how to achieve this.  Any ideas?  I'd Appreciate it.  Right now I am just having trouble writing my file to the client after a forward.  I haven't even gotten to start making my tags yet, so any help would greatly help...
 
Daniel
-----Original Message-----
From: Fredrik Nilsson [mailto:[EMAIL PROTECTED]]
Sent: Friday, December 27, 2002 6:51 AM
To: [EMAIL PROTECTED]
Subject: Re: [iText-questions] JSP, PDF, taglibs

Daniel,
 
Both your code and my code-snippet works just fine for me:
Try to open them in a new window by using the javascript below.
 
****** freniltest.jsp *****
<%@ page import="com.lowagie.text.*" %><%@ page import="java.io.*" %><%@ page import="com.lowagie.text.pdf.PdfWriter"%><%@ page impo
rt="com.lowagie.text.pdf.ColumnText"%><%@ page import="com.lowagie.text.pdf.PdfContentByte"%><%@ page import="com.lowagie.text.pdf.BaseFont"%><%                    
                                                                                                                                   
java.awt.Color border = new java.awt.Color(0xED, 0x1C, 0x24);                                                                      
Font font1 = new Font(Font.HELVETICA, 14, Font.NORMAL, border);                                                                    
Font font2 = new Font(Font.HELVETICA, 24, Font.BOLD, border);                                                                      
Font font3 = new Font(Font.HELVETICA, 12, Font.NORMAL, border);                                                                    
Document document = new Document(PageSize.A4, 30, 30, 80, 50);                                                                     
document.addSubject("Test");                                                                                                       
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();                                                          
PdfWriter writer = PdfWriter.getInstance(document, baos);                                                                          
document.open();                                                                                                                   
Paragraph p = new Paragraph("Hello world",font1);                                                                                  
document.add(p);                                                                                                                   
document.newPage();                                                                                                                
document.close();                                                                                                                  
response.setContentType("application/pdf");                                                                                        
response.setContentLength(baos.size());                                                                                            
ServletOutputStream myOut = response.getOutputStream();                                                                            
baos.writeTo(myOut);                                                                                                               
myOut.flush();                                                                                                                     
%>                    
******SNIPP!*****
 
******** SNIPP! index.jsp ****
<html>                                                   
<head>                                                   
<script>                                                 
function showPrintFrenilTest()                           
{                                                        
        window.open("freniltest.jsp","myWin");           
}                                                        
</script>                                                
</head>                                                  
<body>                                                   
<br>                                                     
<a href="">Freniltest</a>
<br>
</body>                                                  
</html>                                                  
******* SNIPP! *********
 
I use the index.jsp to call on the freniltest.jsp via a javascript showPrintFrenilTest()
I also noticed that you do not use the setContentLength()
 
Regards,
/Fredrik
 
                                                                                                             
----- Original Message -----
Sent: Friday, December 27, 2002 3:18 PM
Subject: RE: [iText-questions] JSP, PDF, taglibs

Fredrik,
 
Here is the contents of my JSP so far...
 
<%
response.setContentType("application/pdf");
java.io.ByteArrayOutputStream buffer = new java.io.ByteArrayOutputStream();
com.lowagie.text.Document document = new com.lowagie.text.Document();
com.lowagie.text.pdf.PdfWriter writer = com.lowagie.text.pdf.PdfWriter.getInstance( document, buffer );
document.open();
document.add(new com.lowagie.text.Paragraph("Hello World"));
document.close();
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
    out.write(bytes[i]);
}
%>
 
Although when I write out to the screen, all I get is....
 
%PDF-1.4 %???? 3 0 obj <> stream x?+?r ?26S?00SI?2P?5?1??  ?B??4> /ProcSet [/PDF /Text] >> /Parent 1 0 R /MediaBox [0 0 595 842] /Contents 3 0 R /Type /Page >> endobj 2 0 obj <> endobj 1 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000386 00000 n 0000000290 00000 n 0000000015 00000 n 0000000145 00000 n 0000000442 00000 n 0000000490 00000 n trailer <> startxref 600 %%EOF
 
I have to use the "out" because for some reason when I forward to my JSP from a servlet, if I then try to call response.getOutputStream() is throws an Illegal State Exception saying that the getWriter has already been called. (I am using JBoss 3.0.2/Tomcat 4.0.4)  Is there any way that I use the write method to display the file? Have you had any luck with that?  I mean, another way would be to prompt the user to download the file to their computer, but then again, I had no luck with that as well.  Anyways, thanks in advance.
 
Daniel
-----Original Message-----
From: Fredrik Nilsson [mailto:[EMAIL PROTECTED]]
Sent: Thursday, December 26, 2002 10:59 PM
To: [EMAIL PROTECTED]
Subject: Re: [iText-questions] JSP, PDF, taglibs

 Hi Daniel,
 
It is pretty straight forward as in the java code.
 
One thing to be careful about is that you cannot have any newlines ("enters") outside of the jsp-tags.
(I.e. the import page in the beginning of the jsp-file must be on the same row)
 
Regards,
Fredrik
 
----- Original Message -----
Sent: Friday, December 27, 2002 12:01 AM
Subject: [iText-questions] JSP, PDF, taglibs

Hi,

I am trying to use iText with taglibs and am wondering if anyone has ever played with that before.  I basically want to have iText created a PDF document using taglibs from a JSP page, and then just write the document to the browser and let the browser deal with opening/downloading it, etc.  I am wondering if anyone has ever done something like this before?  The context in which this would be used is that a Servlet would forward a request to the JSP, and the JSP would create a PDF using JSP tags based upon the information in the request.  Thanks in advance for any help provided.

Daniel

Reply via email to