pdfbase64uint8arrayadobe-embed-api

Adobe PDF Embed API Save Content To Base64


Using Adobe PDF Embed API, you can register a callback:

this.adobeDCView = new window.AdobeDC.View(config);

this.adobeDCView.registerCallback(
    window.AdobeDC.View.Enum.CallbackType.SAVE_API, (metaData, content, options) => {

})

Content is according to the docs here: https://www.adobe.io/apis/documentcloud/dcsdk/docs.html?view=view

content: The ArrayBuffer of file content

When I debug this content using chrome inspector, it shows me that content is a Int8Array.

Normally when we upload a pdf file, the user selects a file and we read as dataURI and get base64 and push that to AWS. So I need to convert this PDF's data (Int8Array) to Base64, so I can also push it to AWS.

Everything I have found online uses UInt8Array to base64, and I don't understand how to go from Int8Array to UInt8Array. I would think you can just add 128 to the signed int to get a ratio between 0-256, but this doesn't seem to work.

I have tried using this:

let decoder = new TextDecoder('utf8');
let b64 = btoa(decoder.decode(content));
console.log(b64);

But I get this error:

ERROR DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.

Please help me figure out how to go from Int8Array to Base64.


Solution

  • I use the function in this answer.

    For Embed API, use the "content" parameter from the save callback as the input to the function.

    You can see a working example at this CodePen. The functional part is below.

    adobeDCView.registerCallback(
      AdobeDC.View.Enum.CallbackType.SAVE_API,
      function (metaData, content, options) {
        /* Add your custom save implementation here...and based on that resolve or reject response in given format */
        var base64PDF = arrayBufferToBase64(content);
        var fileURL = "data:application/pdf;base64," + base64PDF;
        $("#submitButton").attr("href", fileURL);
        /* End save code */
        return new Promise((resolve, reject) => {
          resolve({
            code: AdobeDC.View.Enum.ApiResponseCode.SUCCESS,
            data: {
              /* Updated file metadata after successful save operation */
              metaData: { fileName: urlToPDF.split("/").slice(-1)[0] }
            }
          });
        });
      },
      saveOptions
    );