2009-04-27 31 views
2

有谁知道如何获取html内容,将其转换为pdf并将其保存到数据库?如何获取html内容并使用itextsharp将其转换为PDF?

我已经尝试了很多方法,但似乎没有任何工作。在一些文章,这是写使用HTMLParse,在别人HTMLWorker ...有时会引发错误“的文件已经没有网页” ......有时,它只是抛出一个异常,但它没有指定错误...

有没有人知道这样做的好方法?

我使用C#3.0,ASP.NET MVC和LinQToSQL。

在此先感谢和很多!

回答

2

对于HTML到PDF我往往看HTMLDoc。这是一个exe文件,而不是一个库,所以它可能不是你正在寻找的东西,但它可能会让你超越驼峰。

望着iText的书,Lowagie说:

“之一iText的邮件列表上频繁的问题是,“是否iText的提供HTML2PDF功能?官方的答案是否定的,建议您使用HtmlDoc或ICEBrowser。“第456

他接着展现的HTMLParser的例子,但强调这不是高达解析和呈现HTML的巨大工作。

0
public class Pdf : IPdf 
    { 
     public FileStreamResult Make(string s) 
     { 
      using (var ms = new MemoryStream()) 
      { 
       using (var document = new Document()) 
       { 
        PdfWriter.GetInstance(document, ms); 
        document.Open(); 
        using (var str = new StringReader(s)) 
        { 

         var htmlWorker = new HTMLWorker(document); 

         htmlWorker.Parse(str); 
        } 
        document.Close(); 
       } 

       HttpContext.Current.Response.ContentType = "application/pdf"; 
       HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf"); 
       HttpContext.Current.Response.Buffer = true; 
       HttpContext.Current.Response.Clear(); 
       HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); 
       HttpContext.Current.Response.OutputStream.Flush(); 
       HttpContext.Current.Response.End(); 

       return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf"); 
      } 
     } 
    } 
相关问题