cancel
Showing results for 
Search instead for 
Did you mean: 

Open File PDF sap mobile SDK

former_member731594
Discoverer
0 Kudos
874

Hello

I try to open a base64 file as a pdf file, then I do this:

export default function prb(clientAPI) {

var pdf="JVBERi0xLj.........................U4MzYKJSVFT0YK";

var generatePdf = 'data:application/pdf;base64,' + pdf;

return clientAPI.nativescript.utilsModule.openUrl(generatePdf);

}

in navigator web browser can open and read it

but on the mobile device android& iphone he could not open it

View Entire Topic
bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

I would probably do it by having the button call a rule. In the rule, convert the base64 into binary and write the PDF out to the device. Once the file is written to the device, call OpenDocument from the rule and set the Path property to the path to the file.

In your steps you are missing the actual step to write the data to the file path you created.

Here is an example of taking a base64 string and writing to the device file system. The rule was originally written to grab the image from an Offline media entity and return the base64 string to directly render using the Sample Service.

Since I did not have the data already in base64 format, I am converting to base64 (needed for the original purpose) and for this example then converting back from base64 to demonstrate the conversion and how to write to a file.

Since you already have the base64 string you can just take the appropriate section for your use case.

/**
 * Describe this function...
 * @param {IClientAPI} context
 */
 export default function GetProductImage(context) {
    const platform = context.nativescript.platformModule;  
    const fs = context.nativescript.fileSystemModule;
    let imageData = null;
    let b64Data = null;
    let imageObject = context.binding;
    let pageProxy = context.getPageProxy()

    // This is helper function to convert data buffer to native byte array
    let _bufferToNativeArray = function (byteArray) {
        let array;

        if (platform.isAndroid) {
            array = Array.create("byte", byteArray.length);
            for (let i = 0; i < byteArray.length; i++) {
                array[i] = new java.lang.Byte(byteArray[i]);
            }
        } else {
            array = NSData.dataWithBytesLength(byteArray.bytes, byteArray.length);
        }
        return array;
    }

    return pageProxy.executeAction({
        "Name": "/MDKDemoApp/Actions/Product/DownloadProductImage.action",
        "Properties": {
            "Target": {
                "ReadLink": imageObject['@odata.readLink']
            }
        }
    }).then((result) => {
        // success case
        //console.log('Retrieved Image');
        if (platform.isAndroid) {
            imageData = `data:image/example;base64,${android.util.Base64.encodeToString(result.data,android.util.Base64.DEFAULT)}`;
            b64Data = `${android.util.Base64.encodeToString(result.data,android.util.Base64.DEFAULT)}`;
        } else if (platform.isIOS) {
            imageData = `data:image/example;base64,${result.data.base64Encoding()}`;
            b64Data = `${result.data.base64Encoding()}`;
        } else {  // Assume MDK Web
            let buffer = Buffer.from(result.data);
            imageData = `data:image/example;base64,${buffer.toString('base64')}`;
        }
        //Now prepare to save the data to a file
        let name = imageObject.PictureUrl.split('/');
        let filename = name[(name.length - 1)];
        var tempDir = fs.knownFolders.documents();
        var folder = "Products";
        if (!fs.Folder.exists(fs.path.join(tempDir.path, folder))) {
            fs.Folder.fromPath(fs.path.join(tempDir.path, folder));
        }
        var filePath = fs.path.join(tempDir.path, folder, filename);
        var productFile = fs.File.fromPath(filePath);
        console.log("Saving the file at: " + filePath);
        
        if (platform.isAndroid) {
            //Convert the data buffer to native data array
            let decodeB64 = android.util.Base64.decode(b64Data,android.util.Base64.DEFAULT);
            let nativeData = _bufferToNativeArray(decodeB64);
            //Writing the data to file
            productFile.writeSync(nativeData, err => {
                productFile.remove();
                alert("WRITE SYNC FAILED: " + err);
            });
        } else if (platform.isIOS) {
            context.base64StringToBinary(imageData).then((result) => {
                //Convert the data buffer to native data array 
                let nativeData = _bufferToNativeArray(result);            
                //Writing the data to file
                productFile.writeSync(nativeData, err => {
                    productFile.remove();
                    alert("WRITE SYNC FAILED: " + err);
                });
            });
        }
        return imageData;
    }, (error) => {
        // error case
        console.log('Error downloading image');
    });
}
0 Kudos

Hi bill.froelich. I have a code similar to this and I’m getting “Uncaught ReferenceError: Buffer is not defined“.

I’m building the app using BAS. Do I need to import the Library or do something else?

Thank you

bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

We discovered that the Buffer call was no longer there by default. We will be publishing an updated version of the sample app to correct it this coming week.

bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

The MDK_Demo_Application has been updated with an alternate call to convert to Base64 on the Web.