on 09-04-2009 12:19 AM
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
Sandip,
Are you setting the content-type of the document to application/pdf ?
Regards
Eduardo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
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();
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
I am also having the same problem. Getting the error "File does not begin with "%PDF-". Anybody found a solution to this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.