2009-04-27 92 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"); 
      } 
     } 
    }