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

Export to PDF

8 Answers 164 Views
PivotGrid
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 28 Feb 2014, 06:48 PM
I understand there is no current support for directly creating a PDF from a PivotGrid.

I am attempting to use a third party pdf generator to produce a pdf. I am attempting to use the PivotGrid's RenderControl method to capture the PivotGrid's HTML and push that HTML to the third party pdf generator.

Here is the code I'm using to attempt to create a PDF from the PivotGrid:

01.Protected Sub exportPDF_OnClick(ByVal sender As Object, ByVal e As EventArgs)
02.    Dim stringBuilder As New System.Text.StringBuilder
03.    Dim stringWriter As New System.IO.StringWriter(stringBuilder)
04.    Dim pivotGridHtml As String
05.    Dim htmlTextWriter As New HtmlTextWriter(stringWriter)
06. 
07.    pivotGrid.RenderControl(htmlTextWriter)
08.    pivotGridHtml = stringBuilder.ToString()
09.    PdfGenerator.GetDefaultPdfGenerator().HTMLToPDF(pivotGridHtml)
10.
11.End Sub


Attempts to create a pdf fail on the call to RenderControl. The expand/collapse buttons are causing me trouble. I'm getting the error below:

Control 'ctl00_FeaturedContent_pivotGrid_ctl04_ctl00_RowExpandCollapseButton_0_0' of type 'Button' must be placed inside a form tag with runat=server.

Is there a different approach I can take? Or can I make the PivotGrid not create the expand/collapse buttons?

8 Answers, 1 is accepted

Sort by
0
Aaron
Top achievements
Rank 1
answered on 28 Feb 2014, 06:51 PM
Here is the full source for the markup

<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI, Version=2014.1.225.40, Culture=neutral, PublicKeyToken=121fae78165ba3d4" %>
 
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <style>
        .rpgRowHeaderField {
            white-space: nowrap;
        }
    </style>
            <div>
                <asp:Button runat="server" ID="exportGrid" Text="Export to Excel" OnClick="exportGrid_OnClick"/>
                <asp:Button runat="server" ID="exportPDF" Text="Export to PDF" OnClick="exportPDF_OnClick"/>
            </div>
    <asp:UpdatePanel runat="server" ID="updatePanel">
        <ContentTemplate>
             
           <telerik:RadPivotGrid ID="pivotGrid" Height="600px" runat="server" OnNeedDataSource="inventoryReportPivotGrid_OnNeedDataSource" AllowSorting="True" AllowPaging="True" EnableConfigurationPanel="True"
                ShowColumnHeaderZone="False" ShowDataHeaderZone="False" ShowFilterHeaderZone="False" ShowRowHeaderZone="False" AggregatesPosition="Columns" AggregatesLevel="2" ErrorValue=""
               OnPivotGridCellExporting="inventoryReportPivotGrid_OnPivotGridCellExporting">
                <ClientSettings EnableFieldsDragDrop="True">
                    <Scrolling AllowVerticalScroll="True"></Scrolling>
                </ClientSettings>
                <TotalsSettings GrandTotalsVisibility="None" ColumnGrandTotalsPosition="None" ColumnsSubTotalsPosition="None" RowsSubTotalsPosition="Last" RowGrandTotalsPosition="Last">                   
                </TotalsSettings>
                <Fields>
                    <telerik:PivotGridRowField DataField="State"></telerik:PivotGridRowField>
                    <telerik:PivotGridRowField DataField="Inventory_Type"></telerik:PivotGridRowField>
                     
                    <telerik:PivotGridColumnField DataField="Product"></telerik:PivotGridColumnField>
                     
                    <telerik:PivotGridAggregateField DataField="UOM" Aggregate="Min" UniqueName="UOM">
                    </telerik:PivotGridAggregateField>
                    <telerik:PivotGridAggregateField DataField="Quantity"></telerik:PivotGridAggregateField>
                    <telerik:PivotGridAggregateField DataField="Sales" DataFormatString="{0:C}"></telerik:PivotGridAggregateField>
                </Fields>
            </telerik:RadPivotGrid>     
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>


and the codebeind:

001.Option Strict On
002.Option Explicit On
003. 
004.Imports System.Data
005.Imports System.Data.SqlClient
006.Imports System.Drawing
007.Imports Telerik.Web.UI
008. 
009.Partial Class _Default
010.    Inherits Page
011. 
012.    Protected Sub inventoryReportPivotGrid_OnNeedDataSource(ByVal sender As Object, ByVal e As PivotGridNeedDataSourceEventArgs)
013.        pivotGrid.DataSource = GetPivotTableData()
014.    End Sub
015. 
016.    Private Function GetPivotTableData() As DataTable
017.        Dim sqlCommand As SqlCommand
018.        Dim connectionString As String = "data source=<SERVER>;initial catalog=<DATABASE>;persist security info=True;packet size=4096;Connect Timeout=30;Max Pool Size=200;Integrated Security=SSPI;"
019.        Dim sql As XElement = <SQL>
020.                    SELECT  *
021.                    FROM    Data
022.              </SQL>
023. 
024.        sqlCommand = New SqlCommand()
025. 
026.        sqlCommand.CommandType = CommandType.Text
027.        sqlCommand.CommandText = sql.Value
028. 
029.        Return Nterline.DataAccess.ExecuteDataTable(connectionString, sqlCommand)
030.    End Function
031. 
032.    Protected Sub exportGrid_OnClick(ByVal sender As Object, ByVal e As EventArgs)
033.        pivotGrid.ExportSettings.IgnorePaging = True
034.        pivotGrid.ExportToExcel()
035. 
036.    End Sub
037. 
038.    Protected Sub inventoryReportPivotGrid_OnPivotGridCellExporting(ByVal sender As Object, ByVal e As PivotGridCellExportingArgs)
039.        Dim modelDataCell As PivotGridBaseModelCell = TryCast(e.PivotGridModelCell, PivotGridBaseModelCell)
040.        If modelDataCell IsNot Nothing Then
041.            AddStylesToDataCells(modelDataCell, e)
042.        End If
043. 
044.        If modelDataCell.TableCellType = PivotGridTableCellType.RowHeaderCell Then
045.            AddStylesToRowHeaderCells(modelDataCell, e)
046.        End If
047. 
048.        If modelDataCell.TableCellType = PivotGridTableCellType.ColumnHeaderCell Then
049.            AddStylesToColumnHeaderCells(modelDataCell, e)
050.        End If
051. 
052.        If modelDataCell.IsGrandTotalCell Then
053.            e.ExportedCell.Style.BackColor = Color.FromArgb(128, 128, 128)
054.            e.ExportedCell.Style.Font.Bold = True
055.        End If
056. 
057.        If IsTotalDataCell(modelDataCell) Then
058.            e.ExportedCell.Style.BackColor = Color.FromArgb(150, 150, 150)
059.            e.ExportedCell.Style.Font.Bold = True
060.            AddBorders(e)
061.        End If
062. 
063.        If IsGrandTotalDataCell(modelDataCell) Then
064.            e.ExportedCell.Style.BackColor = Color.FromArgb(128, 128, 128)
065.            e.ExportedCell.Style.Font.Bold = True
066.            AddBorders(e)
067.        End If
068.    End Sub
069. 
070.    Private Sub AddStylesToDataCells(modelDataCell As PivotGridBaseModelCell, e As PivotGridCellExportingArgs)
071.        If modelDataCell.Field.UniqueName = "Sales" Then
072.            e.ExportedCell.Format = "$0.0"
073.        End If
074.    End Sub
075. 
076.    Private Sub AddStylesToColumnHeaderCells(modelDataCell As PivotGridBaseModelCell, e As PivotGridCellExportingArgs)
077.        If e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 0 Then
078.            e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 20.0
079.        End If
080. 
081.        If modelDataCell.IsTotalCell Then
082.            e.ExportedCell.Style.BackColor = Color.FromArgb(150, 150, 150)
083.            e.ExportedCell.Style.Font.Bold = True
084.        Else
085.            e.ExportedCell.Style.BackColor = Color.FromArgb(192, 192, 192)
086.        End If
087.        AddBorders(e)
088.    End Sub
089. 
090.    Private Sub AddStylesToRowHeaderCells(modelDataCell As PivotGridBaseModelCell, e As PivotGridCellExportingArgs)
091.        If e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 0 Then
092.            e.ExportedCell.Table.Columns(e.ExportedCell.ColIndex).Width = 11.0
093.        End If
094.        If modelDataCell.IsTotalCell Then
095.            e.ExportedCell.Style.BackColor = Color.FromArgb(150, 150, 150)
096.            e.ExportedCell.Style.Font.Bold = True
097.        Else
098.            e.ExportedCell.Style.BackColor = Color.FromArgb(192, 192, 192)
099.        End If
100. 
101.        AddBorders(e)
102.    End Sub
103. 
104.    Private Shared Sub AddBorders(e As PivotGridCellExportingArgs)
105.        e.ExportedCell.Style.BorderBottomColor = Color.FromArgb(128, 128, 128)
106.        e.ExportedCell.Style.BorderBottomWidth = New Unit(1)
107.        e.ExportedCell.Style.BorderBottomStyle = BorderStyle.Solid
108. 
109.        e.ExportedCell.Style.BorderRightColor = Color.FromArgb(128, 128, 128)
110.        e.ExportedCell.Style.BorderRightWidth = New Unit(1)
111.        e.ExportedCell.Style.BorderRightStyle = BorderStyle.Solid
112. 
113.        e.ExportedCell.Style.BorderLeftColor = Color.FromArgb(128, 128, 128)
114.        e.ExportedCell.Style.BorderLeftWidth = New Unit(1)
115.        e.ExportedCell.Style.BorderLeftStyle = BorderStyle.Solid
116. 
117.        e.ExportedCell.Style.BorderTopColor = Color.FromArgb(128, 128, 128)
118.        e.ExportedCell.Style.BorderTopWidth = New Unit(1)
119.        e.ExportedCell.Style.BorderTopStyle = BorderStyle.Solid
120.    End Sub
121. 
122.    Private Function IsTotalDataCell(modelDataCell As PivotGridBaseModelCell) As Boolean
123.        Return modelDataCell.TableCellType = PivotGridTableCellType.DataCell AndAlso (modelDataCell.CellType = PivotGridDataCellType.ColumnTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.RowTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.RowAndColumnTotal)
124.    End Function
125. 
126.    Private Function IsGrandTotalDataCell(modelDataCell As PivotGridBaseModelCell) As Boolean
127.        Return modelDataCell.TableCellType = PivotGridTableCellType.DataCell AndAlso (modelDataCell.CellType = PivotGridDataCellType.ColumnGrandTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.ColumnGrandTotalRowTotal OrElse modelDataCell.CellType = PivotGridDataCellType.RowGrandTotalColumnTotal OrElse modelDataCell.CellType = PivotGridDataCellType.RowGrandTotalDataCell OrElse modelDataCell.CellType = PivotGridDataCellType.RowAndColumnGrandTotal)
128.    End Function
129. 
130.    Protected Sub exportPDF_OnClick(ByVal sender As Object, ByVal e As EventArgs)
131.        Dim stringBuilder As New System.Text.StringBuilder
132.        Dim stringWriter As New System.IO.StringWriter(stringBuilder)
133.        Dim pivotGridHtml As String
134.        Dim htmlTextWriter As New HtmlTextWriter(stringWriter)
135. 
136.        pivotGrid.RenderControl(htmlTextWriter)
137.        pivotGridHtml = stringBuilder.ToString()
138.        PdfGenerator.GetDefaultPdfGenerator().HTMLToPDF(pivotGridHtml)
139.    End Sub
140. 
141.End Class
0
Kostadin
Telerik team
answered on 05 Mar 2014, 11:54 AM
Hi Aaron,

Make sure you are calling the RenderControl of the PivotGrid on Render event handler. If you called it earlier you will receive a multiple exceptions. Please check out the following code snippet.
protected override void Render(HtmlTextWriter writer)
{
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    RadPivotGrid1.RenderControl(htw);
    string htmlvalue = sb.ToString();
 
    base.Render(writer);
}

Regards,
Kostadin
Telerik

DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!

0
Aaron
Top achievements
Rank 1
answered on 05 Mar 2014, 05:40 PM
Your suggestion of calling the pivot grid's RenderControl method in the Render event handler produces the same error I was seeing in the export button's click event.

Control 'ctl00_FeaturedContent_pivotGrid_ctl01_ctl01_rc_Product_Column_LinkButton_Product_Column' of type 'LinkButton' must be placed inside a form tag with runat=server.

 The difference is that now the error occurs when the page loads instead of when the "Export to PDF" button is clicked.

Also, the code snippet you provided is doing basically the same thing my exportPDF_OnClick event handler was doing except it does not appear to do anything with the html contained in the string variable, htmlvalue.
0
Kostadin
Telerik team
answered on 10 Mar 2014, 11:31 AM
Hello Aaron,

In order to overcome this exception you have to override the VerifyRenderingInServerForm method.
public override void VerifyRenderingInServerForm(Control control)
{
}


Regards,
Kostadin
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

0
Kalyani
Top achievements
Rank 1
answered on 13 Mar 2014, 09:36 PM
Did this work.. I have this in my code  

this is in my aspx file.
 <asp:Button ID= "Button1" runat ="server" OnClick="exportPDF_OnClick" Text ="PDF"/>
 
C# file
 protected void exportPDF_OnClick(object sender, EventArgs e)
 {
  StringBuilder sb = new StringBuilder();
  StringWriter sw = new StringWriter(sb);
  string pivotGridHtml;
  HtmlTextWriter txtWriter = new HtmlTextWriter(sw);  
  RadPivotGrid1.RenderControl(txtWriter);
  pivotGridHtml = sb.ToString();
  lblTest.Text = sb.ToString();  
 }

  public override void VerifyRenderingInServerForm(Control control)
 {
 }

Now I get this error - 
Script control 'RadTreeViewOLAP' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl

How do I fix this - Thanks
0
Kostadin
Telerik team
answered on 18 Mar 2014, 12:43 PM
Hello Kalyani,

Please make sure you are calling the RenderControl of the PivotGrid on Render event handler. My first reply demonstrates how to achieve that.

Regards,
Kostadin
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Kalyani
Top achievements
Rank 1
answered on 01 Apr 2014, 09:24 PM
Thank you so much.  I did change it to render event -   so the code I have is like this

public override void VerifyRenderingInServerForm(Control control)
 {
 }

 protected override void Render(HtmlTextWriter writer)
 {
  StringBuilder sb = new StringBuilder();
  StringWriter sw = new StringWriter(sb);
  HtmlTextWriter htw = new HtmlTextWriter(sw);
  RadPivotGrid1.RenderControl(htw);
  string htmlvalue = sb.ToString();
  lbltest.Text = htmlvalue;
  base.Render(writer);
 }

But then the render method is called as soon as the page is loaded.  I would like to get the same thing to work when I click a button.  How do we do that.  I am trying to use this code for getting a print preview.  I have a button and when I click it I was hoping to get a screen shot of the page (essentially whatever is in htmlValue).  So something like this -


protected void exportPDF_OnClick(object sender, EventArgs e)
 {
  StringBuilder sb = new StringBuilder();
  StringWriter sw = new StringWriter(sb);
  string pivotGridHtml;
  HtmlTextWriter txtWriter = new HtmlTextWriter(sw);  
  RadPivotGrid1.RenderControl(txtWriter);
  pivotGridHtml = sb.ToString();
  lblTest.Text = sb.ToString();  
 }


  Can I achieve it?

Thank you
0
Kostadin
Telerik team
answered on 04 Apr 2014, 08:50 AM
Hello Kalyani,

A possible solution is to use a flag to distinguish whether the export command is fired. Please check out the following code snippet.
bool isExport = false;
protected void ExportButton_Click(object sender, EventArgs e)
{
    isExport = true;
    RadPivotGrid1.ExportToExcel();
}
 
public override void VerifyRenderingInServerForm(Control control)
{
}
 
protected override void Render(HtmlTextWriter writer)
{
    if (isExport)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        RadPivotGrid1.RenderControl(htw);
        string htmlvalue = sb.ToString();
        lbltest.Text = htmlvalue;
        base.Render(writer);
    }
}

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
PivotGrid
Asked by
Aaron
Top achievements
Rank 1
Answers by
Aaron
Top achievements
Rank 1
Kostadin
Telerik team
Kalyani
Top achievements
Rank 1
Share this question
or