【发布时间】: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