Convert a pdf file to an objectinpustream

C

C-man

Hi all,

I have a pdf file on disk and I would like to print it using java. I
am using jasper print manager.
Here is the piece of code where it fails.

Object obj = null;

InputStream fis = null;
ObjectInputStream ois = null;
File file = new File( pdf.getpath() );
try {
fis = new FileInputStream( file);
ois = new ObjectInputStream( fis );
obj = ois.readObject();
} catch ( IOException e ) {
throw new DBException( "Error loading object from file : "
+ file, e );
}

pdf.getpath() will return something like d:\documents\report
\20080930.pdf.
readObject is failing and giving me a
"java.io.StreamCorruptedException: invalid stream header" error.

I am using jdk 1.4.

Anyway to fix that?

Thanks.
 
C

C-man

ObjectInputStream is specifically for deserializing Java objects--it won't
work for PDF.  What kind of object would you expect to get from it? I don't
know what jasper does, but you can read the raw PDF bytes directly from the
file input stream.

Matt Humphreyhttp://www.iviz.com/



I am using the printReport method from jasperPrintmeanager to print
the file.

public static boolean printReport(
String sourceFileName,
boolean withPrintDialog
) throws JRException
{
JasperPrint jasperPrint =
(JasperPrint)JRLoader.loadObject(sourceFileName);

return printReport(jasperPrint, withPrintDialog);
}


And printReport calls loadObject from JRLoader.

public static Object loadObject(File file) throws JRException
{
if (!file.exists() || !file.isFile())
{
throw new JRException( new
FileNotFoundException(String.valueOf(file)) );
}

Object obj = null;

FileInputStream fis = null;
ObjectInputStream ois = null;

try
{
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
obj = ois.readObject();
}
catch (IOException e)
{
throw new JRException("Error loading object from file : " + file,
e);
}
catch (ClassNotFoundException e)
{
throw new JRException("Class not found when loading object from
file : " + file, e);
}
finally
{
if (ois != null)
{
try
{
ois.close();
}
catch(IOException e)
{
}
}

if (fis != null)
{
try
{
fis.close();
}
catch(IOException e)
{
}
}
}

return obj;
}

So basically, I need to convert the pdf file to jasper print. Any
idea?

ref:
http://www.koders.com/java/fidDFAEB8B8A668E411E6C5FB4D54472CABDD2A9B23.aspx?s=JasperPrintManager#L86
http://www.koders.com/java/fid0D0A33093791495A95D8F75069016E7ED72824C2.aspx?s=jrloader#L89
 
A

Andreas Leitgeb

C-man said:
I am using the printReport method from jasperPrintmeanager to print
the file.

Judging from the code-samples, I'd think that printReport is not
meant to read pdf-files, but only so-called report files.

The existence of methods "printReportToPdfFile" seems affirmative
that Reports and PdfFiles are not the same thing.

If printing pdf files was your primary goal, then jasper perhaps isn't
the solution for that.
 
C

C-man

Judging from the code-samples, I'd think that printReport is not
meant to read pdf-files, but only so-called report files.


The existence of methods  "printReportToPdfFile" seems affirmative
that Reports and PdfFiles are not the same thing.

If printing pdf files was your primary goal, then jasper perhaps isn't
the solution for that.

Any idea on how I can print already generated pdf files with Java
without having to open them in a pdf reader.

The scenario is as follows:
The user clicks on "generate reports" and pdf reports are created and
stored on the hard disk.
The user then clicks on "print" and the generated reports should be
sent to the printer.

Thanks.
 
C

C-man

I haven't tried any of these, but here's a link of free Java PDF
libraries (found with Google btw).

http://java-source.net/open-source/pdf-libraries

While many of these generate PDFs only, there's at least one that does
printing, and at least one that will render to a JPanel or Image, which
might help also.

Good luck, and let us know what you find out please.

After some more searching around, I came across this thread:
http://forums.sun.com/thread.jspa?threadID=523898&start=15&tstart=0

And there is this link
http://www.crionics.com/tutorials/123-printer-jdk14.html.

I have not tried that yet but will let you know the result once I do.

Thank you all for your help.
 
C

C-man

After some more searching around, I came across this thread:http://forums..sun.com/thread.jspa?threadID=523898&start=15&tstart=0

And there is this linkhttp://www.crionics.com/tutorials/123-printer-jdk14..html.

I have not tried that yet but will let you know the result once I do.

Thank you all for your help.

In fact java has a print class class, javax.print.

I just need to find the flavors supported bu the printed and set my
flavor to be a compatible one.

More information in that thread.
http://forums.sun.com/thread.jspa?messageID=1178698
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top