How to get PDF binary content?
Off late, I have received few questions on how retrieve the print preview form (PDF content) of standard business object via coding . In this blog, I would like to share the code snippet to simulate the preview action in ABSL (code). The logic for standard object and custom objects are different as explained below.
Standard Objects:
I will take Quote object as an example. Before we start to write the code, we will require following information related to form template.
- Form Template Group Code: Group code identifies the collection of form templates with same semantics grouped under it..
- Object Type Code: It is the type code of the business object.
- Object Node Type Code – It is the type code of the node (usually root node).
We get the above information from the repository explorer in cloud application studio. (menu options: view->repository explorer) as shown below.
Form Template Group Code: We From repository explorer, under data types, search for OutputRequestFromTemplateGroupCode:
Object Type Code: Again from repository explorer, under business object, select the business object. Under description section you will find the Object Type Code. . Ex – CustomerQuote = 30
Object Node Type Code: Again from repository explorer, select the root node of the business object. Under description section you will find the Object Node Type Code. Ex- 3807
Now we are ready to write the code.
Code snippet:
import ABSL;
import BASIS.Global;
import AP.Common.GDT;
import DocumentServices.Global;
//To simulate Preview
var docOutReq : elementsof DocumentOutputRequest;
docOutReq.ReferenceObjectNodeID.content = this.UUID.content.ToString(); // UUID of root node.
docOutReq.ReferenceObjectNodeTypeCode.content = "3807"; //Object Node Type code of Quote root node
docOutReq.ReferenceObjectTypeCode.content = "30"; //Object type code of Quote BO
var docInst = DocumentOutputRequest.Create(docOutReq);
var FTG : OutputRequestFormTemplateGroupCode;
FTG.content = "C02"; //Form Template Group Code for Quote
docInst.RefreshDefaultOutputRequest(FTG);
var DocItem = docInst.Item;
var docItem = docInst.Item.GetFirst(); // Or get based on lang, template code.
docItem.Preview(); // Execute preview action
var binCont = docItem.ItemPreview.PreviewBinaryObject; // Get the binary object of PDF.
docInst.Delete();
Custom Objects:
It is simple compared to standard objects, we use the standard reuse library 🙂
OutputManagementUtilities.GetPDF(BusinessObjectNode, FormTemplateCode, FormTemplateLanguage);
- BusinessObjectNode = Node instance of the business object , ex “this”.
- FormTemplateCode = Code in the Form Template Header
- FormTemplateLanguage = Language of form template, ex ‘E’
Code snippet:
import ABSL;
import AP.Common.GDT;
import DocumentServices.Global;
import BASIS.Global;
//variables
var FormTemplateLanguage = "E";
var PDF : BinaryObject;
var FormTemplateCode : OutputRequestFormTemplateCode;
//Code is Form Template Header Code
//Could be different for Orgianal vs Patch solution.
if(Context.IsPatchSolution()){
FormTemplateCode.content = "YXXXXXXW_P00RY"; //patch solution
}else{
FormTemplateCode.content = "YXXXXXXW_P00RY"; //Orignal Solution
}
// Reuse Service Call
PDF = OutputManagementUtilities.GetPDF(this,FormTemplateCode,FormTemplateLanguage);
Dear Pramodh,
Hi Said,
thanks & regards
Pramodh
Dear Pramodh,
excuse me, i meant "Customer Visit" C03
Best Regards
Said
Hi Pramodh,
In my production system, CustomerInvoice, I use the DocumentOutputRequest.Item, and PreviewBinaryObject, then send it to customer using email. The script is:
foreach(var attach in ci.DocumentOutputRequest.Item)
{
if(attach.Status.OutputStatusCode != "3")
{
var attachment : pe:Attachment;
attachment.FileName=attach.DocumentName+".pdf";
attachment.Binary=attach.ItemPreview.PreviewBinaryObject.content;
attachments.Add(attachment);
}
}
The issue: the PDF attached to the customer email is blank, but also has the "Test Copy" watermark on the bottom-right of the page.
In test system hasn't the issue, do you know where the problem is?
Best Regards,
Benny Huang
Hi Benny,
Can you please check what is the active template for the group code in form maintenance UI?
If there are multiple templates available for the same group code, then you might have to loop through the DocumentOutputRequest.Item and pick the right template based the template code language.
Hope this helps.
Thanks & regards
Pramodh
Hi Pramodh,
Thanks a lot.
Now I using the script as you, the template is ok, now still has the "Test Copy" issue in my production. Could I do something in the script?
Best Regards,
Benny Huang
Hi Benny,
The Test Copy should removed from the template, and cannot be handled in the script.
Please check from UI Workcenter Application User Management-> Business Flexibility - Master Template Maintenance for any logs or header/footers.
Business Flexibility - Form Template Maintenance : For the form and check if it has test copy and replace with correct template.
thanks & regards
Pramodh
Hi Pramodh,
Thanks very much. I know the way using the adobe livecycle.
Best Regards,
Benny Huang
Hi Pramodh,
thank you for the tutorial!
I implemented the exact code for Customer Quote Standard BO and I bound it to a custom action
on Quote TI (Root Level, single) via the extensibility explorer. But unfortunately, it loads shortly but
nothing shows up. I also debugged the Code and I can see that only one doc item instance is found
for "C02" (Form Template Group Code).
Therefore, I didn´t loop via the doc items via language and country code.
Do you have any idea what the issue could be?
Best regards,
Rufat
Hi Rufat,
To be clear, this action will not preview the PDF on UI, it will only fetch you the PDF binary content, which you can for ex add it to attachments or e-mail.
In your case, did you get the binary content?
var binCont = docItem.ItemPreview.PreviewBinaryObject
was PreviewBinaryObject filled?
Thanks & regards
Pramodh
Dear Pramodh,
thank you for the clarification! Yes, I did get the binary content. But my initial goal was to build a custom print preview as we wanted to check if we can use our own print preview or use the sap standard action in our own custom action for quotes. We implemented several custom actions and didn´t want to have several buttons on the UI but use only one. So, as I understood, there is no quick possibility to implement a custom print preview for standard sap objects like CustomerQuote?
Best regards
Rufat
Hello Pramodh,
I am trying to do this with a production Order but when I get to the part of the
It says that the var is not Set.
Here you can see how I am doing it.
I hope you can help me.
Thanks
Fernando
Hi Pramodh,
I would like to get the binary content of standard "purchasing contracts".
All the data could be found as written in your blogpost.
It seems like there are no items in the generated docInst-object.
Can you provide some information about this?
Thank you in advance,
David