我使用jsreport
和phantom pdf recipe
在ASP.NET Core
中呈现视图为pdf。该项目作为App Service
在Azure
中运行。这在我的本地机器上正常工作,但是当我发布到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
*.js
和node_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
这基本上意味着你不能在使用幻象PDF配方Azure应用服务。其他人可以确认吗?如果是这种情况,是否有人知道可以在Azure应用服务中工作的html to pdf渲染器?
你已经通过链接到的其他问题找到了答案(基本上这是该问题的重复)。您不能在Web应用程序中使用ActiveX/GDI /等。 –