How to download a PDF using AJAX Call?
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can download PDF documents using AJAX Call.
A file cannot be downloaded with AJAX directly. You can request a file using AJAX, but AJAX response will contain the actual file stream. Due to JavaScript limitations, you cannot save the stream on the user’s machine with a file download dialog. As a work around, you can check the status of the PDF generation and then export the document after saving in the disk using AJAX call.
Steps to download PDF using AJAX call programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.Pdf.AspNet.Mvc NuGet package as a reference to your .NET Framework application from NuGet.org.
- A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;
VB.NET
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing
- Add a following code in the index.cshtml.
@using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Get)) { <input type="submit" value="Create PDF" onclick="GeneratePDF()" /> } <div id="spinner"></div> <script type="text/javascript"> function GeneratePDF() { var spinnerDiv = document.getElementById("spinner"); spinnerDiv.innerHTML = '<i class="fa fa-circle-o-notch fa-spin" style="font-size:24px"></i >'; var refreshIntervalId = window.setInterval(function () { $.ajax({ type: "GET", url: '@Url.Action("GetStatus", "Home")', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { if (response.responseText == 'True') { document.getElementById("spinner").innerHTML = ''; window.clearInterval(refreshIntervalId); } }, error: function (response) { if (response.responseText == 'True') { document.getElementById("spinner").innerHTML = ''; window.clearInterval(refreshIntervalId); } } }); }, 1000); } </script>
- Add a new action method named GeneratePDF in HomeController.cs and include the following code snippet to download PDF using AJAX call.
C#
static bool taskCompleted = false;
public ActionResult GeneratePDF()
{
taskCompleted = false;
System.Threading.Thread.Sleep(5000);
//Create a new PdfDocument
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics graphics = page.Graphics;
//Create a solid brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
//Set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f);
//Draw the text
graphics.DrawString("Hello world!", font, brush, new PointF(20, 20));
taskCompleted = true;
//Export the document after saving
return document.ExportAsActionResult("output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);
}
//Get the status of PDF generation
public bool GetStatus()
{
return taskCompleted;
}
VB.NET
Shared taskCompleted As Boolean = False
Public Function GeneratePDF() As ActionResult
taskCompleted = False
System.Threading.Thread.Sleep(5000)
'Create a new PdfDocument
Dim document As New PdfDocument()
'Add a page to the document
Dim page As PdfPage = document.Pages.Add()
'Create Pdf graphics for the page
Dim graphics As PdfGraphics = page.Graphics
'Create a solid brush
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
'Set the font
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20.0F)
'Draw the text
graphics.DrawString("Hello world!", font, brush, New PointF(20, 20))
taskCompleted = True
'Export the document after saving
Return document.ExportAsActionResult("output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)
End Function
'Get the status of PDF generation
Public Function GetStatus() As Boolean
Return taskCompleted
End Function
A complete working sample can be downloaded from AjaxCall.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation to learn how to create a PDF file in ASP.NET MVC application.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion:
I hope you enjoyed learning about how to download a PDF using AJAX Call.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!