• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

create pdf command line

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Katie Doody
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28235
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28235
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic