cancel
Showing results for 
Search instead for 
Did you mean: 

Error opening PDF using webdynpro

Former Member
0 Kudos

Hello All,

I created a webdynpro application that opens a pdf document using the code described in the following link.

I am getting the following error:

"There was an error opening this document. The file is damaged and could not be repaired"

But I am able to open the same file using another ASP application.

Am I missing anything?

Thanks in advance.

Accepted Solutions (0)

Answers (7)

Answers (7)

Former Member
0 Kudos

Nikhil,

I tried increasing the byte size but I am still getting the same error.

May be the code where I am reading the bytes from blob :

Blob pdfBlob = resultSet.getBlob(COLUMNNAME)

byte[] pdf = new byte[pdfBlob.length];

pdf = pdfBlob.getBytes(1,(int)pdfblob.length());

is not working. Should I read the bytes on by one using a while loop? If so how? (I am reading this from the database)

Naga - Thanks for the response. I am reading this pdf data from a database ( stored as a blob).

Thanks.

Former Member
0 Kudos

Nikhil,

I tried and I am getting the following error while opening the PDF document:

"The file is damaged and could not be repaired"

Thanks.

nikhil_bose
Active Contributor
0 Kudos

seems problem with byte[] size only. Can you try small sized file or still increase array size?

ex.


new byte[6500000]

lets try and reply status

nikhil

Former Member
0 Kudos

Hi,

Use this


FileInputStream in = new FileInputStream(new File(resourcePath));
ByteArrayOutputStream out = new ByteArrayOutputStream();
int i;
byte[] bytes = new byte[in.available()];
while ((i= in.read( bytes)) != -1){
 out.write(bytes, 0, i);
}
in.close();

Regards,

Naga

nikhil_bose
Active Contributor
0 Kudos

did you try what i suggested? I think in dynpro whole binary is not cached.

Please try changing the byte[] size to blob size and reply me what error you are getting.

nikhil

Former Member
0 Kudos

Nikhil and Vinod,

Thanks for your help. There is nothing wrong with the PDF, I can open the same file using an ASP application without any problems. The only difference I observed between the ASP application and the webdynpro application is that, in the ASP application, the pdf file is opening directly in the browser while in the webdynpro application it is opening in Adobe. Would this make any difference?

Thanks again for your help.

Former Member
0 Kudos

Nikhil and Vinod,

Thanks for your response. I used this code but it is not working. I am still getting the same error.

In my application, I am not reading the pdf from a file. Instead, it is stored in the database as a blob. I am reading the data using the following code:

Blob pdfBlob = resultSet.getBlob(COLUMNNAME)

byte[] pdf = new byte[1024]

pdf = pdfBlob.getBytes(1,(int)blob.length());

I am storing this value in a context and then displaying it as a pdf. I am using the following code to get bytes

byte[] pdfBytes = wdContext.currentElement().getBLOB(); (BLOB - name of the context attribute where bytes are stored)

and using the following code to open the pdf document (as suggested)

IWDResource pdfResource = WDResourceFactory.createResource(pdfBytes,"test.pdf",WDWebResourceType.PDF);

* *

* wdComponentAPI.getWindowManager().createNonModalExternalWindow(pdfResource.getUrl(0),"test.pdf").show();*

Am I missing anything here?

How can I force the pdf document to open in a browser window instead of Adobe?

Thanks in advance.

Former Member
0 Kudos

Please verify that the PDF file is not corrupted while storing to Database. The Error that is displaying is due to teh missing part in PDF file. I don't see any error in code.

Please go through the first sentence and check the part.

Regards

Vinod V

nikhil_bose
Active Contributor
0 Kudos

try this code


Blob pdfBlob = resultSet.getBlob(COLUMNNAME)

byte[] pdf = new byte[pdfBlob.length];
pdf = pdfBlob.getBytes(1,(int)blob.length());

once you get byte[] you can pretty well go for constructing a cached web resource from it thus opening in browser.

refer the code below


resource = WDResourceFactory.createCachedResource(pdf,"Name",WDWebResourceType.PDF);
IWDWindow window = wdComponentAPI.getWindowManager().createNonModalExternalWindow(resource.getUrl(0),
"Name");
window.show();

nikhil

Former Member
0 Kudos

Shilaja,

This will open the PDF file in server in to your browser application.

Specify the location of the file in Path.

try{

InputStream stream = new FileInputStream(Path);

IWDResource resource = resource = WDResourceFactory.createResource( stream, "test.pdf", WDWebResourceType.pdf, true);

stream.close();

wdComponentAPI.getWindowManager().createNonModalExternalWindow( resource.getUrl( 0), "test.pdf").show();

}catch( Exception e){ e.printStackTrace();}

Modify the code as follows to read file


 FileInputStream in = new FileInputStream(new File(resourcePath));
 ByteArrayOutputStream out = new ByteArrayOutputStream();
   int len;
byte[] bytes = new byte[1024];
	while ((len = in.read( bytes)) != -1)
	{
	   out.write(bytes, 0, len);
	}
    in.close();
   

regards

Vinod

Edited by: Vinod V on Jul 5, 2008 10:50 AM

nikhil_bose
Active Contributor
0 Kudos

try giving more length to byte array like


byte[] part = new byte[30 * 1024];

nikhil

Edited by: Nikhil ßos on Jul 5, 2008 11:09 AM