Hi, Im trying to use PDFSharp with Asp.Net Core 2.1. It working great, creating the pdf and save it to the path.
But it didn't show anything in the browser that the file has been downloaded to the browser default download path.
Here is my code:
Code:
namespace CreatePDF.Controllers
{
[Route("api/[controller]")]
public class CreatePDFController : Controller
{
// GET: api/<controller>
[HttpGet]
public IActionResult CreatePDF()
{
PDFGenerator.GeneratePDF();
return Ok("Successfully generate pdf.");
}
}
}
Code:
public class PDFGenerator
{
public static string GeneratePDF()
{
GlobalFontSettings.FontResolver = new FontResolver();
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("OpenSans", 20, XFontStyle.Bold);
gfx.DrawString("Hello World!", font, XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.Center);
document.Save("D:\\test.pdf");
return "pdf generated";
}
}
So, when user click button. It will generate the pdf and download it through browser like below. How do I achieve this using PDFSharp and .Net Core?