This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes create pdf command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "create pdf command line" Watch "create pdf command line" New topic
Author

create pdf command line

Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Hello, I have been tasked with creating a PDF command line by pulling a string from a third party machine and creating a pdf that can be archived onto a new server.

Currently, i have an web app that pulls the string from the third party machine and renders the pdf in the browser.

I have taken parts of that code and tried to create a pdf. The pdf gets created but then I am unable to open it because it is "not a supported type or it is damaged".

Here is the code I am using to create the pdf:
*baos is the inputstream I get from the third party machine converted to a ByteArrayOutputStream.


Document document = new Document();
FileOutputStream myFile = new FileOutputStream(pathDate + accountNbr + ".pdf");
baos.writeTo(myFile);
PdfWriter writtenPdf = PdfWriter.getInstance(document, myFile);
document.open();
System.out.println("good pdf" + baos.size());
myFile.flush();

if (myFile != null) {
myFile.close();
}


I thought I had it working but do not. I was looking for some guidance for what i may be doing wrong?

Thanks.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Based on your previous posts, I have a feeling you are leaving out vital details.
What is a "PDF command line"? Do you mean local PDF file?
What are Document and PdfWriter? You use them, but I don't understand why. For example, you invoke document.open BEFORE flushing and closing myFile. I don't know why you'd want to open a file for reading before you have finished writing to it.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Well, you have me figured out...I think I may have a problem starting in the middle rather than at the beginning .

My previous posts were for taking this string from the third party machine and writing it as a PDF to the browser. This has been working for a while now.

I am going to create a shell script that will query the database and return a cursor of records, from which I will send the appropriate parameters to the java app(problem in question) which will send a request to the third party machine and return a string (code not shown). I then need to take the string and write it as a PDF file that can be archived to a new server.

My problem is trying to change the code to write to a file as a standalone app rather than (previous posts) writing to the browser inline in a web app.

PDF writer is a class I found in itext.jar that I thought could help me create the pdf. The code is in that order based on examples I have found.

Does that explain everything? Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't like the way you refer to this document as a "string". A PDF document isn't a "string" in the Java sense because it's a binary document, not a text document. So if at any point you are converting the bytes you get from the other server into a Java String, then that's where you are damaging the document.

As for your problem in general, I would just read the bytes from the server and write them to a file. That's all you need to do. Then that file will be a PDF document that can be opened by Acrobat.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Thanks for the reply. I am getting the data from the third party server as a DataInputStream which I convert to BufferedInputStream. I then read through this BufferedInputStream and write it to a ByteArrayOutputStream. There are some problems with how the DataInputStream is returned to me which i have not control over. Therefore, I read through it to remove some bytes which come before the %PDF. If the %PDF is not the first thing in the PDF then adobe does not know what to do with it.

So...up to this point I know the code works because i am able to render the pdf in the browser.

It is at this point I would like to change the code from rendering in the browser to creating a pdf file.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Also, my new code works but not consistently. The pdf opens successfully but not every time. This may be a timing issue or a problem with the third party machine. I have no support on the third party machine so I have to manipulate it from my side.

If I leave my machine for a while and come back and run it. The pdf generates successfully. If I then try to run it again, i cannot open the pdf generated.

I am just not sure how to debug it from here? I am wondering if anyone had any ideas.
Katie Doody
Ranch Hand

Joined: Nov 03, 2006
Posts: 90
Thanks Paul, I simplified it and just wrote the BufferedInputStream to a file and it works beautifully. I just love to complicate things. Thanks for your help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yeah. Like Jim Yingst's signature says:

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: create pdf command line
 
Similar Threads
want to write file at specified location
iText pdf image conversion
getting error in programatically created pdf
itext paragraph spacing
Reading a file