James
James

Reputation: 3805

iTextSharp asp:DataGrid does not show up in PDF

Converting a generated html page to pdf using iTextSharp, I cannot get the table created from the asp:DataGrid to show up in the PDF. The page's HTML output for the datagrid looks like this:

<p>Purchases:</p>
    <table rules="all" id="dg_Purchase" style="font-size: smaller; border-collapse: collapse;" border="1" cellspacing="0">
    <tbody><tr>
        <td>Lot #</td>
        <td>&nbsp;</td>
        <td>Title</td>
        <td align="right">Price</td>
        <td align="right">Buyer's Premium</td>
        <td align="right">Tax</td>
        <td align="right">Sub Total</td>
    </tr><tr>
        <td>118</td>
        <td><input id="dg_Purchase_ctl02_CheckBox1" name="dg_Purchase$ctl02$CheckBox1" type="checkbox"></td>
        <td>Yih-Wen Kuo (1959, Taiwan)</td>
        <td align="right">$1,900.00</td>
        <td align="right">$332.50</td>
        <td align="right">$0.00</td>
        <td align="right">$2,232.50</td>
    </tr><tr>
        <td>119</td>
        <td><input id="dg_Purchase_ctl03_CheckBox1" name="dg_Purchase$ctl03$CheckBox1" type="checkbox"></td>
        <td>Yih-Wen Kuo (1959, Taiwan)</td>
        <td align="right">$1,800.00</td>
        <td align="right">$315.00</td>
        <td align="right">$0.00</td>
        <td align="right">$2,115.00</td>
    </tr>
</tbody></table>

<p>Payments:</p>
    <table rules="all" id="dg_Payment" style="font-size: smaller; border-collapse: collapse;" border="1" cellspacing="0">
    <tbody><tr>
        <td style="width: 120px;">Payment Method</td>
        <td>Date</td>
        <td align="right">Amount&nbsp;&nbsp;</td>
    </tr><tr>
        <td>Total</td>
        <td>&nbsp;</td>
        <td align="right">$0.00</td>
    </tr>
</tbody></table>

When the HTML page is viewed, the table looks exactly how it should. But when I generate the PDF, the table (or rather, everything within the table) is not shown. Only the border is visible, pretty much being just a 2 px straight line for each table.

So in my normal script, after the entire page has loaded, the very last thing I do is:

    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
    Render(writer);

The functions for iTextSharp are below:

protected override void Render(HtmlTextWriter writer)
{
    MemoryStream mem = new MemoryStream();
    StreamWriter twr = new StreamWriter(mem);
    HtmlTextWriter myWriter = new HtmlTextWriter(twr);
    base.Render(myWriter);
    myWriter.Flush();
    myWriter.Dispose();
    StreamReader strmRdr = new StreamReader(mem);
    strmRdr.BaseStream.Position = 0;
    string pageContent = strmRdr.ReadToEnd();
    strmRdr.Dispose();
    mem.Dispose();
    writer.Write(pageContent);
    CreatePDFDocument(pageContent);
}

public void CreatePDFDocument(string strHtml)
{

    string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    // we create a writer that listens to the document
    PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
    StringReader se = new StringReader(strHtml);
    HTMLWorker obj = new HTMLWorker(document);
    document.Open();
    obj.Parse(se);
    document.Close();
    ShowPdf(strFileName);
}

public void ShowPdf(string strFileName)
{
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
    Response.ContentType = "application/pdf";
    Response.WriteFile(strFileName);
    Response.Flush();
    Response.Clear();
}

Again, everything on the page that is set programatically does show up in the PDF EXCEPT for the datagrid. Going off the borders, it appears that it is not a width issue. It just appears to be empty, is all. Any help would be greatly appreciated.

Upvotes: 0

Views: 571

Answers (1)

James
James

Reputation: 3805

Well I couldn't find rhyme or reason as to why this would not work, so I went another route. I decided to make the page that generates the HTML code a secondary page, and have the one generating the PDF just reference it. I ended up with:

String address = "http://address.com/admin/Invoice2.aspx?sale_id=" + sale_id + "&bidder_number=" + bidder_number;

using (WebClient wc = new WebClient())
{
    string content = wc.DownloadString(address);
    CreatePDFDocument(content);
}

in my page load method, and just kept the other 2 functions:

public void CreatePDFDocument(string strHtml)
{

    string strFileName = HttpContext.Current.Server.MapPath("invoice.pdf");
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    // we create a writer that listens to the document
    PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
    StringReader se = new StringReader(strHtml);
    HTMLWorker obj = new HTMLWorker(document);
    document.Open();
    obj.Parse(se);
    document.Close();
    ShowPdf(strFileName);
}

public void ShowPdf(string strFileName)
{
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
    Response.ContentType = "application/pdf";
    Response.WriteFile(strFileName);
    Response.Flush();
    Response.Clear();
}

Now I just need to get the styling of the tables to transfer to the PDF and I'll be good. But that's another issue.

Upvotes: 0

Related Questions