0

I am using c# to create a pdf file and then the file is sent via response. The file seems to be created totally fine because the swagger shows the download link. Below is the controller code where a pdf file is created and then returned:

[HttpGet("generatepdf")]
    public async Task<IActionResult> GeneratePDF(string AccountId, [FromQuery] InvoiceRequestDto model)
    {
        var document = new PdfDocument();
        var rv = await service.GetAccountInvoices(model);

        string htmloutput = string.Empty;
        htmloutput = "<table>";

        htmloutput += "<thead><tr><th>Invoice Id</th><th>Invoice Date</th><th>Invoice Due Date</th><th>Fee Type</th><th>Fee</th><th>Amount Outstanding</th><th>Status</th></tr></thead>";
        htmloutput += "<tbody>";
        foreach (var item in rv.Items)
        {
            htmloutput += "<tr><td>" + item.Id + "</td><td>" + item.Date + "</td><td>" + item.PaymentDueDate + "</td><td>" + item.AmountOutstanding + "</td><td>" + item.StatusName + "</td>";
        }
        htmloutput += "</tbody></table>";

        string HtmlContent = htmloutput;
        PdfGenerator.AddPdfPages(document, HtmlContent, PageSize.A4);
        byte[]? response = null;
        using (MemoryStream ms = new MemoryStream())
        {
            document.Save(ms);
            response = ms.ToArray();
        }
        string Filename = "Invoice.pdf";
        return File(response, "application/pdf", Filename);
    } 

And here is the service code at the angular end:

async getPdfList(params: any) {
    return await this.ws.get(`${this.billingApiUrl}/invoice/generatepdf`,params).toPromise();
  }

However, I am unable to download the pdf file from the angular js. It is throwing an error as follow:

enter image description here

And here is the response that I get in the network:

enter image description here

Any help would be more than appreciated.

4
  • stackoverflow.com/questions/42509737/… Commented Jan 27, 2023 at 4:43
  • @Chetan Thank you for your reply. However, in the above link, the data is being sent with the request. However, I am creating the file in the controller itself. I would share my latest code if you would be interested to look. Commented Jan 30, 2023 at 8:01
  • You can force browser to download this file if you have file content in your response. please read the answer with this line in the above link.. Commented Jan 30, 2023 at 9:02
  • @Chetan Yah I checked that and tried the same approach. However, getting the error as ERROR () => this.unwrapHttpValue(error) core.mjs:6485 The response shows the pdf file contents in the network as shown above. Commented Feb 1, 2023 at 11:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.