menu

Questions & Answers

Download pdf rendered from backend

I have project written in Angular + TS + Net 6 Web api.

My goal is to have button. On button click, pdf should be created in backend and PDF downloaded to user.

PDF is long report which has some variables. I heard that PDF can be generated from HTML. To simplify, lets say it looks like this:

<html>
   <head></head>
   <body>
      <div>My name is FirstParameter</div>
      <div>My age is SecondParameter</div>
   </body>
</html>

I believe FileStreamResult should be returned in backend. How to achieve it (prefer without 3rd party library)

.cs

[HttpGet]
[Route("DownloadPDF")]
public async Task<IActionResult> DownloadPDF(Guid userId)
{
    var user = await _context.Users.FirstAsync(p=>p.id == userId);
    string firstParameter = user.FirstName;
    string secondParameter = user.SecondName;

    // generating pdf as filestream

    return File(fs, "application/pdf", "random.pdf");
}

.html

<a (click)="download('f58c8036-915a-4c39-bbb8-352e21eae49c')">Download pdf</a>

.ts

    download(id) {

    }
Comments:
Answers(0) :