如何使用带有css的itextsharp将html页面转换为pdf [英] how to convert html page to pdf using itextsharp with css

查看:77
本文介绍了如何使用带有css的itextsharp将html页面转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//这是我的代码:



//this is my 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'。

什么是这个例外?



the error is : Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
what is this exception ?

推荐答案

我试过用itextsharp但是,这是一个漫长的过程,也不支持图像,

而不是你可以使用wkhtmltopdf转换器,只是为了传递链接的路径并通过进程类命中exe,你将得到所有数据转换为字节,然后您可以轻松下载这些字节。
I have tried by using itextsharp but, it is some of lengthy process,Also it not supporting for images,
instead of this you can use wkhtmltopdf converter, just to pass path of link and hit exe by process class, you will get all data convert into bytes, then you can easily download these bytes.


请参阅以下资源。那些可能对你有帮助。



1. 如何使用iTextSharp将HTML转换为带图像标记的PDF [ ^ ]。

2. ItextSharp尝试解析html进行pdf转换时出错 [ ^ ]。
Refer the below resources. Those might help you.

1. How To Convert HTML To PDF With Image Tags Using iTextSharp[^].
2. ItextSharp Error on trying to parse html for pdf conversion[^].


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

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