请求并保存后,DocuSign文档为空白

时间:2018-11-30 16:18:48

标签: docusignapi

通过DocuSign api请求文档并将其写入文件系统后,打开文档后将显示为空白。文档说它返回一个“ PDF文件”,并且响应正文如下所示。

const doc = 
    await rp.get(`${apiBaseUrl}/${BASE_URI_SUFFIX}/accounts/${accountId}/envelopes/${envelopeId}/documents/${document.documentId}`,
    {auth: { bearer: token }}
    );

fs.writeFile(document.name, new Buffer(doc, "binary"), function(err) {
        if (err) throw err;
        console.log('Saved!');
});

响应正文:

{
"documents": [
    {
        "name": "Name of doc.docx",
        "content": "%PDF-1.5\n%\ufffd\ufffd\ufffd\ufffd\n%Writing objects...\n4 0 obj\n<<\n/Type /Page\n/Resources 5 0 R\n/Parent 3 0 R\n/MediaBox [0 0 612 792 ]\n/Contents [6 0 R 7 0 R 8 0 R 9 0 R 10 0 R ]\n/Group <<\n/Type /Group\n/S /Transparency\n/CS /DeviceRGB\n>>\n/Tabs /S\n/StructParents 0\n>>\nendobj\n5 0 obj\n<<\n/Font <<\n/F1 11 0 R\n/F2 12 0 R\n/F3 13 0 R\n>>\n/ExtGState <<\n/GS7 14 0 R\n/GS8 15 0 R\n>>\n/ProcSet [/PDF /Text ...
    }
]}

文档截图: enter image description here

1 个答案:

答案 0 :(得分:1)

EnvelopeDocuments::get API方法将返回PDF本身,而不是所显示的对象。

有关该方法的有效示例,请参阅example 7,它是Node.js示例集中的一部分。

已添加

此外,fs.writeFile调用支持从字符串源进行写入。我会尝试:

fs.writeFile(document.name, doc, {encoding: "binary"},
    function(err) {
        if (err) throw err;
        console.log('Saved!');
    });

编码错误

您的问题将pdf的内容显示为字符串,并且控制字符编码为unicode字符串:

"%PDF-1.5\n%\ufffd\ufffd\ufffd\ufffd\n%Writing objects...

但这是不正确的。 PDF文件的开头包含只能在十六进制编辑器中显示的二进制字符。这是您应该在PDF顶部看到的内容:

enter image description here

请注意第10个字符。它是十六进制c4。在您的字符串中,等效字符已编码为\ufffd(可以确定它们不是同一字符,它们是两个不同的PDF)。字符已编码的事实是您的问题。

解决方案

  1. 说服请求库和fs.WriteFile方法不对数据进行编码。或根据需要对其进行解码。有关请求库,请参见this solution
  2. 或者使用上面引用的示例代码中所示的DocuSign Node.js SDK。