This is a migrated thread and some comments may be shown as answers.

[Solved] RadGrid - PDF Export is not working

4 Answers 208 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vivek
Top achievements
Rank 2
Vivek asked on 20 Mar 2013, 09:04 AM
Hi,  Presently I'm using the rad grid and facing a problem with PDF download where data is not getting displayed in the downloaded PDF file.  I investigated the cause and found that <table> tag is creating the issue. Since I'm using the <table> tag inside the <ItemTemplate> and while downloading data in PDF, data is not getting displayed. Not sure why rad grid is behaving in this way.
I'm attaching the demo code for your look, please go through it and let me know the solution to proceed further.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PDFXLDownload.aspx.cs"
    Inherits="PDFXLDownload.PDFXLDownload" %>
  
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnableScriptCombine="false">
    </telerik:RadScriptManager>
    <telerik:RadGrid runat="server" ID="myGrid" EnableEmbeddedSkins="false" GridLines="None"
        ShowFooter="false" AllowFilteringByColumn="false" AllowSorting="false" ShowGroupPanel="false"
        AllowPaging="false" PageSize="10">
        <mastertableview autogeneratecolumns="False" commanditemdisplay="Bottom">
            <Columns>
                <telerik:GridTemplateColumn>                   
                    <HeaderTemplate>
                        <asp:Label ID="labelName" runat="server" Text="Name"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="labelName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>  
                        <asp:Label ID="labelSName" runat="server" Text='<%# Bind("SName") %>'></asp:Label>  
                        <asp:Label ID="labelStatus" runat="server" CssClass="FundStatus" Text='<%# Bind("Status") %>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <HeaderStyle CssClass="GridHeaderItem" />
                    <ItemStyle CssClass="GridItem" />
                    <HeaderTemplate>
                        <asp:Label ID="labelSymbol" runat="server" Text="Symbol"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="labelSymbol" runat="server" Text='<%# Bind("Symbol") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <HeaderStyle CssClass="GridHeaderItem" />
                    <ItemStyle CssClass="GridItem" />
                    <HeaderTemplate>
                        <asp:Label ID="labelMER" runat="server" Text="Rating"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="labelMER" runat="server" Text='<%# Bind("MSN") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <HeaderStyle CssClass="GridHeaderItem" />
                    <ItemStyle CssClass="GridItem" />
                    <HeaderTemplate>
                        <asp:Label ID="lblNER" runat="server" Text="NER"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                     <table>
                            <tr>
                                <td>
                                    <asp:Label ID="lblNER" runat="server" Text='<%# Bind("NER") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                         
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <HeaderStyle CssClass="GridHeaderItem" />
                    <ItemStyle CssClass="GridItem" />
                    <HeaderTemplate>
                        <asp:Label ID="lblGER" runat="server" Text="GER"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="lblGER" runat="server" Text='<%# Bind("GER") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <HeaderStyle CssClass="GridHeaderItem" />
                    <ItemStyle CssClass="GridItem" />
                    <HeaderTemplate>
                        <asp:Label ID="lbl3Y" runat="server" Text="3Y"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="lbl3Y" runat="server" Text='<%# Bind("ThreeYear") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn>
                    <HeaderStyle CssClass="GridHeaderItem" />
                    <ItemStyle CssClass="GridItem" />
                    <HeaderTemplate>
                        <asp:Label ID="lbl5Y" runat="server" Text="5Y"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                      <table>
                            <tr>
                                <td>
                                    <asp:Label ID="lbl5Y" runat="server" Text='<%# Bind("FiveYear") %>'></asp:Label>
                                </td>
                            </tr>
                        </table>
                         
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>           
        </mastertableview>
    </telerik:RadGrid>
    <asp:Button ID="DownloadPDFButton" runat="server" OnClick="DownloadPDFButton_Click"
        Text="Download PDF" Width="184px" />
    </form>
</body>
</html>

using

 

 

System;

 

using

 

 

System.Collections.Generic;

 

using

 

 

System.Web.UI.WebControls;

 

using

 

 

Telerik.Web.UI;

 

namespace

 

 

PDFXLDownload

 

{

 

 

public partial class PDFXLDownload : System.Web.UI.Page

 

{

#region

 

 

Event Handler

 

 

 

protected void Page_Load(object sender, EventArgs e)

 

{

myGrid.DataSource =

 

MyTestData.GetMyTestDataData();

 

myGrid.DataBind();

}

 

 

protected void DownloadPDFButton_Click(object sender, EventArgs e)

 

{

myGrid.ExportSettings.OpenInNewWindow =

 

true;

 

myGrid.ExportSettings.FileName =

 

"PDFExportDemo";

 

myGrid.ExportSettings.ExportOnlyData =

 

false;

 

 

 

//Landscape

 

myGrid.ExportSettings.Pdf.PageHeight =

 

Unit.Parse("10in");

 

myGrid.ExportSettings.Pdf.PageWidth =

 

Unit.Parse("16in");

 

 

 

//// 1/2 inch margins

 

myGrid.ExportSettings.Pdf.PageLeftMargin =

 

Unit.Parse("1in");

 

myGrid.ExportSettings.Pdf.PageRightMargin =

 

Unit.Parse("1in");

 

myGrid.ExportSettings.Pdf.PageTopMargin =

 

Unit.Parse("1in");

 

myGrid.ExportSettings.Pdf.PageBottomMargin =

 

Unit.Parse("1in");

 

myGrid.ExportSettings.FileName =

 

"IP";

 

myGrid.MasterTableView.ExportToPdf();

}

#endregion

#region

 

 

Test Data

 

 

 

public class MyTestData

 

{

 

 

public string Name { get; set; }

 

 

 

public string SName { get; set; }

 

 

 

public string Status { get; set; }

 

 

 

public string Symbol { get; set; }

 

 

 

public string MSN { get; set; }

 

 

 

public string NER { get; set; }

 

 

 

public string GER { get; set; }

 

 

 

public string OneQ { get; set; }

 

 

 

public string YTD { get; set; }

 

 

 

public string OneYear { get; set; }

 

 

 

public string ThreeYear { get; set; }

 

 

 

public string FiveYear { get; set; }

 

 

 

public string TenYear { get; set; }

 

 

 

public string Risk { get; set; }

 

 

 

public static List<MyTestData> GetMyTestDataData()

 

{

 

 

List<MyTestData> myDummyData = new List<MyTestData>

 

{

 

 

new MyTestData

 

{

Name =

 

"SRW1", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW2", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW3", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW4", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW5", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW6", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW7", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW8", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW9", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

 

 

new MyTestData

 

{

Name =

 

"SRW10", SName="S2", Status="R",

 

Symbol =

 

"N/A", MSN="N/A", NER="0.46", GER="0.46",

 

OneYear=

 

"00.00%", ThreeYear="00.00%", FiveYear="00.00%"

 

 

},

};

 

 

return myDummyData;

 

}

}

#endregion

}

}

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 21 Mar 2013, 09:14 AM
Hi,

 
There are a few rules that should be followed when exporting HTML tables to PDF:

  • The table should define <colgroup> and <col> elements

  • The number of col elements should be equal to the number of the columns in the table body

  • Width in pixels (or another absolute units) should be set to the table.

    Check the following help documentation for more.
    PDF Export

    Thanks,
    Shinu

0
Vivek
Top achievements
Rank 2
answered on 16 May 2013, 05:21 PM
Hi Shinu,

As you said -  "Width in pixels (or another absolute units) should be set to the table."

But In my scenario, I really can not hard code anything style related stuff in the html, everything should come from css classes.

Is there any alternative way to avoid setting width explicitly.

What happens if I set the width on table explicitly, will it change my UI? as actual width is coming from css.

For Example -

Width in css class is : 20%

And if I hardcode width in table as 200px which may not match with width in % then this may affect my UI as well.

Please let me know your thoughts on this.
0
Vivek
Top achievements
Rank 2
answered on 20 May 2013, 06:08 AM
Is there any update on this?
0
Kostadin
Telerik team
answered on 21 May 2013, 11:43 AM
Hi Vivek,

I am afraid you have to hardcoded the width of the tables in the templates. There is no workaround for this scenario.

Greetings,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Vivek
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
Vivek
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or