【问题标题】:Adobe PDF Embed API Save Content To Base64Adobe PDF Embed API 将内容保存到 Base64
【发布时间】:2021-09-01 17:05:54
【问题描述】:

使用 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 添加到带符号的 int 以获得 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。

【问题讨论】:

    标签: pdf base64 uint8array tobase64string adobe-embed-api


    【解决方案1】:

    我使用函数in this answer

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

    您可以在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
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多