itextsharp html转换pdf,如何使用itextsharp将html页面转换为pdf与css?

在尝试使用iTextSharp库将HTML转换为PDF时,遇到了一个运行时异常。代码试图将'CellWrapper'类型的对象转换为'iTextSharp.text.Paragraph',导致转换失败。这个问题可能源于HTML到PDF的解析过程,可能需要检查HTML内容或调整解析配置。
摘要由CSDN通过智能技术生成

这是我的代码:

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'。

这是什么例外?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值