azure - ASP.NET Core 中 Azure 上的 Phantom pdf

标签 azure asp.net-core pdf-generation jsreport

我正在使用jsreportphantom pdf RecipeASP.NET Core中将 View 渲染为pdf。该项目作为应用服务Azure中运行。这在我的本地计算机上运行良好,但是当我发布到 Azure 时,我收到以下错误消息。

异常:调用节点模块失败并出现错误:ReferenceError:e 未定义 在 module.exports (D:\home\site\wwwroot\serviceCallDetailsPdf.js:18:26)

这是我的 js 文件,它位于我的项目的根目录中(与 wwwroot、 View 、 Controller 等相同的目录级别)。

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

注意:最后一次回调通常不会出现。如果我删除它,我会收到 Node.js 模块在 60000 毫秒后超时的更改,当然它需要 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"
  }
}

最后,这是我的操作:

[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

这基本上意味着您无法在 Azure 应用服务中使用幻影 pdf 配方。还有人可以确认吗?如果是这种情况,有人知道可以在 Azure 应用服务中使用的 html 到 pdf 渲染器吗?

最佳答案

phantomjs 和其他 pdf 渲染无法在默认 Windows Server 上运行的 Azure 应用服务中运行。不过,您现在可以使用在 Linux 上运行的 Azure 应用服务,其中 jsreport 可以很好地工作。

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

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

我知道我没有回答如何在与 asp.net core 相同的应用程序中实际运行它。我目前不知道该怎么做,但我建议您在单独的应用程序中运行 asp.net 应用程序和 jsreport。您可以通过REST轻松调用jsreport渲染。这也可以改进您的架构设计。

关于azure - ASP.NET Core 中 Azure 上的 Phantom pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42472649/

相关文章:

node.js - 部署到 Azure 时出现 Npm 错误:fsEvents 平台不受支持

asp.net-core - ASP.Net Core Razor 页面 : How to return the complex model on post?

java - 带有 Flying Saucer 的 SVG 背景图像

azure - Azure SQL 数据库中的数据传输

azure - Arm模板800资源限制

c# - ASP.NET 核心 FromQuery 获取内部带有点号的无效参数

c# - 为什么 HttpPlatformHandler 不创建日志文件?

c# - 如何在 MigraDoc 库中设置文档方向(针对所有页面)?

python - 在 Python 中将 HTML 页面转换为 PDF?

java - windows azure java客户端调用RESTFUL API