Adobe PDF Embed API 将内容保存到 Base64

问题描述

使用 Adob​​e PDF Embed API,您可以注册回调:

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

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

})

内容根据此处的文档:https://www.adobe.io/apis/documentcloud/dcsdk/docs.html?view=view

content: The ArrayBuffer of file content

当我使用 chrome 检查器调试此内容时,它显示内容一个 Int8Array。

通常,当我们上传 pdf 文件时,用户选择一个文件,我们将其读取为 dataURI 并获取 base64 并将其推送到 AWS。所以我需要把这个PDF的数据(Int8Array)转换成Base64,这样我也可以把它推送到AWS。

我在网上找到的所有内容都使用 UInt8Array 到 base64,我不明白如何从 Int8Array 转到 UInt8Array。我认为您可以将 128 添加到有符号整数以获得 0-256 之间的比率,但这似乎不起作用。

我试过用这个:

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

但我收到此错误

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

请帮我弄清楚如何从 Int8Array 转到 Base64。

解决方法

我使用函数 in this answer

对于嵌入 API,使用保存回调中的“内容”参数作为函数的输入。

您可以在 this CodePen 看到一个工作示例。功能部分如下。

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
);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...