如何使用iTextSharp的html页面转换成用CSS PDF?
问题描述:
这是我的code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
**htmlparser.Parse(sr);** //the exception here
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
该错误是:
无法投类型的对象iTextSharp.text.html.simpleparser.CellWrapper到类型
iTextSharp.text.Paragraph。
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
这是什么例外?
答
我不知道这是不是答案,但我发现,iTextSharp的是挑剔有有效的HTML。我的测试已经被打开两次,从不关闭,并且花了大约一个小时,我注意到一个表。唯一的例外是非常相似,你必须有一个。
I don't know if this is the answer, but I found that iTextSharp is picky about having valid html. My tests had a table that was opened twice and never closed and that took about an hour for me to notice. The exception was very similar to the one you have there.