我有一个使用react.js作为前端的asp.net核心2.0解决方案。我不久前将我的应用程序发布到Azure(PaaS)。在我尝试生成pdf之前,一切都很好。
在我的本地计算机上它可以工作,但在Azure App Service上我得到以下异常:
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException"," Message":"尝试连接到节点在60000ms后超时
我试过已经设定了
"engines":{"node":version}
或
WEBSITE_NODE_DEFAULT_VERSION
(关于天蓝色变量)
前端代码:
private generatePdf() {
let htmlContent = document.getElementById("dataToPrint");
GetDetailsPdf(this.props.prop1, htmlContent.innerHTML).then((data: Blob) => {
// Create of hidden anchor for start download in browser
var a: HTMLAnchorElement = document.createElement("a");
a.hidden = true;
document.body.appendChild(a);
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = `${this.props.prop1}_${this.props.prop2}.pdf`;
a.click();
window.URL.revokeObjectURL(url);
});
}
后端:
public async Task<IActionResult> ExportPdf(
Guid id,
[FromBody] JObject jObject,
[FromServices] INodeServices nodeServices,
[FromServices] IHtmlToPdfConverter converter)
{
var htmlJObject = jObject.First;
var htmlContent = htmlJObject.First.Value<string>();
var pdfFile = await converter.GetPdfFile(htmlContent);
return File(pdfFile, "application/pdf", $"MyPDf_{id}.pdf");
}
转换器只需调用
nodeServices.InvokeAsync<byte[]>("./pdf", htmlTemplate);