2016-12-07 16 views
1

Docusign webhooksを受け取るリスナーページを作成しました。 Webhookからデータを取り出すまではすべて動作していますが、DocumentPDFを循環させるとPDFファイルが作成されますが、PDFファイルは破損して開くことができません(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

レシピexample webhook listenerの線402を参照してください。おそらく 'byteStr'はbase64でエンコードされており、実際のpdfを得るためにはそれをデコードする必要がありますか? – mkl

+0

ようこそStackOverflowへ!他人の質問を含め、すべての有益な回答をupvoteしてください。そして、あなた自身の質問に対する最良の答えを選択/チェックしてください。 –

答えて

2

@mklが正しいです。Webhook(Connect)通知メッセージのPDFコンテンツはbase6です4エンコードされます。デコードしてPDFファイルを入手します。

例: - 私はDocuSignのAPIを知らないが、テキスト文字列としてPDF文書を書くことはほとんど正しいことはできません

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

mlkとLarry K、どうもありがとうございました。私のbase64文字列を解読してpdfファイルに変換できました。 – RVille

+0

@RVille答えが気に入ったら、upvoteして "チェック"してください(これが最善の答えです)。 –

関連する問題