Skip to Content
Technical Articles
Author's profile photo Jacky Liu

Use SAP Cloud SDK to render PDF document with BTP SAP Forms Service by Adobe rest API

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 external\CF_ADSRestAPI.json –outputDir  external\CF_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 app\resources\index.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_archives\sdkforads_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

 

The end .

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jacky Liu
      Jacky Liu
      Blog Post Author

      Hi,  Dear Reader,

       

      If you run into error like 'make is not a recognized command' with command mbt build  . You can install make with referrence to the following linkage .

      Make - Cloud MTA Build Tool (sap.github.io)

       

      Best regards!

       

      Jacky Liu

      Author's profile photo Cedric Heisel
      Cedric Heisel

      Hi Jacky,

      SAP Cloud SDK is an underrated framework and utility portfolio. I am using it on a daily basis and just can recommend it together with typescript and SAP Cloud Solutions 🙂

      So thanks for highlighting and sharing your approach with us!

       

      Regards,

      Cedric

      Author's profile photo Jacky Liu
      Jacky Liu
      Blog Post Author

      Hi, Cedric,

       

      Thanks for your recommendatiion, maybe in next blog, I can try ui5 with typescript to render the pdf in  browser .

      Best regards!

      Jacky Liu

       

      Author's profile photo Jacky Liu
      Jacky Liu
      Blog Post Author

      Hi,  Pandit,

       

      Have you assign yourself role collection sdkforads_admin and sdkforads_user  ?

       

      Best regards!

       

      Jacky Liu

      Author's profile photo Vikrant Pandit
      Vikrant Pandit

      Hi Jacky Liu,

      I was actually missing something in the steps between, I went through each and every step from scratch again and it worked, thus deleted my previous comment.
      I've been following your other blog as well.

      As you have mentioned above you'll be coming up with a blog to consume rendered pdf in a UI5 application, are you planning to post a blog for the same? There are no blogs available as such in detail for this, related to cloud foundry.