代码之家  ›  专栏  ›  技术社区  ›  MJK

Microsoft Edge关于在ASP中将HTML导出为PDF的问题。净核心

  •  0
  • MJK  · 技术社区  · 9 年前

    我正在尝试在一个 ASP。Net核心Web 项目关于这一点,我在网上没有找到多少。大多数软件包还没有为asp做好准备。净核心。浏览了几天后,我发现 this How to export HTML to PDF in ASP.NET Core

    我已经下载了这个项目

    Edge和pdf有什么问题吗?我在node上没有做太多工作。所以不确定出了什么问题。任何帮助都将不胜感激。在这里,我还添加了来自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); 
        }) 
    }; 
    

    还有包裹。NodeJSON

    { 
      "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" 
    } 
    

    @Martin Beeby发现了控制器代码的问题。以下代码在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();
        }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   MJK    9 年前

    如果将控制器代码更改为:

    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");
    }