Re: Convert a pdf file to an objectinpustream
On Oct 2, 10:42 am, "Matt Humphrey" <ma...@iviz.com> wrote:
"C-man" <c00ldia...@gmail.com> wrote in message
news:71143162-7b66-4816-9433-6e1a0747d6a7@k37g2000hsf.googlegroups.com...
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.
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 d=
on't
know what jasper does, but you can read the raw PDF bytes directly from t=
he
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