使用iTextSharp将HTML转换为PDF时出现问题 [英] Problem with converting HTML to PDF with iTextSharp

查看:321
本文介绍了使用iTextSharp将HTML转换为PDF时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  btnPdf_Click(对象发​​件人,EventArgs e)
{
字符串 attachment = attachment; filename = + D :\ abc + 。pdf;
Response.ClearContent();
Response.AddHeader( content-disposition,附件);
Response.ContentType = application / pdf;
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
h_textw.AddStyleAttribute( font-size 7pt);
h_textw.AddStyleAttribute( color 黑色);
Panel1.RenderControl(h_textw); // 面板名称
文件doc = new Document();
doc = new 文档(PageSize.A4, 5 5 15 5 );
FontFactory.GetFont( Verdana 80 ,iTextSharp.text.Color.RED);
PdfWriter.GetInstance(doc,Response.OutputStream);
doc.Open();
StringReader s_tr = new StringReader(s_tw.ToString());
HTMLWorker html_worker = new HTMLWorker(doc);
html_worker.Parse(s_tr);
doc.Close();
Response.Write(doc);
}





我使用了以下参考文献:



 使用 iTextSharp.text; 
使用 iTextSharp.text.pdf;
使用 iTextSharp.text.html;
使用 iTextSharp.text.html.simpleparser;
使用 System.Drawing;





在上面的编码中,要将网页(HTML输出)转换为PDF,我在html_worker.Parse(s_tr);​​收到错误说找不到网络路径。

请帮助我

解决方案

Escape

  string  attachment =   attachment; filename = +   D:\abc +  。pdf; 

to

  string  attachment =   attachment; filename = +  @  D:\abc +  。 pdf; 

  string  attachment =    attachment; filename = +   D:\\abc +  。pdf; 


protected void btnPdf_Click(object sender, EventArgs e)
        {
            string attachment = "attachment; filename=" + "D:\abc" + ".pdf";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/pdf";
            StringWriter s_tw = new StringWriter();
            HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
            h_textw.AddStyleAttribute("font-size", "7pt");
            h_textw.AddStyleAttribute("color", "Black");
            Panel1.RenderControl(h_textw);//Name of the Panel
            Document doc = new Document();
            doc = new Document(PageSize.A4, 5, 5, 15, 5);
            FontFactory.GetFont("Verdana", 80, iTextSharp.text.Color.RED);
            PdfWriter.GetInstance(doc, Response.OutputStream);
            doc.Open();
            StringReader s_tr = new StringReader(s_tw.ToString());
            HTMLWorker html_worker = new HTMLWorker(doc);
            html_worker.Parse(s_tr);
            doc.Close();
            Response.Write(doc);
}



I have used following References:

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Drawing;



In the above coding, to convert a web page (HTML output) to PDF, I am getting an error at "html_worker.Parse(s_tr);" saying that "The network path was not found."
Kindly help me

解决方案

Escape

string attachment = "attachment; filename=" + "D:\abc" + ".pdf";

to

string attachment = "attachment; filename=" + @"D:\abc" + ".pdf";

or

string attachment = "attachment; filename=" + "D:\\abc" + ".pdf";


这篇关于使用iTextSharp将HTML转换为PDF时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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