【问题标题】:Html to pdf converterhtml到pdf转换器
【发布时间】:2013-07-08 07:49:56
【问题描述】:

我的要求是将 Html 转换为 pdf 转换器,我通过代码做到了这一点。但现在我的要求是如何保存到文件夹中。

 Response.ContentType = "application/pdf";
 Response.AddHeader("content-disposition", 
 "attachment;filename=Certificate.pdf");
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 System.Text.StringBuilder ss = new System.Text.StringBuilder(CertificateHtml);
 StringWriter sw = new StringWriter(ss);
 HtmlTextWriter hw = new HtmlTextWriter(sw);
 StringReader sr = new StringReader(sw.ToString());
 Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
 HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
 PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
 pdfDoc.Open();
 htmlparser.Parse(sr);
 pdfDoc.Close();
 Response.Write(pdfDoc);
 Response.End();

请帮忙。

【问题讨论】:

  • 什么意思?这是处理程序的代码,将 pdf 发送到浏览器。

标签: asp.net c#-4.0 html-to-pdf


【解决方案1】:

你可以在Response.Write(pdfDoc);之后试试这个

Response.Write(pdfDoc);

FileStream fs = new FileStream(Server.MapPath("~/pdfFolder/pdfFile.pdf"), FileMode.Create);
StreamReader sr = new StreamReader(Response.OutputStream);
byte[] data = new byte[Response.OutputStream.Length];
Response.OutputStream.Read(data, 0, data.Length);
fs.Write(data, 0, data.Length);

fs.Flush();
fs.Close();

除此之外,您使用的第三方工具或 PDF 生成也可能为您提供此操作的功能。

【讨论】:

    【解决方案2】:

    使用这个。

    string fileName = DateTime.Now.Ticks.ToString();
    string filepath = Server.MapPath("~") + fileName + ".pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    Response.ContentType = "application/pdf";
    Response.TransmitFile(filepath);
    Response.End();
    

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 2011-02-24
      • 2011-11-16
      • 2015-07-08
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2011-07-27
      相关资源
      最近更新 更多