MJK
MJK

Reputation: 3514

Microsoft Edge issue on export HTML to PDF in ASP.NET Core

I am trying to generate pdf from Html on an ASP.Net Core Web project. I haven't found much on web regarding this. Most of the packages are not ready for asp.net core. After browsing for days, I found this How to export HTML to PDF in ASP.NET Core

I have downloaded the project

Export to pdf works perfectly on chrome but not on Edge; It just never finished the export.

Is there any issue with Edge and pdf I haven't do much work on node.js so not sure what is going wrong. Any help will be highly appreciated. Here, I also add the code from pdf.js

module.exports = function (callback, html) { 
    var jsreport = require('jsreport-core')(); 

    jsreport.init().then(function () { 
        return jsreport.render({ 
            template: { 
                content: html, 
                engine: 'jsrender', 
                recipe: 'phantom-pdf' 
            } 
        }).then(function (resp) { 
            callback(null, resp.content.toJSON().data); 
        }); 
    }).catch(function (e) { 
        callback(e, null); 
    }) 
}; 

And the package.json for nodejs

{ 
  "name": "pdf", 
  "version": "1.0.0", 
  "description": "", 
  "main": "index.js", 
  "dependencies": { 
    "jsreport-core": "^1.3.1", 
    "jsreport-phantom-pdf": "^1.4.4", 
    "jsreport-jsrender": "^1.0.2" 
  }, 
  "devDependencies": {}, 
  "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
  }, 
  "author": "", 
  "license": "ISC" 
} 

Updated

@Martin Beeby find the issue on Controller code. The following code is not working on MS Edge

public class HomeController : Controller
{
    [HttpGet]
    public async Task<IActionResult> Index([FromServices] INodeServices nodeServices)
    {
        HttpClient hc = new HttpClient();
        var htmlContent = await hc.GetStringAsync($"http://{Request.Host}/report.html");

        var result = await nodeServices.InvokeAsync<byte[]>("./pdf", htmlContent);

        HttpContext.Response.ContentType = "application/pdf";

        HttpContext.Response.Headers.Add("x-filename", "report.pdf");
        HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "x-filename");
        HttpContext.Response.Body.Write(result, 0, result.Length);
        return new ContentResult();
    }
}

Upvotes: 0

Views: 794

Answers (1)

Martin Beeby
Martin Beeby

Reputation: 4599

If you change the controller code to:

public async Task<IActionResult> Index([FromServices] INodeServices nodeServices)
{
    HttpClient hc = new HttpClient();
    var htmlContent = await hc.GetStringAsync($"http://{Request.Host}/report.html");

    var result = await nodeServices.InvokeAsync<byte[]>("./pdf", htmlContent);

    return File(result, "application/pdf", "report.pdf");
}

Then it works in Chrome and prompts to Download in Edge.

Upvotes: 1

Related Questions