0

I am using this dll iTextSharp 5.3.0 to make a pdf file . Is there a way to convert full .aspx page in pdf ? My page has grids and server side code .

This is my code:

protected void Button1_Click(object sender, EventArgs e) {

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

}

It's working but the file test.pdf is just plain text. The html isn't well interpreted, my grids are missing and my server side values (the values from the grids) are also missing . I also tried the codes from here: http://forums.asp.net/t/1199774.aspx and here: Problem with HTMLParser in Itextsharp

Thanks in advance!

Community
  • 1
  • 1
user1482442
  • 567
  • 2
  • 7
  • 14

2 Answers2

0

This is my honest advice! Don't waste your time on the HTMLWorker.ParseToList. It has a very elementary HTML parser. Try this packge and you will never look back! https://github.com/pruiz/WkHtmlToXSharp

VahidN
  • 18,457
  • 8
  • 73
  • 117
  • Thanks for your answer VahidN. I finally used wkhtmltopdf and it's working perfect. It's using a webkit to render the pdf , so the pdf looks exactly like the webpage. – user1482442 Jun 29 '12 at 10:12
0

ITextSharp only renders inline css, it is giving problem while adding CSS files.

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

Use XMLWorker instead of HTMLWorker. works like charm.

Abhijit_Srikumar
  • 106
  • 1
  • 13