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

Export to PDF whithout writing to HttpResponse

1 Answer 272 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ITERGO
Top achievements
Rank 1
ITERGO asked on 13 Mar 2015, 08:41 AM
Hallo,

The class GridTableView supplies the method ExportToPdf(). This method writes the generated PDF-Data to the Page.Response. What I need is a method/solution which doesn't writes to Page.Response and returns a byte-Array or Stream which represents the PDF. Something like that:

public Byte[] ExportToPdfAsByteArray();

Does anyone know how to realize that?

BTW: I have seen the example from Telerik where a method was attached to the GridExporting-Event to extract the PDF-Data from the GridExportingArgs and write the pdf to a file. But that isn't a solution for me because it still writes to HttpResponse and it does a Response.Redirect to the actual page.

Thanks

Oliver

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 17 Mar 2015, 04:27 PM
Hello Oliver,

I will post the answer to the support ticket that you have opened regarding this question, so it could help others with the same requirement:

"With the current implementation of the exporting mechanism it is not possible to retrieve the content of the exported file and cancel the actual exporting, but you could use one of the following workaround:

1) Since it is not possible to export from RadGrid with enabled AJAX, you could enable the AJAX for your grid and handle the server-side OnGridExporting event for retrieving the file content (as a string) and manually convert it to byte array (examples on this matter are available in the following forum thread - Converting a string to byte-array without using an encoding (byte-by-byte))
:
<telerik:RadAjaxPanel runat="server" EnableAJAX="true">
    <telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnGridExporting="RadGrid1_GridExporting">
        <MasterTableView CommandItemDisplay="Top" CommandItemSettings-ShowExportToPdfButton="true">
            <Columns>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
</telerik:RadAjaxPanel>

And the code-behind:

protected void RadGrid1_GridExporting(object sender, GridExportingArgs e)
{
    var output = e.ExportOutput;
    //your custom logic
}

2) The second option is to avoid the AJAX, but redirect to the current page within the OnGridExporting event:
protected void RadGrid1_GridExporting(object sender, GridExportingArgs e)
{
    var output = e.ExportOutput;
    // your custom logic
    Response.Redirect(Request.Url.ToString());
}
"

If you have other questions on this matter, I could suggest that we continue our conversation in the support ticket.


Regards,
Konstantin Dikov
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
Grid
Asked by
ITERGO
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or