How can I help you?
Performing Server Side PDF Export in the Angular Grid
19 Mar 202619 minutes to read
The Grid component provides the ability to export grid data to a PDF document on the server side using the Grid server export library. Server-side PDF export operations provide additional security and flexibility. Enabling server-side PDF exporting requires configuring server dependencies and implementing the necessary server configuration.
Server dependencies
To enable server-side PDF exporting in the Syncfusion® Angular Grid component, include the following dependencies:
Syncfusion.EJ2Syncfusion.EJ2.GridExport
These dependencies are available in the Essential Studio® package and can also be obtained from nuget.org.
Server configuration
To export grid data to a PDF document on the server side, perform the following server configuration using an ASP.NET Core Controller Action:
-
Set up the necessary dependencies and imports in the server-side code.
-
Define a controller action that handles server-side PDF export. This action receives Grid properties from the client-side and initiates the PDF export operation on the server.
-
Use the serverPdfExport method to pass Grid properties to the server exporting action. This method specifies the server action URL and other export options.
The following code snippet shows server configuration using ASP.NET Core Controller Action.
public ActionResult PdfExport([FromForm] string gridModel)
{
GridPdfExport exp = new GridPdfExport();
Grid gridProperty = ConvertGridObject(gridModel);
return exp.PdfExport<OrdersDetails>(gridProperty, orddata);
}
private Grid ConvertGridObject(string gridProperty)
{
Grid GridModel = (Grid)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(Grid));
GridColumnModel cols = (GridColumnModel)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(GridColumnModel));
GridModel.Columns = cols.columns;
return GridModel;
}
public class GridColumnModel
{
public List<GridColumn> columns { get; set; }
}
public IActionResult UrlDatasource([FromBody]DataManagerRequest dm)
{
IEnumerable DataSource = OrdersDetails.GetAllRecords();
DataOperations operation = new DataOperations();
int count = DataSource.Cast<OrdersDetails>().Count();
return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource);
}import { Component, OnInit, ViewChild } from '@angular/core';
import { ToolbarItems, GridComponent } from '@syncfusion/ej2-angular-grids';
import { DataManager, UrlAdaptor } from '@syncfusion/ej2-data';
import { ClickEventArgs } from '@syncfusion/ej2-navigations';
@Component({
selector: 'app-root',
template: `<ejs-grid #grid id='Grid' [dataSource]='data' [toolbar]='toolbar' height='273px' (toolbarClick)='toolbarClick($event)'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipName' headerText='Ship Name' width=150></e-column>
</e-columns>
</ejs-grid>`
})
export class AppComponent implements OnInit {
public data: DataManager;
public toolbar: ToolbarItems[];
public dataManager: DataManager = new DataManager({
url: 'Home/UrlDatasource',
adaptor: new UrlAdaptor()
});
@ViewChild('grid')
public grid: GridComponent;
ngOnInit(): void {
this.data = this.dataManager;
this.toolbar = ['PdfExport'];
}
toolbarClick(args: ClickEventArgs): void {
if (args.item.id === 'Grid_pdfexport') { // 'Grid_pdfexport' -> Grid component id + _ + toolbar item name
this.grid.serverPdfExport('Home/PdfExport');
}
}
}Refer to the GitHub sample for quick implementation and testing from here.
Export grid as memory stream
The Grid provides the ability to export data as a memory stream instead of downloading it as a file in the browser. To obtain the memory stream of the exported Grid, set the AsMemoryStream parameter to true in the PdfExport method.
The following code demonstrates getting the memory stream of exported grid.
public object PdfExport(string gridModel)
{
GridPdfExport exp = new GridPdfExport();
Grid gridProperty = ConvertGridObject(gridModel);
// Pass third parameter as true to get the MemoryStream of exported grid data
return (MemoryStream)exp.PdfExport<OrdersDetails>(gridProperty, OrdersDetails.GetAllRecords(), true);
}Merge grid’s memory stream
The Essential® PDF library merges multiple memory streams into a single stream. For more information about the merge option, refer to this documentation.
A memory stream, file stream, or local file can be merged with the Grid’s memory stream in the following ways:
Merging with an existing memory stream
When an existing memory stream is available, it can be directly used to merge with the Grid’s memory stream.
In the following code, the Merge method of the PdfDocumentBase class merges the grid’s memory stream with an existing memory stream.
using Syncfusion.Pdf;
public MemoryStream ms1; // Existing memory stream
public object PdfExport(string gridModel)
{
GridPdfExport exp = new GridPdfExport();
Grid gridProperty = ConvertGridObject(gridModel);
// Memory stream of exported grid data
MemoryStream ms2 = (MemoryStream)exp.PdfExport<OrdersDetails>(gridProperty, OrdersDetails.GetAllRecords(), true);
PdfDocument finalDoc = new PdfDocument();
Stream[] streams = { ms1, ms2 }; // ms1 = existing stream, ms2 = grid's stream
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream ms3 = new MemoryStream();
finalDoc.Save(ms3);
ms3.Position = 0;
finalDoc.Close(true);
ms1.Dispose();
ms2.Dispose();
return ms3;
}Merging with an existing file stream
When an existing file stream is available, it can be directly used to merge with the Grid’s memory stream. In the following code, the existing file stream is merged with the Grid’s memory stream.
using Syncfusion.Pdf;
public FileStream fs1; // Existing file stream
public ActionResult PdfExport(string gridModel)
{
GridPdfExport exp = new GridPdfExport();
Grid gridProperty = ConvertGridObject(gridModel);
MemoryStream ms1 = (MemoryStream)exp.PdfExport<OrdersDetails>(gridProperty, OrdersDetails.GetAllRecords(), true);
PdfDocument finalDoc = new PdfDocument();
Stream[] streams = { fs1, ms1 }; // fs1 = file stream, ms1 = grid's stream
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream ms3 = new MemoryStream();
finalDoc.Save(ms3);
ms3.Position = 0;
return ms3;
}Merging with a local file
To merge a local file with the Grid’s memory stream, convert it into a file stream before merging. In the following code, the existing local file is merged with the Grid’s memory stream.
using Syncfusion.Pdf;
// Convert local file to a file stream
public FileStream fs1 = new FileStream("D:/PdfDoc.pdf", FileMode.Open, FileAccess.Read);
public ActionResult PdfExport(string gridModel)
{
GridPdfExport exp = new GridPdfExport();
Grid gridProperty = ConvertGridObject(gridModel);
MemoryStream ms1 = (MemoryStream)exp.PdfExport<OrdersDetails>(gridProperty, OrdersDetails.GetAllRecords(), true);
PdfDocument finalDoc = new PdfDocument();
Stream[] streams = { fs1, ms1 }; // fs1 = local file stream, ms1 = grid's stream
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream ms3 = new MemoryStream();
finalDoc.Save(ms3);
ms3.Position = 0;
return ms3;
}Downloading the merged memory stream
The merged memory stream can be downloaded by converting it into a FileStreamResult. In the following code, the merged memory stream is downloaded to the browser.
using Syncfusion.Pdf;
public ActionResult PdfExport(string gridModel)
{
PdfDocument finalDoc = new PdfDocument();
// ms1 and ms2 are the streams to be merged
Stream[] streams = { ms1, ms2 };
PdfDocumentBase.Merge(finalDoc, streams);
MemoryStream ms3 = new MemoryStream();
finalDoc.Save(ms3);
ms3.Position = 0;
FileStreamResult fileStreamResult = new FileStreamResult(ms3, "application/pdf");
fileStreamResult.FileDownloadName = "Export.pdf";
finalDoc.Close(true);
ms1.Dispose();
ms2.Dispose();
return fileStreamResult;
}Rotating header text in the exported grid
The Grid provides the ability to rotate the header text while exporting the grid on the server side.
-
The server-side event PdfHeaderQueryCellInfo is triggered when creating a column header for the PDF document to be exported. In this event, column header details can be collected and customizations can be handled.
-
In the
BeginCellLayoutevent handler, theGraphics.DrawStringmethod can be used to rotate the header text to the desired degree. This event is triggered when creating a column header for the PDF document to be exported, and column header details are collected in this event with custom handling done in theBeginCellLayoutevent handler.
In the following demo, the DrawString method from the Graphics is used to rotate the header text of the column header inside the BeginCellLayout event handler.
Rotating column headers is not supported in client side PDF exporting.
public ActionResult PdfExport(string gridModel)
{
GridPdfExport exp = new GridPdfExport();
Grid gridProperty = ConvertGridObject(gridModel);
gridProperty.ServerPdfHeaderQueryCellInfo = PdfHeaderQueryCellInfo;
PdfGrid grid = new PdfGrid();
PdfExportProperties pdfExportProperties = new PdfExportProperties();
pdfExportProperties.IsRepeatHeader = true;
pdfExportProperties.BeginCellLayout = new PdfGridBeginCellLayoutEventHandler(BeginCellEvent);
gridProperty.ServerPdfQueryCellInfo = PdfQueryCellInfo;
IEnumerable data = Utils.DataTableToJson(dt);
var result = exp.PdfExport<dynamic>(gridProperty, data, pdfExportProperties);
return result;
}
public void BeginCellEvent(object sender, PdfGridBeginCellLayoutEventArgs args)
{
PdfGrid grid = (PdfGrid)sender;
var brush = new PdfSolidBrush(new PdfColor(Color.DimGray));
args.Graphics.Save();
args.Graphics.TranslateTransform(args.Bounds.X + 50, args.Bounds.Height + 40); // User-defined X, Y positioning
args.Graphics.RotateTransform(-60); // User-defined rotation degree
args.Graphics.DrawString(headerValues[args.CellIndex], new PdfStandardFont(PdfFontFamily.Helvetica, 10), brush, new PointF(0, 0));
if (args.IsHeaderRow)
{
grid.Headers[0].Cells[args.CellIndex].Value = string.Empty;
}
args.Graphics.Restore();
}
private void PdfHeaderQueryCellInfo(object pdf)
{
ServerPdfHeaderQueryCellInfoEventArgs name = (ServerPdfHeaderQueryCellInfoEventArgs)pdf;
PdfGrid grid = new PdfGrid();
headerValues.Add(name.Column.HeaderText);
var longestString = headerValues.Where(s => s.Length == headerValues.Max(m => m.Length)).First();
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 6);
SizeF size = font.MeasureString(longestString);
name.Headers[0].Height = size.Width * 2;
}Passing additional parameters to the server during export
Passing additional parameters to the server when exporting data in the Syncfusion® Angular Grid provides flexibility to include extra information or customize the export process based on specific requirements.
This is achieved by utilizing the query property and the toolbarClick event. Within the query property, invoke the addParams method to add parameters to the request.
The following example demonstrates passing additional parameters to the server when PDF exporting within the toolbarClick event. Within the event, the additional parameters, specifically “recordcount” as “15”, are passed using the addParams method and displayed as a message.
import { data } from './datasource';
import { Component, OnInit, ViewChild } from '@angular/core';
import { GridComponent, GridModule, PageService, PdfExportService, ToolbarItems, ToolbarService } from '@syncfusion/ej2-angular-grids';
import { ClickEventArgs } from '@syncfusion/ej2-angular-navigations';
import { Query } from '@syncfusion/ej2-data';
@Component({
imports: [GridModule],
providers: [PdfExportService, ToolbarService, PageService],
standalone: true,
selector: 'app-root',
template: `
<div style="margin-left:180px"><p style="color:red;" id="message"></p></div>
<ejs-grid #grid id='Grid' [dataSource]='data' [allowPaging]='true'
[toolbar]='toolbarOptions' height='272px' [allowPdfExport]='true'
(pdfExportComplete)='pdfExportComplete()' (toolbarClick)='toolbarClick($event)'>
<e-columns>
<e-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-column>
<e-column field='CustomerID' headerText='Customer ID' [visible]='false' width=150></e-column>
<e-column field='ShipCity' headerText='Ship City' width=150></e-column>
<e-column field='ShipCountry' headerText='ShipCountry' width=150></e-column>
</e-columns>
</ejs-grid>`,
})
export class AppComponent implements OnInit {
public data?: object[];
public toolbarOptions?: ToolbarItems[];
@ViewChild('grid') public grid?: GridComponent;
public queryClone?: Query;
public message?: string;
ngOnInit(): void {
this.data = data;
this.toolbarOptions = ['PdfExport'];
}
toolbarClick(args: ClickEventArgs): void {
if (args.item.id === 'Grid_pdfexport') {
this.queryClone = (this.grid as GridComponent).query;
(this.grid as GridComponent).query = new Query().addParams('recordcount', '15');
this.message =
'Key: ' +
(this.grid as GridComponent).query.params[0].key +
' and Value: ' +
(this.grid as GridComponent).query.params[0].value + ' on ' + args.item.text;
(this.grid as GridComponent).pdfExport();
}
}
pdfExportComplete(): void {
(this.grid as GridComponent).query = this.queryClone as Query;
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Limitations
- The export feature for detail and caption templates is not supported in server-side exporting.
- Multiple grids exporting feature is not supported with server side exporting.