Conversion of html to pdf

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    Conversion of html to pdf

    I am converting an html into pdf, I have observed that the pdf is getting generated but not properly.

    i.e. the Datalist2 and the gridview2 present inside the Datalist2 is not getting populated.

    Any idea why is it working so.

    I have the html code as below
    Code:
      <table id="tblReport" runat="server">
                <asp:DataList ID="DataList1" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Label ID="lbl1" runat="server">  </asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:GridView ID="GridView1" runat="server">
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lbl1" runat="server">  </asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:GridView ID="GridView2" runat="server">
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:Label ID="lbl1" runat="server">  </asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:DataList ID="DataList2" runat="server">
                                    <ItemTemplate>
                                        <table id="tblvalues" runat="server">
                                            <tr>
                                                <td>
                                                    <asp:GridView ID="GridView2" runat="server">
                                                        <Columns>
                                                            <asp:TemplateField>
                                                                <ItemTemplate>
                                                                    <asp:Label ID="lbl1" runat="server">  </asp:Label>
                                                                </ItemTemplate>
                                                                <FooterTemplate>
                                                                    <asp:Label ID="lbl1" runat="server">  </asp:Label>
                                                                </FooterTemplate>
                                                            </asp:TemplateField>
                                                        </Columns>
                                                    </asp:GridView>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:DataList>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:DataList>
            </table>
    I convert using itextsharp the code is as below
    Code:
      Response.ContentType = "application/pdf";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            System.IO.StringWriter stringWrite = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            tblReport.RenderControl(htmlWrite);
            //tblReport is a table  
            StringReader reader = new StringReader(textConvert(stringWrite.ToString()));
            Document doc = new Document(PageSize.A4);
            HTMLWorker parser = new HTMLWorker(doc);
            PdfWriter.GetInstance(doc, Response.OutputStream);
            doc.Open();
            parser.Parse(reader);
            doc.Close();
    Can anyone let me know as to why i am not able to convert that datalist into pdf?
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    There is good example to export gridview to pdf .Pls have a look into that

    Comment

    • zxfr
      New Member
      • Jan 2010
      • 2

      #3
      If you have nothing against commercial tools try .NET version of PD4ML.
      PD4ML is a PDF generating library (managed code) uses HTML and CSS as page layout and content definition format.

      The API is quite straightforward (it converts to PDF either URL or HTML, represented as a string):

      Code:
      using System;
      using System.Collections;
      using System.Drawing;
      using System.Reflection;
      using System.IO;
      using org.zefer.pd4ml;
      
      ...
      
      private void generatePDF(String inputUrl, int htmlWidth,
                   String pageFormat, String orientation, String outfile) {
      
          pd4ml = new PD4ML();
          pd4ml.HtmlWidth = (htmlWidth);
      
          if(orientation == null)
              orientation = "portrait";
      
          if ("PORTRAIT".Equals(orientation.ToUpper()))
              pd4ml.PageSize = PD4Constants.getSizeByName(pageFormat);
          else
              pd4ml.PageSize = pd4ml.changePageOrientation( PD4Constants.getSizeByName(pageFormat) );
      
          if ( outfile == null || outfile.Equals("-")  )
              pd4ml.render(new Uri(inputUrl), Console.Out);
          else
              pd4ml.render( new Uri(inputUrl), new System.IO.FileStream(outfile, System.IO.FileMode.Create) );
      }
      
      ...
      More info: http://pd4ml.com/dotnet.htm

      Comment

      • cmrhema
        Contributor
        • Jan 2007
        • 375

        #4
        Originally posted by mzmishra
        There is good example to export gridview to pdf .Pls have a look into that
        http://csharpdotnetfreak.blogspot.co...textsharp.html
        Actually I have noticed (I may be wrong too), that when I have multiple controls listed into one another. eg. In the final version of my application, I had a datalist and three gridviews inside the datalist, also among this i had one nested gridview.

        During such occassions, if I simply convert to pdf it does not.

        But however it was resolved using Chunks.
        Instead of just having a gridview I converted into IList and then split it into Chunks and cells.

        The issue was resolved.

        Comment

        Working...