如何使用iTextSharp的html页面转换成用CSS PDF? [英] how to convert html page to pdf with css using itextsharp?

查看:115
本文介绍了如何使用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.

这篇关于如何使用iTextSharp的html页面转换成用CSS PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆