2013-07-08 41 views
2

我的要求是转换Html到PDF转换器,我做了这个虽然代码。但现在我的要求是我如何保存到文件夹。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(); 

请帮忙。

+0

你是什么意思?这是一个处理程序的代码,将PDF发送到浏览器。 – Aristos

回答

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生成可被过多您提供此操作功能的第三方工具试试这个。

1

使用这个。

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();