【问题标题】:Saved Docusign Document PDF's come out corrupted保存的 Docusign 文档 PDF 出现损坏
【发布时间】:2016-12-07 01:41:56
【问题描述】:

我创建了一个接收 Docusign webhook 的侦听器页面。就从 webhook 中提取数据而言,一切正常,但是当我循环浏览 DocumentPDF 时,它会创建 PDF 文件,但它们已损坏且无法打开(当我尝试在 Acrobat 中打开它时,我收到以下消息:Acrobat无法打开...因为它不是受支持的文件类型或文件已损坏。")

谁能帮我弄清楚为什么创建的 pdf 文件损坏了?

我的页面代码如下:

 protected void Page_Load(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader(Request.InputStream);
        string xml = sr.ReadToEnd();
        string fileName = HttpContext.Current.Server.MapPath("") + "\\Results\\" + DateTime.Now.Ticks + ".xml";
        File.WriteAllText(fileName, xml);


        try
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml);

            var mgr = new XmlNamespaceManager(xmldoc.NameTable);
            mgr.AddNamespace("a", "http://www.docusign.net/API/3.0");

            XmlNode envelopeStatus = xmldoc.SelectSingleNode("//a:EnvelopeStatus", mgr);
            XmlNode envelopeId = envelopeStatus.SelectSingleNode("//a:EnvelopeID", mgr);
            XmlNode status = envelopeStatus.SelectSingleNode("//a:Status", mgr);

            if (status.InnerText == "Completed")
            {
                LogException("Looking for DocPDF's_" + DateTime.Now.Ticks + ";");
                // Loop through the DocumentPDFs element, storing each document.

                XmlNode docs = xmldoc.SelectSingleNode("//a:DocumentPDFs", mgr);
                foreach (XmlNode doc in docs.ChildNodes)
                {
                    string documentName = doc.ChildNodes[0].InnerText; // pdf.SelectSingleNode("//a:Name", mgr).InnerText;
                    string documentId = doc.ChildNodes[2].InnerText; // pdf.SelectSingleNode("//a:DocumentID", mgr).InnerText;
                    string byteStr = doc.ChildNodes[1].InnerText; // pdf.SelectSingleNode("//a:PDFBytes", mgr).InnerText;

                    LogException("Writing Out PDF_" + HttpContext.Current.Server.MapPath("") + "\\Documents\\" + envelopeId.InnerText + "_" + documentId + "_" + documentName + "_" + DateTime.Now.Ticks + ";");

                    File.WriteAllText(HttpContext.Current.Server.MapPath("") + "\\Documents\\" + envelopeId.InnerText + "_" + documentId + "_" + documentName, byteStr);

                    LogException("Successfully wrote out PDF_" + DateTime.Now.Ticks + ";");
                }
            }
        }
        catch (Exception ex)
        {
            LogException("Exception: " + ex.Message + "; InnerException: " + ex.InnerException.ToString() + "_" + DateTime.Now.Ticks + ";");
        }
    }

【问题讨论】:

  • 我不知道 Docusign api,但是将 pdf 文档作为文本字符串编写几乎是不正确的。 byteStr 可能是 base64 编码的,您必须对其进行解码才能获得实际的 pdf?
  • 欢迎来到 StackOverflow!请为所有有用的答案投票,包括对他人问题的回答。请选择/检查您自己问题的最佳答案。

标签: pdf docusignapi


【解决方案1】:

@mkl 是正确的。 Webhook (Connect) 通知消息的 PDF 内容采用 base64 编码。解码得到PDF文件。

一个例子:见配方example webhook listener的第402行--

pdf_file.write(base64.b64decode(pdf.PDFBytes.string))

【讨论】:

  • mlk 和 Larry K,非常感谢。能够解码我的 base64 字符串并将其转换为 pdf 文件。
  • @RVille 如果您喜欢某个答案,请点赞并“检查”它(如果它是最佳答案。)
  • 我正在向docusign发送word文档(字节)。我正在收到电子邮件,我正在签署该文档。在侦听器中,我收到了 xml 响应,并且我能够获取 pdf 字节,但是当我从中生成文件时,它已损坏。
  • 嗨@sanjay,请提出一个新问题。
【解决方案2】:

我正在使用 Azure 函数并使用 Blob 存储来保存它。 这对我有用:

var byteStr = doc.ChildNodes[1].InnerText;
outputBlob.Write(System.Convert.FromBase64String(byteStr));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多