New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

PdfExporting

The client-side PdfExporting event is raised when RadClientExportManager is about to export a PDF document. You can handle this event in case you want to prevent the export. This article shows how to use this event in order to send the file content to the server, save it there and cancel the further execution of the export (See Example 1). For your reference you can find the API controller implementation as well (See Example 2).

To handle this event, simply write a JavaScript function that can be called when the event occurs. Then assign the name of this function as the value of the the OnClientPdfExporting property.

The client-side PdfExporting event handler receives two arguments:

  1. Sender—the RadClientExportManager object that fired the event.

  2. Event arguments — a ClientExportManagerPdfExportingEventArgs object that exposes the following methods:

ClientExportManagerPdfExportingEventArgs Object

NameParametersReturn TypeDescription
get_cancel()boolGets a value that indicates whether the event is canceled.
get_dataURI()StringReturns the PDF document dataURI.
set_cancel(value)boolSets whether the event will be canceled (if true is passed).

Example 1: Send the PDF to the server and prevent the client-side export.

ASPNET
<telerik:RadClientExportManager runat="server" ID="RadClientExportManager1" OnClientPdfExporting="OnClientPdfExporting">
	<PdfSettings FileName="MyFile.pdf" />
</telerik:RadClientExportManager>

<input type="button" onclick="exportElement()" value="export" />
JavaScript
var $ = $telerik.$;

function OnClientPdfExporting(sender, args) {
	var dataRaw = args.get_dataURI().split(',');

	var data = { contentType: dataRaw[0].split(';')[0].split(':')[1], fileName: "test 1.pdf", base64: dataRaw[1] };

	$.ajax({
		type: "POST",
		data: data,
		url: "api/export/file",
		success: success
	});

	args.set_cancel(true);
}

function success() {
	$find("FileExplorer1").refresh();
}

function exportElement() {
	var exp = $find("<%= RadClientExportManager1.ClientID %>");

	exp.exportPDF($telerik.$("#foo"));
}

Example 2: Save a PDF sent from the client to a folder on the server.

C#
public class ExportController : ApiController
{
	[HttpPost]
	public HttpResponseMessage File(FormDataCollection values)
	{
		var contentType = values.Get("contentType");
		var base64 = values.Get("base64");
		var fileName = values.Get("fileName");

		var fileContents = Convert.FromBase64String(base64);


		System.IO.File.WriteAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/files") + "\\" + Guid.NewGuid() + "_" + fileName, Convert.FromBase64String(base64));

		var response = new HttpResponseMessage();
		response.StatusCode = HttpStatusCode.OK;

		return response;
	}
}

See Also

In this article
See Also
Not finding the help you need?
Contact Support