2016-12-07 2 views
1

Docusign webhook을받는 리스너 페이지를 만들었습니다. 모든 것이 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에 오신 것을 환영합니다! 다른 사람들의 질문에 대한 대답을 포함하여 모든 유용한 대답을 upvote하십시오. 그리고 자신의 질문에 대한 최선의 대답을 선택/확인하십시오. –

답변

2

@mkl Webhook (연결) 알림 메시지의 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하고 "확인"하십시오 (가장 좋은 대답 인 경우). –