File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes jsp to pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "jsp to pdf" Watch "jsp to pdf" New topic
Author

jsp to pdf

Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
hey i am designing some web pages in jsp

now when i input certain information reports are being created from database

now i want those reports to be in pdf format

what should i do

please guide me
thank you
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Probably setting the response type to application/pdf should solve the problem.


Swastik
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

iText is basic api to generate pdf from java.
Now, if you are creating a report from db, I hope you must be using servlet/action etc in between where you can generate this pdf.
Also, which reporting api/tool are you using?

If none, try iReports its free and very easy.


SCJP 91%, SCWCD 97%
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 54


Hope this helps, If you need more help ,please let us know.


Regards,
Deepakkumar Devarajan
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
thanks guys for your help

@deepak

where should i include this code & i dnt already have any pdf file i have to show my jsp page report in pdf format

please help me

thank you
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
hey when i give this code then i got an error opening the document

file doesnot begin with %pdf
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Aditi,

As suggested by Shashank, you have to first convert the data into pdf format, then only setting the response type will work.
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 54

Aditi agarwal wrote:hey when i give this code then i got an error opening the document

file doesnot begin with %pdf


Can you please post what you have tried?
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 54

Swastik Dey wrote:Aditi,

As suggested by Shashank, you have to first convert the data into pdf format, then only setting the response type will work.


I tried exporting data as a PDF file from jsp, it worked perfectly for me(I did not use iText, heard about it. Hope while using iText, conevrsion is must but when you set ContentType as PDF in JSP, it automatically take care of the conversion of data. Will learn iText in the coming days.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
Sory bear for using java class code in jsp

hey thanks both of you for replying

i am giving same as you have written deepak


@swastik i cannt understand how to do that

thank you

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

@Deepakkumar

Your case is slightly different, you already have a pdf file and that is being rendered as pdf. But Aditi's case is different, she is first getting data from underlying database table. So as per my understanding that has to be first converted to a pdf format and then only that can be rendered as pdf.

Aditi

Your code as this line


So if you render this file to the user with the content type pdf, you should get the correct output. But I don't think that is the case. Probably what you have to do is get the data from table, convert it to pdf using the suggested libraries. Then render that pdf to the user with content type application/pdf.

Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 54

@Swastik Thanks for the explanation, I understood, why i did not get the error, I tried to export a html table and it did not work, :-). I am looking at iText examples.. As of now i assume that API is converting the data automatically as i do not see any code for conversion. Correct me if i am wrong..

Aditi

The link is for you, hope it will help.

java]http://www.coderanch.com/how-to/java/ItextExample
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
thanks for the reply @swastik & @deepak

but still it cannot include

com.itextpdf.text.*,com.itextpdf.text.pdf.*

files
what could be the problem
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 54

You have not add the Itext jar path to your class path.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
@deepak thanks for your reply

i have already added itext-1.3.1 .jar but still its giving error if you have any link for itext then please post it so that i can download it from new link

if any other problem is there then please let me know

thank you
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Try from here

http://sourceforge.net/projects/itext/files/iText/iText5.0.6/iText-5.0.6.jar/download
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
@swastik thanks for reply but that link is not working
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Its working for me even now. I just tested it. It opens and pop ups a window to save the jar file. If you wish I can send the file.
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 54

That link works, May be your browser blocks you downloading the file. If so,just click the message that gets displayed at the top of the page and select download file.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
@swastik then please send me that file bcause i need that if still it will make any error then i can discuss it here only

thank you
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
Sorry to disappoint you both guys but still same error is coming

that is

there was an error opening this documentfile doesnot begin with %PDF

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

So, the import issue is resolved. Could you please show your code?
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
sure this is my code




Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Comment/Remove the following two lines


And make sure in c:\ (c root) you have a folder called temp, because the code is create the file FirstPdf.pdf under temp folder which is under c:\.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
problem is solved to some extent but still hard luck

actually i need that link as soon as clicked it should open file in pdf format not jsp as it is doing now

& the firstPdf.pdf which is gettings aved in temp when clicked shows

"there was an error opening this document sharing violation"



thank you
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Do you mean that at first it displays a jsp/html with the tabular data, and when the button is clicked it displays the same data in pdf format? If that is the case, you probably need two codes. First one displays the html/jsp page with the button. When button is clicked send to request to server which again invokes a jsp/servlet the generate the pdf contents.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
yes you are right partially its now opening tabl ein jsp firstly & when i click my pdf file in temp folder in C then it gives th error

Its in the sense still no oopening pdf aqutomatically

thank you
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

As far as I know sharing violation is a os level issue, as you are trying to open the file manually. Just stop tomcat or whatever server you are using and see if it opens.
Aditi agarwal
Ranch Hand

Joined: Feb 23, 2011
Posts: 225
thanks swastik its now not showing violation error but showing file load error
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Aditi

Your code is not writing anything to the pdf file, that might be a probable reason. Just after opening the document, write something to the file



It would be also good, if you declare the document object before starting try block and close it in finally block.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp to pdf