2012-06-28 99 views
0

我使用這個DLL iTextSharp 5.3.0來製作一個pdf文件。 有沒有辦法將PDF格式的完整.aspx頁面轉換成?我的網頁有網格和服務器端代碼。aspx to pdf using itextSharp 5.3.0

這是我的代碼:

保護無效的button1_Click(對象發件人,EventArgs的){

createPDF(Server.MapPath("Default.aspx")); 

} 


private void createPDF(string html) 
{ 


    TextReader reader = new StringReader(html); 

    // step 1: creation of a document-object 
    Document document = new Document(PageSize.A4, 30, 30, 30, 30); 

    // step 2: 
    // we create a writer that listens to the document 
    // and directs a XML-stream to a file 
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://test.pdf", FileMode.Create)); 


    HTMLWorker worker = new HTMLWorker(document); 

    document.Open(); 
    worker.StartDocument(); 
    List<IElement> p = HTMLWorker.ParseToList(new StreamReader(html), new StyleSheet()); 
    for (int k = 0; k < p.Count; k++) 
    { 
     document.Add((IElement)p[k]); 
    } 



    worker.EndDocument(); 
    worker.Close(); 
    document.Close(); 

} 

它的工作,但該文件是檢驗.pdf只是純文本。 HTML沒有很好的解釋,我的網格丟失,我的服務器端值(來自網格的值)也丟失。 我還試圖從這裏代碼: http://forums.asp.net/t/1199774.aspx 這裏: Problem with HTMLParser in Itextsharp

提前感謝!

回答

0

這是我誠實的建議!不要在HTMLWorker.ParseToList上浪費時間。它有一個非常基本的HTML解析器。 試試這個包裝,你永遠不會回頭看! https://github.com/pruiz/WkHtmlToXSharp

+0

感謝您的回答VahidN。我終於使用了wkhtmltopdf,它的工作非常完美。它使用webkit來呈現PDF,因此pdf看起來與網頁完全相同。 – user1482442

0

ITextSharp只呈現內聯css,它添加​​CSS文件時出現問題。

  System.Web.HttpContext.Current.Response.ContentType = "application/pdf"; 
      System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=BookingDetails.pdf"); 
      System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      StringWriter sw = new StringWriter(); 
      HtmlTextWriter hw = new HtmlTextWriter(sw); 

      this.CreateBookingMainDiv.RenderControl(hw); 

      StringReader sr = new StringReader(sw.ToString()); 
      Document pdfDoc = new Document(new Rectangle(922,1296),7f,7f,7f,0f); 

      PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream); 

      pdfDoc.Open(); 

      //HtmlPipeline 

      CssAppliers ca = new CssAppliersImpl(); 
      //ICssFile cfile = new CssFileProcessor(); 
      HtmlPipelineContext htmlContext = new HtmlPipelineContext(ca); 
      htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); 
      //CSS stuff 

      //var cssResolver = new StyleAttrCSSResolver(); 
      //var DamcoCss = XMLWorkerHelper.GetCSS(new FileStream(HttpContext.Current.Server.MapPath("~/css/damco.css"), FileMode.Open)); 
      ICssFile cfile = new CssFileImpl(); 

      ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true); 

      //String DamcoCss = HttpContext.Current.Server.MapPath("~/css/damco.css"); 
      //String BootStrapCss = HttpContext.Current.Server.MapPath("~/css/bootstrap.css"); 
      //String BootStrapCssTheme = HttpContext.Current.Server.MapPath("~/css/bootstrap-theme.css"); 

      //Add the external CSS file   

      //cssResolver.AddCssFile(DamcoCss, true); 
      //cssResolver.AddCssFile(BootStrapCss, true); 
      //cssResolver.AddCssFile(BootStrapCssTheme, true); 

      //Pipeline 
      IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, writer))); 
      //XMLWorker 
      XMLWorker worker = new XMLWorker(pipeline, true); 
      //and...we parse 
      XMLParser parser = new XMLParser(true, worker); 
      //parser.AddListener(worker); 
      parser.Parse(sr); 
      parser.Flush(); 
      pdfDoc.Close(); 

      System.Web.HttpContext.Current.Response.Write(pdfDoc); 
      System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); 
      //System.Web.HttpContext.Current.Response.End(); 

使用XMLWorker而不是HTMLWorker。像魅力一樣工作。