# Grid PDF Export

You can export the Grid to PDF with the click of a button. The current filter, sort, page, grouping, column order, and column size are applied to the exported PDF document. When you click the Export button, your browser will receive the resulting file.

The Grid exports its raw data to PDF and does not export the full HTML. This behavior is different from the PDF exporting in Kendo UI for jQuery. [To export to PDF as HTML, you can use a custom approach](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#custom-export).

> tip Make sure to get familiar with all the [general export documentation first](https://www.telerik.com/blazor-ui/documentation/components/grid/export/overview).

#### In This Article

* [Basics](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#basics)
* [Limitations](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#limitations)
* [Programmatic Export](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#programmatic-export)
* [Customization](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#customization)
* [Custom Export](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#custom-export)

## Basics

To enable the PDF export in the Grid:

1. [Add the Export Tool](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#add-the-export-tool)
2. [Configure the Export Settings](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#configure-the-export-settings)
3. [Set the Columns Width in Pixels](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#set-the-columns-width-in-pixels)

### Add the Export Tool

Add the `GridToolBarPdfExportTool` inside the [`<GridToolBar>`](https://www.telerik.com/blazor-ui/documentation/components/grid/toolbar#command-tools)

```RAZOR.skip-repl
<GridToolBar>        
    <GridToolBarPdfExportTool>
        Export to PDF
    </GridToolBarPdfExportTool>
</GridToolBar>
```

If you have a custom Toolbar, add a command button with the `PdfExport` command name inside a [templated Grid Toolbar](https://www.telerik.com/blazor-ui/documentation/components/grid/toolbar#custom-toolbar-configuration)(`<GridToolBarTemplate>`):

### Configure the Export Settings

To configure the PDF export settings, add the `GridPdfExport` tag under the `GridExport` tag. You may set the following options:

<style>
    .article table {
        table-layout: auto;
        word-break: normal;
    }
</style>

| Parameter         | Type and Default Value                                 | Description                                                                       |
| ----------------- | ------------------------------------------------------ | --------------------------------------------------------------------------------- |
| `FileName`        | `string`                                               | The name of the file. The Grid will add the `.pdf` extension for you.             |
| `AllPages`        | `bool`                                                 | Whether to export the current page only, or the entire data from the data source. |
| `PaperSize`       | `GridPdfExportPaperSize` enum <br/> (`A4`)             | The size of the paper for the exported file.                                      |
| `PageOrientation` | `GridPdfExportPageOrientation` enum <br/> (`Portrait`) | The orientation of the page—portrait or landscape.                                |

Provide appropriate `PaperSize` and `PageOrientation` properties. For example, if you want to render 20 columns (100px each) in an A4 sheet, then this will yield unexpected results. The column dimensions in a PDF file are fixed, thus they cannot be resized as in Excel, which requires the developer to ensure proper export dimensions.

For further customizations, use the `GridPdfExport` tag to subscribe to the [Grid export events](https://www.telerik.com/blazor-ui/documentation/components/grid/export/events).

### Set the Columns Width in Pixels

Provide pixel widths for all columns - PDF export requires pixel widths for all columns. Widths in other units such as `%` or `em` cannot be translated correctly and the respective columns will collapse in the exported PDF file.

The column widths for the PDF export can differ from the ones in the Grid configuration for the web browser. To set column widths for the PDF file only, use the `Width` property of the [`OnBeforeExportEventArgs.Columns`](https://www.telerik.com/blazor-ui/documentation/components/grid/export/events#for-pdf-export) members.

> caption Export the Grid to PDF

<demo metaUrl="client/grid/export-pdf/" height="550"></demo>

## Limitations

The PDF export has the following limitations:

* For performance reasons, the PDF export mechanism draws each cell value on a single line. Any content that does not fit in the available space will be clipped. Text wrapping and PDF column resizing is not supported.
* Some PDF fonts do not include Cyrillic or other non-Latin characters. In such cases, [load a compatible font explicitly](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-load-cyrillic-fonts-in-pdf-export).

## Programmatic Export

You can programmatically invoke the export feature of the Grid, by using the following methods exposed on the `@ref` of the Grid:

<style>
    .article table {
        table-layout: auto;
        word-break: normal;
    }
</style>

| Method               | Type                 | Description                                                                                                                                                                                                            |
| -------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ExportToPdfAsync`   | `Task<MemoryStream>` | Returns the exported data as a `MemoryStream`. The stream itself is finalized, so that the resource does not leak. To read and work with the stream, clone its available binary data to a new `MemoryStream` instance. |
| `SaveAsPdfFileAsync` | `ValueTask`          | Sends the exported PDF file to the browser for download.                                                                                                                                                               |

> caption Invoke the PDF export function from code

<demo metaUrl="client/grid/export-pdf-programmatic/" height="600"></demo>

## Customization

To customize the exported file, handle the `OnBeforeExport` or `OnAfterExport` events the Grid exposes.

The component allows you to control the data set that will be exported. It also provides built-in customization options for the columns such as `Width`, `Title`, and more. The [column widths in the exported PDF file must be large enough, so that the cell content fits](https://www.telerik.com/blazor-ui/documentation/components/grid/export/pdf#limitations). Text wrapping is not supported.

For more advanced customization the Grid lets you get the `MemoryStream` of the file. Thus, you can customize it using the [`PdfProcessing`](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/overview) library that is available with your license.

Read more about how to [customize the exported file](https://www.telerik.com/blazor-ui/documentation/components/grid/export/events).

## Custom Export

If you want to have full control over the PDF export, you can perform it with a custom approach.

The following sample projects show two ways to implement a PDF export

* <a href="https://github.com/telerik/blazor-ui/tree/master/grid/pdf-export-server" target="_blank">Export Grid to PDF on the Server</a> - This export is achieved through the [Telerik Document Processing tools](https://www.telerik.com/blazor-ui/documentation/integrations/document-processing-libraries) that come with your Blazor license. The example shows how to get the `DataSourceRequest` from the Grid and send it to the server service for processing. Thus, you can fetch the same data that the Grid has (including paging, filtering, sorting) so you can generate the PDF. For WebAssembly apps, this improves the performance by not generating the file in the browser, which is, at the time of writing, too slow for a real-life scenario.

* <a href="https://github.com/telerik/blazor-ui/tree/master/common/pdf-jpg-export-js" target="_blank">PDF and JPG Export in the Browser with JS</a> - This sample uses Kendo JS libraries to generate the PDF file from the current DOM in the browser.

## See Also

* [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview)
* [Live Demo: Grid Export](https://demos.telerik.com/blazor-ui/grid/export)
* [Sowing a Loader While Exporting the Grid](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-show-loader-while-exporting)
* [Loading Cyrillic Fonts When Exporting the Grid to PDF](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-load-cyrillic-fonts-in-pdf-export)
