Export from RadGrid to PDFViewer

1 Answer 97 Views
Grid PdfViewer
Danielle
Top achievements
Rank 1
Iron
Danielle asked on 30 Mar 2023, 04:19 PM
I'm trying to use the PDFViewer to preview a PDF of the radgrid export on my page, but I can't find how I would export the radgrid PDF to a filesteram instead of directly to teh browser.  Is this possible?
Rumen
Telerik team
commented on 30 Mar 2023, 06:22 PM

Hi Danielle,

You can use the OnGridExporting server event to show the result in a RadWindow or the PdfViewe.

You can find more information in these resources:

Danielle
Top achievements
Rank 1
Iron
commented on 04 Apr 2023, 12:44 PM

Thanks, I looked at the documentation and we're finding two issues, one is locating the file being exported from the radgrid in order to assign it to the PDFViewer.  The only event we see that document surfaced is the GridExporting.  We used the code referenced above and assigned it to the PDFViewer, however, the PDFViewer is never populated.  Further, the filestream is unable to be cancelled so it still streams to the browser, which is behavior we don't want to see.  Here is the code we are using.  I really can't see why the PDFViewer isn't displaying the document.

Rumen
Telerik team
commented on 04 Apr 2023, 03:18 PM

Hi Danielle,

Assigning the values to the PDF Viewer in the Grid's Export event will not have an effect, because the grid export functionality clears the headers. What you can do is save the file to the disk, cookie, or session, and in the Page_Load event, if the file exists or the data in the cookie/session exists, assign that info to the PdfViewer.

Danielle
Top achievements
Rank 1
Iron
commented on 07 Apr 2023, 01:41 PM

Thanks!  We tweaked the code so it saves to a session variable and then redirects to cancel the download and it worked beautifully.   Thanks for the direction!
Rumen
Telerik team
commented on 07 Apr 2023, 02:09 PM

Great! If you wish you can also share a code snippet of your code with your fellow developers. 

Keep up the good work!

1 Answer, 1 is accepted

Sort by
1
Accepted
Danielle
Top achievements
Rank 1
Iron
answered on 07 Apr 2023, 02:20 PM

Per Rumen's guidance we were able to implement this.  The solution we have requires the user to select a location from a dropdown and the click a 'Get Report' button.  The get report button click event triggrs the export of the document to the viewer using the following code:


    Protected Sub BtnGetReport_Click(sender As Object, e As EventArgs)
        If ddlLocation.SelectedValue <> "None" Then
            'Sets the session to the drop down's selected value
            Session("LocationDropDownValue") = ddlLocation.SelectedValue

            If RadGrid1.Items.Count > 1 Then
                'format PDF and trigger RadGrid's export to PDF function
                RadGrid1.ExportSettings.Pdf.BorderType = GridPdfSettings.GridPdfBorderType.AllBorders
                RadGrid1.ExportSettings.Pdf.PageHeader.MiddleCell.Text = "<h4>Header 1</h4><h2>Header 2</h2>"
                RadGrid1.ExportSettings.Pdf.PageHeader.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center
                RadGrid1.ExportSettings.Pdf.PageFooter.MiddleCell.Text = "<?page-number?>"
                RadGrid1.ExportSettings.Pdf.PageFooter.MiddleCell.TextAlign = GridPdfPageHeaderFooterCell.CellTextAlign.Center
                RadGrid1.MasterTableView.ExportToPdf()
                Session("IsPageLoad") = True
            End If
        Else
            RadPdfViewer1.Visible = False
        End If
    End Sub

 in the rad grid exporting event:

    Protected Sub RadGrid1_GridExporting(sender As Object, e As GridExportingArgs)
        If Session("IsPageLoad") Then
            'generate data for PDF viewer
            Session("ReportStream") = Encoding.Default.GetBytes(e.ExportOutput)
            Session("IsReportLoaded") = True
            Session("IsPageLoad") = False
            'stop page from redirecting and cancel PDF client-side download
            Response.Redirect(Request.RawUrl, True)
        End If
    End Sub

and on the page load event we assign the document to the viewer and clear up all the session variables


        If Not IsNothing(Session("IsReportLoaded")) Then
            RadPdfViewer1.PdfjsProcessingSettings.FileSettings.Data = Convert.ToBase64String(Session("ReportStream"))
            RadPdfViewer1.Visible = True
            Session("IsReportLoaded") = False
            Session("ReportStream") = New Byte() {}
        End If

Rumen
Telerik team
commented on 07 Apr 2023, 02:26 PM

Thank you! I updated your Telerik points for your contribution :)
Danielle
Top achievements
Rank 1
Iron
commented on 07 Apr 2023, 04:21 PM

Thank you! :)
Tags
Grid PdfViewer
Asked by
Danielle
Top achievements
Rank 1
Iron
Answers by
Danielle
Top achievements
Rank 1
Iron
Share this question
or