In my blogs Configure the SAP BTP Cloud Foundry Environment Subaccount with SAP Forms Service by Adobe and Test with Postman, I have explained configuring ads and testing ads rest api with postman . I have received many queries about this blog . One question is how to use  SAP cloud sdk to call  ads rest api to render pdf document .

I assume you have installed  nodejs ,yeoman. If you have not installed yeoman, you can use the following  command to install it .

npm install -g yo

Also suggest you instll generator-saphanaacademy-mta with the folowing command :

npm install -g generator-saphanaacademy-mta

You have install cloud foundry cli and  mta tool . For cloud foundry cli,  you can refer the toturial .

For MTA tool, you can install it with :

npm install -g mbt

 

Base on all these prerequisites, today I want to blog the steps on how to use SAP Cloud SDK to render PDF document with BTP SAP Forms Service by Adobe rest API .

The following is the steps :

Step 1 :  Check  SAP Cloud SDK for  Forms Service by Adobe API in API hub  and download API specification .

Step 2 : generate  a mta project with yeoman .

Step 3: use visual code to change the code :

Step 4:  add a folder external under srv and move the downloaed ADS specification file  into folder external

Step 5: Generate a typed client by running the generator

create folder CF_ADSRestAPI under folder external .

rename API specification file to CF_ADSRestAPI.json

add the following line in srv/package.json

,”@sap-cloud-sdk/openapi”: “latest”

run the following command:

cd srv

npm install

openapi-generator -t –input externalCF_ADSRestAPI.json –outputDir  externalCF_ADSRestAPI

Step 6: add the following code to server.js

const {ADSRenderRequestApi} = require('./external/CF_ADSRestAPI/CF_ADSRestAPI/ads-render-request-api')
const { StoreFormsApi } =require('./external/CF_ADSRestAPI/CF_ADSRestAPI/store-forms-api')


app.get('/srv/ads/adsforms', function (req, res) {
    if (req.authInfo.checkScope('$XSAPPNAME.User')) {
        // res.setHeader()  
      StoreFormsApi.formGet('labelprint').execute({ destinationName:'ads-rest-api' }).then(data=>{res.status(200).json(data)}).catch(err=>{res.status(200).json(err)});         
    } else {
        res.status(403).send('Forbidden');
    }
});
app.get('/srv/ads/render',function(req,res){
    if (req.authInfo.checkScope('$XSAPPNAME.User')){

    const data = "<form1><LabelForm><DeliveryId>8000311</DeliveryId> <Position>0010</Position> <MaterialNo>M100001</MaterialNo><Quantity>123456.321</Quantity> <Package>P111111</Package><QRCode>01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789</QRCode></LabelForm></form1>"

    const bodyE = new Buffer(data).toString('base64')
    
    renderinput = {
        "embedFont": 0,
        "formLocale": "en_US",
        "formType": "print",
        "taggedPdf": 1,
        "xdpTemplate": "labelprint/PrintLabel",
        "xmlData": bodyE
          }
        const queryP =  { templateSource: 'storageName', TraceLevel: 1 }
        ADSRenderRequestApi.renderingPdfPost(renderinput,queryP).execute({destinationName:'ads-rest-api' }).then(pdf=>{res.status(200).json(pdf.fileContent)}).catch(err=>{res.status(500).send(err)})



    }else{
        res.status(403).send('Forbidden');

    }

})

Step 7: add the following code to appresourcesindex.html

    <a href=”/srv/ads/render”> ads render</a><br />
    <a href=”/srv/ads/adsforms”> ads forms</a><br />

Step 8: build mta file with the following command

cd ..

mbt build

Step 9: login into BTP subaccount with :

cf login

Step 10: deploy  generated mta file with

cf deploy mta_archivessdkforads_0.0.1.mtar

 

Step 11:  create destination for ads rest api in previouse blog

 

Step 12,  assign role collections to user

 

Step 12, test the deployed application

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x