【问题标题】:How to show text in PDF generated by Azure function如何在 Azure 函数生成的 PDF 中显示文本
【发布时间】:2018-11-07 11:30:53
【问题描述】:

我正在尝试使用 DinkToPdf 通过 Azure 函数生成 PDF。这是我到目前为止所做的。

[FunctionName("GeneratePdf")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log,
    ExecutionContext executionContext)
{
    string name = await GetName(req);
    return CreatePdf(name, executionContext);
}

private static ActionResult CreatePdf(string name, ExecutionContext executionContext)
{
    var globalSettings = new GlobalSettings
    {
        ColorMode = ColorMode.Color,
        Orientation = Orientation.Portrait,
        PaperSize = PaperKind.A4,
        Margins = new MarginSettings { Top = 10 },
    };
    var objectSettings = new ObjectSettings
    {
        PagesCount = true,
        WebSettings = { DefaultEncoding = "utf-8" },
        HtmlContent = $@"
               <!DOCTYPE html>
               <html>
               <head>
                   <meta charset="utf-8" />
                   <title></title>
                   <meta name="viewport" content="width=device-width, initial-scale=1">
               </head>
               <body>
                  Hello, ${name}
               </body>
               </html>",
     };

     var pdf = new HtmlToPdfDocument()
     {
        GlobalSettings = globalSettings,
        Objects = { objectSettings }
     };

     byte[] pdfBytes = IocContainer.Resolve<IConverter>().Convert(pdf);
     return new FileContentResult(pdfBytes, "application/pdf");
}

当我在本地测试该功能时,这工作得很好。但是,它在部署到 Azure 时无法按预期工作。

主要问题是在 pdf 中的文本位置出现了框(例如,请参见下文)。

此外,响应速度也非常缓慢。有没有办法改进/纠正这个?

附加信息:

  1. 我也在使用统一 IOC 来解决 IConverter。类型注册如下所示:

    var container = new UnityContainer();
    container.RegisterType<IConverter>(
        new ContainerControlledLifetimeManager(),
        new InjectionFactory(c => new SynchronizedConverter(new PdfTools()))
    );
    
  2. 我已经尝试过其他几个 NuGet 包,例如 PdfSharp、MigraDoc、Select.HtmlToPdf.NetCore 等。但所有这些包都依赖于 System.Drawing.Common,这在 Azure 函数中不可用。

【问题讨论】:

  • 当我看到渲染时首先想到的是您要么在 Azure 环境中缺少字体,要么编码效果不佳。
  • @rickvdbosch 这也是我的想法,然后添加了WebSettings = { DefaultEncoding = "utf-8" } 部分,但它也没有帮助:(
  • @rickvdbosch 另外,我没有使用任何自定义字体。在这种情况下,我假设默认字体和编码(utf-8,如指定)应用作后备。或者,不是这样吗?
  • 我也在考虑字体问题。这是一个似乎与您的问题非常相关的开放功能请求:feedback.azure.com/forums/169385-web-apps/suggestions/…。也许您可以尝试强制使用字体(以某种方式不是“webfont”,让我们从“Times New Roman”开始)
  • @Pac0 谢谢你的评论。按照您的建议,我尝试了明确设置字体系列 (font-family: 'Times New Roman', Times, serif;),以及包括像 &lt;link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet"&gt;&lt;style type = "text/css"&gt; * { font-family: 'Open Sans', sans-serif !important; } &lt;/style&gt; 这样的外部字体。但这些都不起作用。

标签: c# azure .net-core azure-functions wkhtmltopdf


【解决方案1】:

该问题似乎与“消费”模式下 Azure 函数的限制有关。如果您使用“应用程序模式”,它应该可以工作。 请参阅the discussion below this Gist 了解一些成功将 Azure Function 转换为“应用程序模式”的用户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    相关资源
    最近更新 更多