Question

pzGenerateDocument returns corrupted file

I have a requirement to generate a Word Document that relies on a template. However when I use the OOTB pzGenerateDocument function to generate this document it returns a file that is corrupted and requires repair to view. The dynamic fields get filled out but I want to solve this corrupted file issue. Has anyone run into this and if so what was your solution?

Group Tags

Comments

Keep up to date on this post and subscribe to comments

Pega
October 28, 2018 - 1:10pm

Hi ,

Its always helpful to provide the PEGA version and also screen shot/doc of corrupted file and steps to reproduce .

So that It helps others to try out and suggest any info on this.

Please provide this Info, will get back to you on this.

 

January 16, 2019 - 4:54pm

I found a work around for this if you can stomach a pdf. It will return a file that is not corrupted but it will be in the pdf format.

  1. //call the api to generate word document
  2. try{
  3. url = alwo_worddocgeneration.GenerateWordDocument({Word Template}, tools,"{Primary Page Class}",null);
  4. docFilePath = tools.findPage("pxProcess").getString("pxServiceExportPath") + pega_rules_string.whatComesAfterLast(url, '/');
  5. java.io.DataInputStream tempSourceInputStreamGeneratedFile = new java.io.DataInputStream(new PRInputStream(docFilePath));
  6. //convert word doc to pdf
  7. org.docx4j.openpackaging.packages.WordprocessingMLPackage wordMLPackage = org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(tempSourceInputStreamGeneratedFile);
  8. //Converting above input stream to output stream
  9. java.io.ByteArrayOutputStream tempTargetOutputStreamGeneratedFile = new java.io.ByteArrayOutputStream();
  10. org.docx4j.Docx4J.toPDF(wordMLPackage, tempTargetOutputStreamGeneratedFile);
  11. //Converting above OutputStream to byte array and then encoding it
  12. byte[] byteArrayFinalFileSource = Base64Util.encode(tempTargetOutputStreamGeneratedFile.toByteArray());
  13. //Converting encoded byte array to string
  14. strFileData = new String(byteArrayFinalFileSource);
  15. }catch(Exception e)
  16. {
  17. oLog.error("Error in generating the document");
  18. }

The GenerateWordDocument function correlates to pzGenerateDocument its just usable in my current context. So you may have to save as it. As far as the rest of it goes all you have to do after this to make it into an attachment is make a new attachment page and set the source for the file to the local variable strFileData (you will have to have this in your activity). You will also need a word template to use.

January 24, 2019 - 5:08pm
Response to JJK03551

Hello

 

This looks like a post that can help me achieve my use case. With this, I am guessing the word document will be stored in /tempDir/StaticContent/global/ServiceExport which we are converting to .pdf

Can you please let me know if there is any way filename.pdf to ServiceExport directory.

 

Thanks in advance.

January 24, 2019 - 6:22pm
Response to AbhinayC

Well the file should be there in the export directory. From there you should be able to download and whatnot. The end bit of this is so that we can attach the file to a case.

January 25, 2019 - 11:53am
Response to JJK03551

Hello

Thanks for the response. The reason I ask is that we need to FTP the file in serviceExport for print purposes. Since, the word documents are corrupted, the print software is not able to read those .docx files. If I can convert them to pdf and save them in the serviceExport,  that would solve my problem. Can you please let me know if you can think of any methods to do that.

 

Thanks

Abhi

Pega
January 24, 2019 - 7:11pm

I have done a search on this. For a case, when user uses the pzGenerateDocument function to generate a word document based on an existing RULE-TEMPLATE-WORD word document there are a couple of issues occuring: eg. document format is corrupted; page tags inserted in the document are not populated correctly. 

The root cause of the issue is with the template user is using - a typo.  Syntax of pega reference tag is incorrect. Instead of pega:reference it is pega.reference.

Using {pega:reference pyWorkPage.pyID} is making it working fine.

So, please double check your template if it has the correct syntax. 

 

January 24, 2019 - 7:19pm
Response to SusanLiu

I have actually been in talks with Pega about this and the root cause is a problem in Docx4j. It returns a corrupted file even if it is a blank file.

Pega
January 24, 2019 - 7:41pm
Response to JJK03551

Have you raised a SR for it?

January 24, 2019 - 7:52pm
Response to SusanLiu

I did. I apologize for not mentioning it here. But it has since been resolved without a fix because it has to do with an external library. That's why I came back with a workaround that would work in some scenarios.

Pega
January 24, 2019 - 8:19pm
Response to JJK03551

Okay, thanks. 

January 24, 2019 - 9:17pm
Response to JJK03551

in which scenario it's failing ? did you able to provide the trace for any of those