Question
EvonSys Inc
US
Last activity: 16 Oct 2018 12:03 EDT
How to create a New Page in PDF File
There is a function to generate a PDF from a section rule.
I want to have new PDF page for some content. Can we do it within PEGA?
And how to set the Page size : A4 etc?
***Updated by moderator: Lochan to add Categories***
**Moderation Team has archived post**
This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.
-
Like (0)
-
Share this page Facebook Twitter LinkedIn Email Copying... Copied!
Accepted Solution
Pegasystems Inc.
GB
Hi Dhanusha,
This is a bit of a hack : but I found it to work for a simplistic test case - you can use a Java Step to 'merge' two (or more) PDFs; and (at least in my testing) each document will be appended starting on a new Page.
I (briefly - this is not 'bullet-proof' code by any means) tested this on a PRPC722 system and it worked for me.
The Java Code assumes you already have created two PDFs and have them stored on the Parameter Page (which is what HTMLTOPDF does in the parameter 'PDFDocument'.
Hi Dhanusha,
This is a bit of a hack : but I found it to work for a simplistic test case - you can use a Java Step to 'merge' two (or more) PDFs; and (at least in my testing) each document will be appended starting on a new Page.
I (briefly - this is not 'bullet-proof' code by any means) tested this on a PRPC722 system and it worked for me.
The Java Code assumes you already have created two PDFs and have them stored on the Parameter Page (which is what HTMLTOPDF does in the parameter 'PDFDocument'.
byte[] byteArray1=(byte[])pdfOne;
byte[] byteArray2=(byte[])pdfTwo;
org.apache.pdfbox.pdmodel.PDDocument mergedPDF=new org.apache.pdfbox.pdmodel.PDDocument();
java.io.InputStream bis1 = new java.io.ByteArrayInputStream( byteArray1 );
java.io.InputStream bis2 = new java.io.ByteArrayInputStream( byteArray2 );
try {
org.apache.pdfbox.pdmodel.PDDocument doc1=org.apache.pdfbox.pdmodel.PDDocument.load( bis1 );
org.apache.pdfbox.pdmodel.PDDocument doc2=org.apache.pdfbox.pdmodel.PDDocument.load( bis2 );
org.apache.pdfbox.multipdf.PDFMergerUtility merger=new org.apache.pdfbox.multipdf.PDFMergerUtility();
merger.appendDocument( mergedPDF, doc1 );
merger.appendDocument( mergedPDF, doc2 );
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
mergedPDF.save( bos );
mergedPDF.close();
tools.putParamValue("PDFDocument", bos.toByteArray() );
tools.putParamValue("PDFName", "merged.pdf" );
bos.close();
}
catch(Exception e) { oLog.error(e); }
Note: if you are using an earlier version of PRPC - you will probably need to refactor the package names for PDFBox.
To be sure what the package names are on your system; you can query your 'pr_engineclasses' with some SQL like this:
select * from pr_engineclasses
where lower(pzjar) like '%pdfbox%'
and lower(pzclass) like '%merger%';
I tested this with an Activity that does two HTMLTOPDF steps like this:
Note: I'm using the option 'Pass current parameter page' for each call to 'HTMLTOPDF'.
And transferring the resultant Param.PDDocument to a local variable after conversion using a Property-Set:
Property-Set local.pdfOne = Param.PDFDocument
My Pages and classes are:
MyContextPage (Where my Activity is located - actually - probably don't need this - since we are passing in the 'Markup' directly - rather than trying to rule-resolve a HTML input rule).
CodePage : Code-Pega-PDF (this is just to use the 'View' Activity as the last step).
My parameter tabs has two local variables:
pdfOne
pdfTwo
Both defined as type 'Object'.
I have attached an example output.
Pegasystems Inc.
IN
Hi Dhanusha,
How are you generating the PDF ?
Regards,
Evita.
EvonSys Inc
US
Using pxCreatePDF activity
Pegasystems
US
Take a look at activity HTMLToPDF and see if any of the parameters listed there will help. Of course, in order to make use of those, you will need to call that activity more directly then it is being called now, so that you can modify the parameters.
EvonSys Inc
US
Thank you for this. I could set page size to A4 using HTMLToPDF activity params.
Still I dont know how to get a new page for the specific contents.
Accepted Solution
Pegasystems Inc.
GB
Hi Dhanusha,
This is a bit of a hack : but I found it to work for a simplistic test case - you can use a Java Step to 'merge' two (or more) PDFs; and (at least in my testing) each document will be appended starting on a new Page.
I (briefly - this is not 'bullet-proof' code by any means) tested this on a PRPC722 system and it worked for me.
The Java Code assumes you already have created two PDFs and have them stored on the Parameter Page (which is what HTMLTOPDF does in the parameter 'PDFDocument'.
Hi Dhanusha,
This is a bit of a hack : but I found it to work for a simplistic test case - you can use a Java Step to 'merge' two (or more) PDFs; and (at least in my testing) each document will be appended starting on a new Page.
I (briefly - this is not 'bullet-proof' code by any means) tested this on a PRPC722 system and it worked for me.
The Java Code assumes you already have created two PDFs and have them stored on the Parameter Page (which is what HTMLTOPDF does in the parameter 'PDFDocument'.
byte[] byteArray1=(byte[])pdfOne;
byte[] byteArray2=(byte[])pdfTwo;
org.apache.pdfbox.pdmodel.PDDocument mergedPDF=new org.apache.pdfbox.pdmodel.PDDocument();
java.io.InputStream bis1 = new java.io.ByteArrayInputStream( byteArray1 );
java.io.InputStream bis2 = new java.io.ByteArrayInputStream( byteArray2 );
try {
org.apache.pdfbox.pdmodel.PDDocument doc1=org.apache.pdfbox.pdmodel.PDDocument.load( bis1 );
org.apache.pdfbox.pdmodel.PDDocument doc2=org.apache.pdfbox.pdmodel.PDDocument.load( bis2 );
org.apache.pdfbox.multipdf.PDFMergerUtility merger=new org.apache.pdfbox.multipdf.PDFMergerUtility();
merger.appendDocument( mergedPDF, doc1 );
merger.appendDocument( mergedPDF, doc2 );
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
mergedPDF.save( bos );
mergedPDF.close();
tools.putParamValue("PDFDocument", bos.toByteArray() );
tools.putParamValue("PDFName", "merged.pdf" );
bos.close();
}
catch(Exception e) { oLog.error(e); }
Note: if you are using an earlier version of PRPC - you will probably need to refactor the package names for PDFBox.
To be sure what the package names are on your system; you can query your 'pr_engineclasses' with some SQL like this:
select * from pr_engineclasses
where lower(pzjar) like '%pdfbox%'
and lower(pzclass) like '%merger%';
I tested this with an Activity that does two HTMLTOPDF steps like this:
Note: I'm using the option 'Pass current parameter page' for each call to 'HTMLTOPDF'.
And transferring the resultant Param.PDDocument to a local variable after conversion using a Property-Set:
Property-Set local.pdfOne = Param.PDFDocument
My Pages and classes are:
MyContextPage (Where my Activity is located - actually - probably don't need this - since we are passing in the 'Markup' directly - rather than trying to rule-resolve a HTML input rule).
CodePage : Code-Pega-PDF (this is just to use the 'View' Activity as the last step).
My parameter tabs has two local variables:
pdfOne
pdfTwo
Both defined as type 'Object'.
I have attached an example output.
EvonSys Inc
US
Thank you verymuch Prith. This will be the solution for me.
Thanks again..
Dhanusha.
Wipro
IN
We are using Pega 7.2.1 version. Here is the code I used in java step.
byte[] byteArray1=(byte[])pdfOne;
byte[] byteArray2=(byte[])pdfTwo;
com.pega.apache.pdfbox.pdmodel.PDDocument mergedPDF=new com.pega.apache.pdfbox.pdmodel.PDDocument();
java.io.InputStream bis1 = new java.io.ByteArrayInputStream( byteArray1 );
java.io.InputStream bis2 = new java.io.ByteArrayInputStream( byteArray2 );
try {
com.pega.apache.pdfbox.pdmodel.PDDocument doc1=com.pega.apache.pdfbox.pdmodel.PDDocument.load( bis1 );
com.pega.apache.pdfbox.pdmodel.PDDocument doc2=com.pega.apache.pdfbox.pdmodel.PDDocument.load( bis2 );
com.pega.apache.pdfbox.util.PDFMergerUtility merger=new com.pega.apache.pdfbox.util.PDFMergerUtility();
merger.appendDocument( mergedPDF, doc1 );
merger.appendDocument( mergedPDF, doc2 );
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
mergedPDF.save( bos );
mergedPDF.close();
tools.putParamValue("PDFDocument", bos.toByteArray() );
tools.putParamValue("PDFName", "merged.pdf" );
bos.close();
}
catch(Exception e) { oLog.error(e); }
I am getting the below exception.
** Java Exception: java.lang.ClassCastException: java.lang.String incompatible with [B
We are using Pega 7.2.1 version. Here is the code I used in java step.
byte[] byteArray1=(byte[])pdfOne;
byte[] byteArray2=(byte[])pdfTwo;
com.pega.apache.pdfbox.pdmodel.PDDocument mergedPDF=new com.pega.apache.pdfbox.pdmodel.PDDocument();
java.io.InputStream bis1 = new java.io.ByteArrayInputStream( byteArray1 );
java.io.InputStream bis2 = new java.io.ByteArrayInputStream( byteArray2 );
try {
com.pega.apache.pdfbox.pdmodel.PDDocument doc1=com.pega.apache.pdfbox.pdmodel.PDDocument.load( bis1 );
com.pega.apache.pdfbox.pdmodel.PDDocument doc2=com.pega.apache.pdfbox.pdmodel.PDDocument.load( bis2 );
com.pega.apache.pdfbox.util.PDFMergerUtility merger=new com.pega.apache.pdfbox.util.PDFMergerUtility();
merger.appendDocument( mergedPDF, doc1 );
merger.appendDocument( mergedPDF, doc2 );
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
mergedPDF.save( bos );
mergedPDF.close();
tools.putParamValue("PDFDocument", bos.toByteArray() );
tools.putParamValue("PDFName", "merged.pdf" );
bos.close();
}
catch(Exception e) { oLog.error(e); }
I am getting the below exception.
** Java Exception: java.lang.ClassCastException: java.lang.String incompatible with [B
Can you please provide the solution?
Thanks in advance.
Regards,
Raju.
Pegasystems Inc.
GB
What type have you defined your "PDFDocument" as ? (It looks like it might be String, and it probably should be 'Object' ?)
Wipro
IN
Both pdfOne & pdfTwo took as Object(in parameter tab) type.
Pegasystems Inc.
GB
I wonder if the API call you are using:
tools.putParamValue("PDFDocument", bos.toByteArray() );
isn't able to cast the byte array; can you instead try using the Property-Set method (Local->Param) that was used in the original example ? Do you get the same result ?
Property-Set local.pdfOne = Param.PDFDocument
EDIT: That can't be it - I just re-checked the original 722 version above - and that used the API call as well.
Let me fire up a PRPC721 and try with that- I'll report back here.
Pegasystems Inc.
GB
I got this working on PRPC721: here's the Activity I used and the Java Code below : attached sample output.
PARAMS TAB:
And the java Code for Step 7 is as follows:
I got this working on PRPC721: here's the Activity I used and the Java Code below : attached sample output.
PARAMS TAB:
And the java Code for Step 7 is as follows:
byte[] byteArray1=(byte[])pdfOne;
byte[] byteArray2=(byte[])pdfTwo;
com.pega.apache.pdfbox.pdmodel.PDDocument mergedPDF=new com.pega.apache.pdfbox.pdmodel.PDDocument();
java.io.InputStream bis1 = new java.io.ByteArrayInputStream( byteArray1 );
java.io.InputStream bis2 = new java.io.ByteArrayInputStream( byteArray2 );
try {
com.pega.apache.pdfbox.pdmodel.PDDocument doc1=com.pega.apache.pdfbox.pdmodel.PDDocument.load( bis1 );
com.pega.apache.pdfbox.pdmodel.PDDocument doc2=com.pega.apache.pdfbox.pdmodel.PDDocument.load( bis2 );
com.pega.apache.pdfbox.util.PDFMergerUtility merger=new com.pega.apache.pdfbox.util.PDFMergerUtility();
merger.appendDocument( mergedPDF, doc1 );
merger.appendDocument( mergedPDF, doc2 );
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
mergedPDF.save( bos );
mergedPDF.close();
tools.putParamValue("PDFDocument", bos.toByteArray() );
tools.putParamValue("PDFName", "merged.pdf" );
bos.close();
}
catch(Exception e) { oLog.error(e); }
Updated: 25 Apr 2018 14:06 EDT
Navy Federal Credit Union
US
Dear John,
I need this functionality for 7.1.8v, but I don't have all the library classes that you are using for merging. Attached the JARs screenshot from pr_engineclasses tables. Could you please help me to achieve this functionality.
Thanks
Sai