Discussion

Conversion Of HTML in to PDF

Hi All,

I am following the below way to convert HTML document to PDF document

1.Create One page

2.Call HtmlToPdf and in that i gave Mark up,HTML strame&PDFDocument(a java object property as O/P parameter)

3.Call Code-Pega-PDF.View(a java object property as I/P parameter)

but i got the error like "java.lang.String cannot be cast to [B "

Please anybody help me to resolve this issue.

Thanks&Regards,

Shilpa K.

**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.

Comments

Keep up to date on this post and subscribe to comments

September 20, 2013 - 8:24am

Hi Shilpak,

I understood that you are passing some value to PDFDocument O/P parameter. If i am correct, you should not pass any value to that O/P parameter. Give that PDFDocument Param value as input param to Code-Pega-PDF.View activity. Hope this will help.

 

Thanks

Naveen

September 24, 2013 - 9:11pm

were you able to get this to work? i got same issue

September 25, 2013 - 2:09am

Yes..it worked for me..try this

 

Please let me know if you have any queries

Thanks,

Naveen

September 25, 2013 - 10:49am

yes it worked when i run the activity from the activity window. however I have a button that when clicked runs this same activity and when clicked all the steps run normally but nothing shows there is no option to download or open the file. Is there anything that i am doing wrong at this point

thank you so much.

ps.

you must specify the Param.PDFDocument as a Java Object on the activity. then the step before you Call Code-Pega-PDF.View you must set that param equal to EformPageName.pyEForm.

September 25, 2013 - 11:47am

update, i got it to work finally.

initially i was just having the button run the activity and that was not working.

the way i got it to work is on the click event of the button i do a refresh section and within that i run the activity and now it works.

thank you so much for your help.

jay h

September 25, 2013 - 2:03pm

Hi Jaygasi,

 

I didn'y get your point then the step before you Call Code-Pega-PDF.View you must set that param equal to EformPageName.pyEForm.  Can you please explain me which param is to be set to EformPageName.pyEform

 

Thanks,

Naveen

September 25, 2013 - 4:17pm

i meant that the param should be set with the property that holds the Java Object binary PDF file, which resides in a ClipboardPage. but what i was doing wrong originally was simply setting PDFDocument to be equal to the clipboard property, unlike your picture. instead I need to set up the parameter to be equal to that property and then insert that param into PDFDocument. But this is if you plan on applying current page params for the view activity.

Also the PDFName needs to include the extension, so in your picture it would be "name.pdf" otherwise, at least for me, the system doesnt know it is a pdf file.

Hope this helps others.

 

September 30, 2013 - 8:25am

hi Jaygasi,

  now i can download that PDF file but when i tried to open that file it gives error like "Adobe reader couldn't open "PDFdoc.PDF"because it is either not a supported file type or the file has been damaged (for example,it was sent as an email attachment and was not correctly decoded.)".pls share  me the Approach u followed for this

thanks.

September 30, 2013 - 10:17am

Hi Harish,

I think this problem is because of the parameter Param.PDFDocument which you are passing as input to Code-Pega-PDF.View activity. Before you pass it to View activity you should actually generate pdf document using one Java method. That java step should actually contain the following code

PDFUtils pdfUtil=tools.getPDFUtils();
byte[] byteArray = pdfUtil.generatePDF(HTMLStream,tools.getParameterPage());
tools.putParamValue("PDFDocument",byteArray);

the HTMLStream parameter that i am using in the second line should have the html that you are passing in the prperty-set method.

Try this
Hope this helps

please let me know in case of any clarifications.

Thanks,
Naveen

September 30, 2013 - 10:17am

Hi Shilpak,

I think this problem is because of the parameter Param.PDFDocument which you are passing as input to Code-Pega-PDF.View activity. Before you pass it to View activity you should actually generate pdf document using one Java method. That java step should actually contain the following code

PDFUtils pdfUtil=tools.getPDFUtils();
byte[] byteArray = pdfUtil.generatePDF(HTMLStream,tools.getParameterPage());
tools.putParamValue("PDFDocument",byteArray);

the HTMLStream parameter that i am using in the second line should have the html that you are passing in the prperty-set method.

Try this
Hope this helps

please let me know in case of any clarifications.

Thanks,
Naveen

September 30, 2013 - 10:45am

hi shilpak7
how are you getting the pdf that you want to display is from the eForm or is it uploaded into pega as a binary file?

either way the param.PDFDocument needs to have the pdf in it. you can get it there by either utilizing:

@baseclass.GenerateEForm

or

Code-PegaeForm.GetEformFromRuleFileBinary

or do what NaveenCharyV suggested above.

thanks

jay h.

October 2, 2013 - 4:40pm

does any one know what influences pega to chose the editable fields in the pdf. i uploaded one pdf today and it brought back box 1, tex 2 etc all the way up to 59. instead of recognizing fields like Name, Address etc.

Also within that same pdf, some checkboxes are Yes/No and some are On/Off what sets this as well? if there are any ways of modifying this.

thank you

Jay H

December 4, 2013 - 12:35am

Hi Naveen / Jaygasi,

                           I'm stuck with PDF generation. Please look into below steps I have implemented and provide your comments.

                     Step Page                                                       Method

 

Step 1:                                                                            Property-Set-HTML

                   PropertyName : .GeneratePDF (Single Value Text property)

                   HTMLStream   :  ToGeneratePDF (This is a HTML rule which generates for JSP and has following code to call the Section that I want to display in PDF

 " <pega:include name = "PrepareUWProposal" type="Rule-HTML-Section"/>"

 

Step 2:                                                                            call Rule-Obj-Html.HTMLToPDF

                Markup      :    .GeneratePDF (Property that was set above)

                PDFDocument  :   Param.PDFDocument (Of type Java Object)

 

Step 3:                                                                              Call Code-Pega-PDF.View

               PDFName    :   "Name.PDF"

               PDFDocument   :   Param.PDFDocument

 

Getting the following error :"Failed to find instance @baseclass.ToGeneratePDF of type Rule-Obj-HTML"

 

Regards,

Phani

December 4, 2013 - 9:05am

Hi Phanisura,

the GeneratePDF i believe is an activity not a single value property. this activity will load the pdf in the step page. then within it you can find the property that hold the pdf as a binary object. I am not sure if this is what you did but reading your post didnt seem like it was.

March 24, 2014 - 8:05am

Hi,

Can you please tell me the steps in activity to convert HTML to PDF from the beginning???

 

thanks in advance,

Jayshree

March 25, 2014 - 4:16am

Hi Jayshree,

          Follow below steps and let me know if u r able to generate PDF...

 

Rules to be created:

 

  1. Property of type Java Object. (No need to specify class).

  2. HTML rule – Select Generate for as JSP, and place below code which calls the section that you want to display in PDF. Inclued below line(step 3) in the HTML rule.   

  3.  <pega:include name = "SectionName" type="Rule-HTML-Section"/>

Steps to Generate PDF:

 

  1. Call the predefined activity HTMLToPDF and provide following parameters.

 

StreamName : The HTML rule created.

PDFDocument : The Java Object property.

The above activity converts the HTML into bytes and these were stored in the Java Object property.

 

  1. Call the predefined activity Code-Pega-PDF.View and provide following parameters.

 

PDFName : Any name you which followed by .PDF (to save it as PDF by default)

 

PDFDocument : The Java Object property.(Which is created in step 1)

This activity displays the PDF, taking bytes from step1 as input.

 

 

May 6, 2018 - 8:01am
Response to PHANISURA

Hi Phanindra

what does you mean in java Object property? and how can I create it?

May 7, 2018 - 1:47am
Response to GhaidaA3

Hi,

Thank you for posting your query in the PSC. This looks like an inactive post and hence, we suggest you create a new post for your query. Click on the Write a Post button that’s available on the top right pane of this page. Once created, please reply back here with the URL of the new post.

You may also refer this discussion link as a reference in the new thread.

Vidyaranjan | Community Moderator | Pegasystems Inc.

March 25, 2014 - 6:25am

Hi,

i have created a java object property by name ABC, and an html rule (Form) referencing a section and in activity :

Activity steps:  1) call HTMLToPDF : Stream name: ABC,

                                                    PDFDocument :Form,

                       2) call Code-Pega-PDF.View :

                           PDFName : Doc

                           PDFDocument : ABC

 

when i run this activity, its giving the following error, Please advice,

 

Status fail
Message java.lang.String cannot be cast to [B
Operator ID Administrator@LIC.com
Requestor ID

HFFE1DA1DB32A240E25563710D51CBB64

March 25, 2014 - 6:31am

And click of this button, i am callin this activity, but still gettin error

March 26, 2014 - 1:02am

Hi Jayashree,

      Please check : In Step 1, U have mentioned ABC in stream name? and Form in PDFDocument??

Please do the following,

Step 1:

Stream Name -   "Form"   (HTML rule shud be in " ")

PDFDocument -   .ABC

 

Step2:

PDF Name -   "Doc.pdf"

PDFDocument -    .ABC

 

Update above steps and initially run the activity alone(dnt call it, on click of buttion) and check if u r able to get PDF.

March 26, 2014 - 12:25am

Its working,

Thanks a lot :)

July 10, 2014 - 1:28am

Hi,

I was able to generate the PDF but the formatting(ie. font size, font , bold) is missing in generated PDF, do you have any idea to resolve this issue?

Pega version: 7.1

 

Thanks.

March 9, 2015 - 9:14am

Hi  Hapelayo,

 

Could you please provide me the indetail steps for converting the HTML to PDF in the version 7.1?

March 9, 2015 - 10:38am

Hi,

 

    In Pega 7, U can use Smar Shape in flow to create pdf. The "Create PDF" smart shape does below:

Creates a pdf of the section which was referenced in the smart shape and make the pdf available in Attachments.

 

Smart Shape is nothing but Utility. Hope this helps.

May 21, 2015 - 3:00am

Step 1- create HTML rule which conatins  data or sections that you want to import to PDF

Step 2- Call HTMLToPDF activity and pass the parameters like Markup = HTML stream name, PDFName = the name you want for PDF, PDFPageSize = A3/A4, PDFPageOrientation= landscapr/portrait and so required parameters.

As output parameter we will get PDFDocument in the form of java object

Step 3- Call Code-Pega-PDF.View activity to view the PDF or Call Code-Pega-PDF.AttachToWork to attach it to WO but this also requires PZInsKey of the WO.

Frequent Issues-

Q1- Nothing is displayed when calling this activity on Button

A1- Call the activity on the button event of Refresh Section- Its asks for Data transform or Activity, give the activity name that we have written for pdf generation.

Q2- java.lang.String cannot be cast to [B

A2- PDFDocument which is an input parameter for View activity is going empty.

 

Regards,

Vinay

May 26, 2015 - 7:22pm

Is there any PDN article regarding HTML to PDF? I am totally lost after reading all the above posts. No offense to the posters I just didn't got what all the discussion about,  but I want to learn basics.

 

Thanks..!!

June 8, 2015 - 1:50pm

You can create an section, and include it in a  HTML fragment by making use of the pega:include jsp tag. 

This framgement name needs to be passed as the parameter value along with the expected string for the PDF name to the activity @baseclass.pyViewAsPDF. This will generate the PDF and it will download on to the browser. Also, make sure you pass the proper parameter page (the primary page to the pyViewAsPDF activity should have the neccessary properties present which are reference in the section). 

 

June 16, 2015 - 1:00pm

Adding to Imran, please refer to pyGeneratePdf activity present in Pega-Landing-Security-AccessManager-Privileges class for detailed implementation

October 7, 2015 - 11:24am

Hi,
I created a section which i wanted to get in PDF and included it in HTML Stream as pega:include name = "SectionName" type="Rule-HTML-Section"/
Next i created an activity and called "HTMLtoPDF" activity  with parameters for Stream Name=HTML Stream Name, PDFDocument=PdfPage.pyForm.
Then in the 2nd step i called "Code-Pega View" activity with parameters for PDFName & PDFDocument
But when i run this activity i am getting the popup to download the PDF without any data/bytes.
Can u please look into the steps where i have  been mislead

Thank You.

Pega
October 7, 2015 - 12:21pm

Does this section contains any parameters?

Would it be possible to create simple HTML rule and pass that as an input to HTMLtoPDF activity?

<html>

<head>

<title> Testing PDF </title>

</head>

<body>

<h1> Testing PDF body </h1>

</body>

</html>

Incase if this works then you can check the section configuration and pass it as an input to HTMLtoPDF activity.

October 8, 2015 - 2:18am

Can u be more specific, i mean the section i include doesnt have any parameters..

i am able to get pdf download from the activity accept there wasnt any data in it..."Error"

October 13, 2015 - 3:59am

I think there issue with your section I guess so. Better copy the below code given by Gangababu and paste it in HTML stream. then try to generate the pdf and check what is displaying in the pdf. if still it displays null then there is some issue in generating the JAVA object.

 

<html>

<head>

<title> Testing PDF </title>

</head>

<body>

<h1> Testing PDF body </h1>

</body>

</html>

 

Copy it and paste it in HTML stream and check what is being displayed.

 

Regards,

Vinay

April 4, 2016 - 6:57am

Hi

How to resolve this error?

java.lang.String cannot be cast to [B

Pega
November 22, 2016 - 2:38am

Refresh section is not a correct way of downloading a file. Achieve it by configuring Run script. Below is the script.

function downloadFile(activityName, className, applicationName, fileName, fileType){ 
var suURL = SafeURL_createFromURL(pega.u.d.url); 
suURL.put("pyActivity", activityName); 
suURL.put("pyClassName", className); 
suURL.put('ApplicationName', applicationName); 
suURL.put('FileName', fileName); 
suURL.put('FileType', fileType); 

var formEle = document.createElement('form'); 
formEle.id = "download_file"; 
formEle.method = "POST"; 
formEle.action = suURL.toURL(); 
if(pega.env.ua.webkit) 
formEle.target = ""; 
document.body.appendChild(formEle); 
formEle.submit(); 
document.body.removeChild(formEle); 

February 27, 2018 - 5:39am
Response to mohaa5

Hello,

 

With above script if document not found we are getting attached message which we don't want show at all

Any idea how we can handle step execution conditionally formEle.target = ""; ? 

Mod
February 27, 2018 - 6:45am
Response to GANESHA19

Hello!

Thank you for posting your query on PSC. This looks like an inactive post and hence, we suggest you create a new post for your query. Click on the Write a Post button that’s available on the top right pane of this page. Once created, please reply back here with the URL of the new post.

We have also sent you a private message opening up a communication channel in case you have any further questions.

Thanks,

Lochana | Community Moderator | Pegasystems Inc.

November 22, 2016 - 9:23am

Thanks Arif, the above solution is documented in a support article here : https://pdn.pega.com/support-articles/document-download-performed-only-link-clicked-first

February 5, 2017 - 12:08pm

Hey,

I found pyAttachAsPDF as a real good shortcut when you want to generate a PDF from HTML Stream and attach it to Work Object. This makes attaching really easy. It just needs HTML Stream name, PDF File name, and Work Object Key (pzInsKey).

Pega
April 5, 2017 - 7:06am

Reference: SE-32991

Configure run script and call the above function with parameters.