2017-02-26 41 views
0

我使用jsreportphantom pdf recipeASP.NET Core中呈现视图为pdf。该项目作为App ServiceAzure中运行。这在我的本地机器上正常工作,但是当我发布到Azure时,出现以下错误消息。ASP.NET Core上的Azure上的Phantom pdf

Exception: Call to Node module failed with error: ReferenceError: e is not defined at module.exports (D:\home\site\wwwroot\serviceCallDetailsPdf.js:18:26)

这里是我的js文件,是在我的项目(同一目录级别wwwroot文件,视图,控制器等)的根。

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

    jsreport.init().then(function() { 
     return jsreport.render({ 
      template: { 
       content: html, 
       engine: 'jsrender', 
       recipe: 'phantom-pdf', 
       phantom: { 
        orientation: "landscape", 
        allowLocalFilesAccess: true, 
        footer: htmlFooter 
       } 
      } 
     }).then(function (resp) { 
      callback(/* error */ null, resp.content.toString()); 
     }); 
    }).catch(function (e) { 
     callback(/* error */ e, null); 
    }); 

    callback(/* error */ e, null); 
}; 

注意:最后一次回调通常不会在那里。如果我删除它,我得到的错误会在60000毫秒后超时Node.js模块,当然它需要60秒才能显示。

我不得不更新我的project.json文件,包括publishOptions*.jsnode_modules否则我会得到一个错误说Node.js不能罚我打电话的文件。我知道这不是正确的方法(应该使用Gulp来复制我需要的东西,但我只是想先让它工作)。

我已将services.AddNodeServices();增加到ConfigureServices以及"Microsoft.AspNetCore.NodeServices": "1.1.0"增加到project.json

这里是生成自动,我package.json文件:

{ 
    "name": "projectname", 
    "version": "1.0.0", 
    "description": "", 
    "main": "serviceCallDetailsPdf.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "author": "", 
    "license": "ISC", 
    "dependencies": { 
    "jsreport-core":"1.1.1", 
    "jsreport-jsrender": "1.0.1", 
    "jsreport-phantom-pdf": "1.3.1" 
    } 
} 

最后,这里是我的Action

[HttpGet] 
public async Task<IActionResult> ServiceCallDetailsToPdf(int id) 
{ 
    var call = _repository.PullCallDetails(id, User.Identity.RegionId(), User.Identity.TimeZoneOffsetId()); 

    // Render view as string 
    var html = await _viewRenderService.RenderToStringAsync("Render/ServiceCallDetailsPdf", call); 
    html = html.Replace("<style></style>", @"<style>@font-face{font-family:'Open Sans';src:url({#asset OpenSans-Regular.ttf @encoding=dataURI});format('ttf');}*{font-family:'Open Sans';}h2{font-weight:300;line-height:1.1;}</style>"); 

    var htmlFooter = "<style>*{font-family:'Open Sans';text-align:center;}</style>" + "Page {#pageNum} of {#numPages}"; 

    // Invoke node job to create the pdf 
    var result = await _nodeServices.InvokeAsync<byte[]>("./serviceCallDetailsPdf", html, htmlFooter); 

    // Content disposition 'inline' will return the pdf to the browser and not download it 
    var contentDisposition = new ContentDispositionHeaderValue("inline"); 
    // Add file name to content disposition so if it is downloaded, it uses the correct file name 
    contentDisposition.SetHttpFileName("dispatch" + id.ToString() + ".pdf"); 
    Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString(); 

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

是否有可能调试javascript文件被调用?我花了几天和几个小时试图找出为什么这不起作用。任何帮助将不胜感激。


更新

我相信我发现这些链接我的答案:

https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox

PhantomJS as web job in Azure

这基本上意味着你不能在使用幻象PDF配方Azure应用服务。其他人可以确认吗?如果是这种情况,是否有人知道可以在Azure应用服务中工作的html to pdf渲染器?

+0

你已经通过链接到的其他问题找到了答案(基本上这是该问题的重复)。您不能在Web应用程序中使用ActiveX/GDI /等。 –

回答

1

phantomjs和其他pdf渲染在默认Windows Server上运行的Azure App服务中不起作用。但是,您现在可以使用在jsreport很好地运行的Linux上运行的Azure App服务。

你只需要创建新的应用服务时,切换到Web应用程序在Linux上,并使用节点基于容器。然后,您可以使用Docker集线器将带有jsreport的图像下载到Web应用程序中。最简单的是使用官方的jsreport图像,但也很容易使用你自己的。

链接
Tutorial how run jsreport on Azure Web App on linux
jsreport official docker image

我知道我没有回答如何真正在同一个应用程序的asp.net核心运行它。我目前不知道该怎么做,但我建议你在不同的应用程序中运行asp.net应用程序和jsreport。您可以通过REST轻松调用jsreport渲染。这也可以改善您的架构设计。