I am using jsreport
and the phantom pdf recipe
to render a View as a pdf in ASP.NET Core
. The project is running in Azure
as an App Service
. This works fine on my local machine, but when I publish to Azure
, I get the below error message.
我正在使用jsreport和幻影pdf配方在ASP.NET Core中将视图呈现为pdf。该项目在Azure中作为App Service运行。这在我的本地计算机上工作正常,但是当我发布到Azure时,我收到以下错误消息。
Exception: Call to Node module failed with error: ReferenceError: e is not defined at module.exports (D:\home\site\wwwroot\serviceCallDetailsPdf.js:18:26)
例外:调用节点模块失败并显示错误:ReferenceError:e未在module.exports中定义(D:\ home \ site \ wwwroot \ serviceCallDetailsPdf.js:18:26)
Here is my js
file that is in the root of my project (same directory level as wwwroot, Views, Controllers, etc).
这是我的js文件,它位于我的项目的根目录中(与wwwroot,Views,Controllers等相同的目录级别)。
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);
};
Note: The very last call back usually would not be there. If I remove it, the error I get changes to the Node.js module timing out after 60000 milliseconds and of course it takes 60 seconds for it to display.
注意:最后一次回拨通常不会出现。如果我删除它,我得到的错误更改为Node.js模块在60000毫秒后超时,当然它需要60秒才能显示。
I had to update my project.json
file to include *.js
and node_modules
in publishOptions
otherwise I would get an error saying Node.js
could not fine the file I am calling. I know this is not the proper way to do this (should use Gulp to copy only what I need, but I just want to get it working first).
我不得不更新我的project.json文件,在publishOptions中包含* .js和node_modules,否则我会收到一条错误,说Node.js无法处理我正在调用的文件。我知道这不是正确的方法(应该使用Gulp只复制我需要的东西,但我只想让它先工作)。
I've added services.AddNodeServices();
to ConfigureServices
and also "Microsoft.AspNetCore.NodeServices": "1.1.0"
to project.json
.
我添加了services.AddNodeServices(); to ConfigureServices和“Microsoft.AspNetCore.NodeServices”:“1.1.0”到project.json。
Here is my package.json
file that was auto-generated:
这是我自动生成的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"
}
}
Finally, here is my 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");
}
Is it possible to debug the javascript
file that is being called? I've spent a few days and numerous hours trying to figure out why this won't work. Any help would be greatly appreciated.
是否可以调试正在调用的javascript文件?我花了几天时间试图弄清楚为什么这不起作用。任何帮助将不胜感激。
Update
更新
I believe I found my answer from these links:
我相信我从这些链接中找到了答案:
https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox
https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox
PhantomJS作为Azure中的Web作业
This basically means you cannot use the phantom pdf recipe in an Azure App Service. Can anyone else confirm? If this is the case, does anyone know of a html to pdf renderer that will work in an Azure App Service?
这基本上意味着您无法在Azure应用服务中使用幻像pdf配方。其他人可以确认吗?如果是这种情况,是否有人知道将在Azure应用服务中工作的HTML到pdf渲染器?
1 个解决方案
#1
1
phantomjs and other pdf rendering won't work in Azure App service running on default Windows Server. However you can now use Azure App service running on linux where jsreport nicely works.
phantomjs和其他pdf呈现在默认Windows Server上运行的Azure App服务中不起作用。但是,您现在可以使用在Linux上运行的Azure应用服务,其中jsreport非常适用。
You just need to switch to Web App on Linux when creating new App Service and use node based container. Then you can use docker hub to download an image with jsreport right into the Web App. The simplest is to use official jsreport image but it is easy to use also your own.
在创建新的App Service并使用基于节点的容器时,您只需要在Linux上切换到Web App。然后,您可以使用docker hub将带有jsreport的图像下载到Web App中。最简单的是使用官方的jsreport图像,但它也很容易使用你自己的。
Links
Tutorial how run jsreport on Azure Web App on linux
jsreport official docker image
链接教程如何在linux jsreport官方docker镜像上的Azure Web App上运行jsreport
I'm aware I didn't answer how to actually run it in the same app as asp.net core. I don't know how to do that currently, but I suggest you to run asp.net application and jsreport in separate apps. You can easily invoke jsreport rendering through REST. This could also improve your architecture design.
我知道我没有回答如何在与asp.net核心相同的应用程序中实际运行它。我目前不知道该怎么做,但我建议你在不同的应用程序中运行asp.net应用程序和jsreport。您可以通过REST轻松调用jsreport呈现。这也可以改善您的架构设计。
#1
1
phantomjs and other pdf rendering won't work in Azure App service running on default Windows Server. However you can now use Azure App service running on linux where jsreport nicely works.
phantomjs和其他pdf呈现在默认Windows Server上运行的Azure App服务中不起作用。但是,您现在可以使用在Linux上运行的Azure应用服务,其中jsreport非常适用。
You just need to switch to Web App on Linux when creating new App Service and use node based container. Then you can use docker hub to download an image with jsreport right into the Web App. The simplest is to use official jsreport image but it is easy to use also your own.
在创建新的App Service并使用基于节点的容器时,您只需要在Linux上切换到Web App。然后,您可以使用docker hub将带有jsreport的图像下载到Web App中。最简单的是使用官方的jsreport图像,但它也很容易使用你自己的。
Links
Tutorial how run jsreport on Azure Web App on linux
jsreport official docker image
链接教程如何在linux jsreport官方docker镜像上的Azure Web App上运行jsreport
I'm aware I didn't answer how to actually run it in the same app as asp.net core. I don't know how to do that currently, but I suggest you to run asp.net application and jsreport in separate apps. You can easily invoke jsreport rendering through REST. This could also improve your architecture design.
我知道我没有回答如何在与asp.net核心相同的应用程序中实际运行它。我目前不知道该怎么做,但我建议你在不同的应用程序中运行asp.net应用程序和jsreport。您可以通过REST轻松调用jsreport呈现。这也可以改善您的架构设计。