将它写入文件?"/>
在Docusign Node SDK中调用envelopesApi.getDocument时,返回的数据是什么格式?我如何将它写入文件?
成功登录并从信封中获取文档列表后,我正在尝试使用 DocuSign Node SDK 从 DocuSign 检索签名文档,使用以下代码:
envelopesApi.getDocument(accountId, envelopeId, documents[0].documentId, function(err, data, response) {
log.info({data: data.substring(0, 100)}, 'getDocument data');
log.info({response}, 'getDocument response');
fs.writeFile('/home/martin/downloaded.pdf', data, (err) => {
next(err);
});
});
data
变量是一个字符串。它不是 base64 编码的。第一个日志记录语句(使用 Bunyan 日志记录模块)显示字符串以这些字符开头:
%PDF-1.4
%ûüýþ
4 0 obj
<<
/Parent 3 0 R
/Resources 5 0 R
/MediaBox [0.00000 0.00000 595.00000 842.00
因此我可以看到它不是 base64 编码的。将 pdf 文件的内容保存在字符串中对我来说似乎很奇怪。我期待一个
Buffer
对象。
当我(在 Chrome 中)打开此代码保存的文件时,它似乎是一个有效的 pdf 文件(即 Chrome 没有错误提示文件已损坏),并且它的页数正确,但它完全不可读。页面上根本没有清晰的文字,表明某些内容已损坏。
查看 SDK 中的 EnvelopesApi.js 和 ApiClient.js 文件,我可以看到它正在请求 PDF,并且 ApiClient 中有专门用于处理 PDF 的代码——这似乎是从可读流中读取的&附加到一个字符串。
我知道有一个不使用 NOde SDK 的替代方法,直接使用 REST API(根据官方REST API 食谱:获取信封的文档列表中的示例),但我想尽可能使用 SDK。
我错过了我应该用这个
data
参数做的事情吗?
回答如下:
查看 api 配方以下载文档here
这里是下载文档的示例代码。
envelopesApi.getDocument(accountId, envelopeId, documentId, function (error, document, response) {
if (error) {
console.log('Error: ' + error);
return;
}
if (document) {
try {
var fs = require('fs');
var path = require('path');
// download the document pdf
var filename = accountId + '_' + envelopeId + '_' + documentId + '.pdf';
var tempFile = path.resolve(__dirname, filename);
fs.writeFile(tempFile, new Buffer(document, 'binary'), function (err) {
if (err) console.log('Error: ' + err);
});
console.log('Document ' + documentId + ' from envelope ' + envelopeId + ' has been downloaded to ' + tempFile);
} catch (ex) {
console.log('Exception: ' + ex);
}
}
});
更多推荐
在Docusign Node SDK中调用envelopesApi.getDocument时,返回的数据是什么格式?我如何将它写入文件?
发布评论