我有一个基本的方法
public string PdfFriendlyHtml ( Guid pid, int vid, int sid )
{
string html = string.Empty;
html += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>";
html += "<p>blah blah blah</p>";
html += "<p>blah blah blah</p>";
html += "</td></tr>"; // end header row
List<SurveyFullInfo> Surveys = new List<SurveyFullInfo>();
for ( int k = 1; k <= sid; ++k )
{
Surveys.Add(new SurveyFullInfo
{
Title = this._Db.GetSurveyTitle(k),
ViewModel = this._Db.GetAnswersByPartner(pid, k, vid),
SectionAverages = this._Db.GetSectionAverages(pid, k, vid)
});
}
foreach ( var Survey in Surveys )
{
html += "<tr><td bgcolor=\"#EDEDED\">"; // open survey title and traffic lights
html += "<h1>" + Survey.Title + "</h1>"; // write title
html += "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
foreach ( var S in Survey.SectionAverages )
{
RangeInfo Info = AnswersController.Ranges.First(I => I.Range.X <= S.AvgScore && S.AvgScore < I.Range.Y);
html += "<tr><td><div valign=\"middle\" style=\"background-color:" + Info.ColorHex + ";height:20px;width:20px;\"></div></td>"
+ "<td><h3>" + String.Format("{0}: {1}%", S.SectionTitle, S.AvgScore.ToString()) + "</h3>"
+ "<p>" + Info.Text + "</p><td></tr>";
}
html += "</table></td></tr>"; // end survey title and traffic lights row
}
html += "</table>"; // end table
return html;
}
据我所知,生成有效的HTML并在以下方法中调用
[HttpPost]
[OutputCache(NoStore = true, Duration = 0)]
public ActionResult ToPdf ( Guid pid, int vid, int sid )
{
Byte[] bytes;
var ms = new MemoryStream();
try
{
using (var doc = new Document())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
writer.CloseStream = false;
doc.Open();
var example_html = PdfFriendlyHtml(pid, vid, sid);
var example_css = @".headline{font-size:200%}";
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
doc.Close();
}
}
bytes = ms.ToArray();
ms.Position = 0;
return File(ms, "application/pdf", "Test.pdf");
}
catch
{
ms.Dispose();
throw;
}
}
但是我得到了带有堆栈痕迹的黄色死亡屏幕
[IOException:文档没有页面。]
iTextSharp.text.pdf.PdfPages.WritePageTree()+1040
iTextSharp.text.pdf.PdfWriter.Close()+285
iTextSharp.text.pdf.PdfDocument.Close()+319
iTextSharp.text.pdf.PdfWriter.Close()+76
iTextSharp.text.DocWriter.Dispose()+10
我已经缩小了它必须与HTML有关,因为如果我将其更改为
public string PdfFriendlyHtml ( Guid pid, int vid, int sid )
{
return "<p>Test</p>";
}
它有效。
我也试过
public string PdfFriendlyHtml ( Guid pid, int vid, int sid )
{
return "<table><tr><td>Here's a cell</td><td>And here's another cell</td></tr></table>";
}
这是有效的。所以我无法弄清楚它对我的真实方法不喜欢什么。有什么想法吗?
答案 0 :(得分:1)
您的内部foreach
数据块末尾的<td>
标记不正确,应为</td>
:
RangeInfo Info = AnswersController.Ranges.First(I => I.Range.X <= S.AvgScore && S.AvgScore < I.Range.Y);
html += "<tr><td><div valign=\"middle\" style=\"background-color:" + Info.ColorHex + ";height:20px;width:20px;\"></div></td>"
+ "<td><h3>" + String.Format("{0}: {1}%", S.SectionTitle, S.AvgScore.ToString()) + "</h3>"
+ "<p>" + Info.Text + "</p></td></tr>";
// ↑HERE↑
当我做出改变时,一切都会为我编译。