Create PDF file in ASP.NET Core

GemBox.Pdf provides support for .NET Standard 2.0 which is why it can be used on various .NET platforms such as .NET Framework, .NET Core, Xamarin, and Universal Windows Platform (UWP).

Save to PDF in .NET Core

To create a PDF file with an application that targets .NET Core 2.0 or above on Linux, macOS, or Windows using GemBox.Pdf, simply add the following package references in the project file.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Pdf" Version="*" />
  </ItemGroup>

</Project>

GemBox.Pdf is ideal for web applications (like ASP.NET Core, ASP.NET MVC, and ASP.NET Web Forms) because of its fast performance and thread safety when working with multiple PdfDocument objects.

The following example shows how you can create an ASP.NET Core application that generates a simple PDF document with two blank pages and downloads it to the client's browser.

Generated PDF file from ASP.NET Core demo application
Screenshot of created PDF file from ASP.NET Core
<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Create PDF file from ASP.NET Core</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <style>
        html { font-size: 14px; position: relative; min-height: 100%; }
        @@media (min-width: 768px) { html { font-size: 16px; } }
        body { margin-bottom: 60px; }
        a { color: #0366d6; }
        a.navbar-brand { white-space: normal; text-align: center; word-break: break-all; }
        .btn-primary { color: #fff; background-color: #1b6ec2; border-color: #1861ac; }
        .nav-pills .nav-link.active, .nav-pills .show > .nav-link { color: #fff; background-color: #1b6ec2; border-color: #1861ac; }
        .border-top { border-top: 1px solid #e5e5e5; }
        .border-bottom { border-bottom: 1px solid #e5e5e5; }
        .box-shadow { box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05); }
        .footer { position: absolute; bottom: 0; width: 100%; white-space: nowrap; line-height: 60px; }
    </style>
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>

    <div class="container">
        <main role="main" class="pb-3 text-center">
            <h1 class="display-4 mb-4">PDF generator in ASP.NET Core</h1>
            <form method="post">
                <button class="btn btn-primary" asp-controller="Home" asp-action="Download">Download PDF</button>
            </form>
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; GemBox d.o.o. — All rights reserved.
        </div>
    </footer>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
using System.IO;
using Microsoft.AspNetCore.Mvc;
using GemBox.Pdf;

namespace PdfCore.Controllers
{
    public class HomeController : Controller
    {
        static HomeController()
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        }

        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Download()
        {
            // Create new PDF document.
            using (var document = new PdfDocument())
            {
                // Add a first empty page.
                document.Pages.Add();

                // Add a second empty page.
                document.Pages.Add();

                using (var stream = new MemoryStream())
                {
                    // Save PDF to stream.
                    document.Save(stream);

                    // Download PDF to client's browser.
                    return File(stream.ToArray(), "application/pdf", "Hello World.pdf");
                }
            }
        }
    }
}

GemBox.Pdf is only licensed per developer and the licenses include a royalty-free deployment. There are no server or OEM licenses, there are no additional costs for anything (like building, testing and deploying).

So, you're free to build an unlimited number of applications and deploy or distribute them to an unlimited number of servers or end user machines with no extra cost.

Print and Save to XPS or image in .NET Core

The .NET Standard version of GemBox.Pdf has full functionality of .NET Framework version, but with few rendering limitations:

However, GemBox.Pdf provides a version for .NET Core 3.0 that has no rendering limitations. This version includes support for printing and saving to XPS or image formats, when used in .NET Core 3.0 or above application running on Windows.

But when used on a non-Windows platform then the same .NET Standard limitations are applied. For more information see Linux / macOS example.

To create an XPS or image file from ASP.NET Core 3.0 web application using GemBox.Pdf, you'll need to add Microsoft.WindowsDesktop.App framework reference in the project file.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Pdf" Version="*" />
  </ItemGroup>

</Project>

To create an XPS or image file from .NET Core 3.0 desktop application using GemBox.Pdf, you'll need to use Microsoft.NET.Sdk.WindowsDesktop SDK and add UseWPF property in the project file.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="GemBox.Pdf" Version="*" />
  </ItemGroup>

</Project>

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.