Skip to content

An example of printing a PDF using a .NET Core port of PDFSharp and using Chrome

License

Notifications You must be signed in to change notification settings

TomasHubelbauer/asp-pdf

Repository files navigation

ASP .NET Core PDF

Generating a PDF in ASP .NET Core.

Options

Paid & Bad

https://dotnetcoretutorials.com/2019/07/02/creating-a-pdf-in-net-core

Good

PDFSharpCore

https://www.nuget.org/packages/PdfSharpCore

public IActionResult Index()
{
  var document = new PdfDocument();
  var page = document.AddPage();
  var graphics = XGraphics.FromPdfPage(page);
  var font = new XFont("OpenSans", 20, XFontStyle.Bold);

  graphics.DrawString("Hello World!", font, XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.Center);

  var memoryStream = new MemoryStream();
  document.Save(memoryStream);

  // Preview:
  return new FileStreamResult(memoryStream, new MediaTypeHeaderValue("application/pdf"));

  // Download:
  return new FileStreamResult(memoryStream, new MediaTypeHeaderValue("application/pdf")) { FileDownloadName = "download.pdf" };
}

dotnet watch run

See the Index Home controller action for a full example. Headers and footers seem to need to be done on per-page basis.

Chromium

https://www.nuget.org/packages/GoogleChrome

chrome --headless --print-to-pdf="render.pdf" file://

See the Privacy Home controller action for a full example. Headers and footers seem to need to be done on per-page basis using CSS.

About

An example of printing a PDF using a .NET Core port of PDFSharp and using Chrome

Topics

Resources

License

Stars

Watchers

Forks