2016-12-07 95 views
1

我創建了一個接收Docusign網頁鉤的偵聽器頁面。儘管從webhook獲取數據,但所有內容都起作用,但是當我循環訪問DocumentPDF時,它會創建PDF文件,但它們已損壞且無法打開(當我嘗試在Acrobat中打開它時,我收到以下消息:Acrobat無法打開...因爲它是要麼不支持的文件類型或因爲該文件已損壞。「)已保存的Docusign文檔PDF已損壞

任何人可以幫助我弄清楚爲什麼創建的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 + ";"); 
     } 
    } 
+0

我不知道的DocuSign API,但寫一個PDF文檔的文本字符串也很難是正確的。可能'byteStr'是base64編碼的,你必須解碼才能得到實際的pdf? – mkl

+0

歡迎來到StackOverflow!請提供所有有用的答案,包括對其他人的問題。請選擇/檢查您自己問題的最佳答案。 –

回答

2

@mkl是正確的。Webhook(Connect)通知消息的PDF內容是base6 4編碼。解碼它以獲取PDF文件。

一個例子:看配方example webhook listener線402 -

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

mlk和Larry K,非常感謝。能夠解碼我的base64字符串並將其轉換爲pdf文件。 – RVille

+0

@RVille如果你喜歡一個答案,upvote它和「檢查」它(如果這是最好的答案)。 –