cancel
Showing results for 
Search instead for 
Did you mean: 

Error creating PDF from Web Dynpro using IText Jar

SandipAgarwalla
Active Contributor
0 Kudos

Hello all,

I am getting the following error while trying to open the PDF file which is generated by a web dynpro java application using IText Interfaces (Document, pdfWriter etc)

File does not begin with '%PDF-'.

Here is the basic essence of the code I am using inside the onAction event of the UI element


document = new Document(PageSize.A4, 50, 50, 50, 50);
outputStream = new ByteArrayOutputStream();
writer = PdfWriter.getInstance(document, outputStream);
---------- some more code for filling the document---
document.open();
document.add(table);
document.close();
byte[] b = new byte[100 * 1024];
b =  document.toString().getBytes("UTF-8");	
IWDCachedWebResource pdfRes = WDWebResource.getPublicCachedWebResource
(b, WDWebResourceType.PDF, WDScopeType.CLIENTSESSION_SCOPE, wdThis.wdGetAPI().getComponent().getDeployableObjectPart(),"FileName");
IWDWindow win = wdComponentAPI.getWindowManager().createNonModalExternalWindow(pdfRes.getUrl(WDFileDownloadBehaviour.
OPEN_INPLACE.ordinal()));   
////creating a new external window to display the PDF....
win.show();

If you click on the UI element, it does open a new window, however it show sthe Pdf error I have mentioned above and no pdf is displayed.

I know using ADS is one more option, I want to see the feasibility of IText Jar.

The server config - EP 7.0 SP18

My computer config - Windows Vista/ IE 7 /Adobe Reader 9.1 .3/ Adobe LiveCycle

Appreciate any help.

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Sandip,

Are you setting the content-type of the document to application/pdf ?

Regards

Eduardo

SandipAgarwalla
Active Contributor
0 Kudos

Hey Eduardo

Thanks for reply

How do I set the content type for document object? I didn't see any such method for the object.

Thanks

Sandip

SandipAgarwalla
Active Contributor
0 Kudos

Hello All,

Any other pointers to solve this issue?

Thanks

Sandip

Former Member
0 Kudos

Hi,

Try saving the file to your disk. After that, try to open the file. If the same error occurs, open the file using a text editor. Example, one I've opened here started with "%PDF-1.6" in the first line. Your file should be missing it.

Can you check this please?

Regards,

Daniel

SandipAgarwalla
Active Contributor
0 Kudos

Hey Daniel,

I am not able to save the file to local disk at all. On click of the web dynpro button action, it opens up a new window and then shows the error window "file doe not***".

Any other pointers what may be missing here?

Appreciate your replies.

Thanks

Former Member
0 Kudos

Hi,

Before converting your document to byte[] you have it in String format. Check with startsWith("%PDF").

You can also use setResourceName(name.extension) - Try to manually set to .pdf.

Regards,

Daniel

SandipAgarwalla
Active Contributor
0 Kudos

Hey Daniel,

Seems like the string "%PDF" is not getting added to the PDF. I added it manually thru the code, atleast the error is gone. Thanks

Now the new window opens a pop-up to ask whether to save or open the file. But when I open the file, it says

"the file is damaged and could not be repaired"

I tried to open the pdf in EditPlus, and interestingly it only has the following string

%PDF-1.4

com.lowagie.text.Document@39d4465a

I am using the following code to add the string "%PDF" to the doucment and then open it in a new window

	
doc.add(table);					
doc.close();
byte[] pdfContent = new byte[100 * 1024];			
String strDoc = doc.toString();
if(!strDoc.startsWith("%PDF")){
strDoc = "%PDF-1.4\n".concat(strDoc);	
}
pdfContent =  strDoc.getBytes("UTF-8");
IWDResource pdf = WDResourceFactory.createCachedResource(pdfContent,"ListOsUsers.pdf",WDWebResourceType.PDF);
IWDWindow win = wdComponentAPI.getWindowManager().createNonModalExternalWindow(pdf.getUrl(WDFileDownloadBehaviour.
ALLOW_SAVE.ordinal()));
win.show();


is anything wrong here???

Thanks

Sandip

Former Member
0 Kudos

Hi,

I think you misunderstand me. I wouldn't suggest you to mannually add the "PDF" string, and also, you are getting the "Object String Name", not it's contents into the String you are checking against.

I will put some code here, try to adapt your code..


doc.add(table);					
doc.close();
byte[] pdfContent = strDoc.getBytes("UTF-8");
String pdfString = new String(pdfContent);
// Your Test Logic..
IWDResource pdf = WDResourceFactory.createCachedResource(pdfContent,"ListOsUsers.pdf",WDWebResourceType.PDF);
IWDWindow win = wdComponentAPI.getWindowManager().createNonModalExternalWindow(pdf.getUrl(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()));
win.show();

Note, you could also add the %PDF into the newly created PDF String, and convert it again to a byte array, using String.getBytes(String charsetName). I do not recommend you do that, and also, I think your PDF at this moment contain the String, and it's all OK.

I think the case is, when you are creating the resouce, it's getting modified by something else, and it's removing the header PDF information, maybe because before you did not have the "file extension" set in the filename.

First, use the logic I've posted to ensure the %PDF is there before creating the "IWDResource", and if is there, create the resource like you are doing it now (with the file extension) and test to see if it works. And please, if it works, delete the new generated String, don't leave it there!

Revert back with your findings.

Rgds,

Daniel

Former Member
0 Kudos

Can you tell me how did you get strDoc from doc in the 3rd line?

Former Member
0 Kudos

Use the following code and it works like a charm. Thanks for all your help.

		Document document = new Document(PageSize.A4, 50, 50, 50, 50);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		PdfWriter.getInstance(document, out);
		
		document.open();
		
		Paragraph para = new Paragraph("Test");
		para.setAlignment(Element.ALIGN_CENTER);
		document.add(para);
		document.close();
		
		byte[] b =  out.toByteArray();	
		
		IWDCachedWebResource pdfRes = WDWebResource.getPublicCachedWebResource(b, WDWebResourceType.PDF, WDScopeType.CLIENTSESSION_SCOPE, wdThis.wdGetAPI().getComponent().getDeployableObjectPart(),"Test.PDF");
		IWDWindow win = wdComponentAPI.getWindowManager().createNonModalExternalWindow(pdfRes.getUrl(WDFileDownloadBehaviour.OPEN_INPLACE.ordinal()), "");   
		
		win.show();

SandipAgarwalla
Active Contributor
0 Kudos

Hey Faraz,

That really worked like charm ! Thanks

I was using gettting the bytes from the document object rather then using the output stream. That was the problem. Now it works great.


// byte[] b = document.to
byte[] pdfContent =  outputStream.toByteArray();		
IWDCachedWebResource pdfRes = WDWebResource.getPublicCachedWebResource
(pdfContent, WDWebResourceType.PDF, WDScopeType.CLIENTSESSION_SCOPE, wdThis.wdGetAPI().getComponent().
getDeployableObjectPart(),"ListOfUsers");

I am not adding pdf at the file name (ListofUsers), else it generates file with the following name - ListOfUsers.PDF48448.pdf

Not sure why is this happening.

Do you see any such issues ?

Hey Daniel, Thanks a lot for your help.

Thanks

Answers (1)

Answers (1)

Former Member
0 Kudos

I am also having the same problem. Getting the error "File does not begin with "%PDF-". Anybody found a solution to this?