如何在 Azure 函数生成的 PDF 中显示文本
How to show text in PDF generated by Azure function
我正在尝试使用 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 文本的位置出现了方框(参见下面的示例)。
此外,响应速度也非常慢。有办法 improve/correct 这个吗?
附加信息:
我也在用unity IOC来解析IConverter
。类型注册如下所示:
var container = new UnityContainer();
container.RegisterType<IConverter>(
new ContainerControlledLifetimeManager(),
new InjectionFactory(c => new SynchronizedConverter(new PdfTools()))
);
我尝试了其他几个 NuGet 包,例如 PdfSharp、MigraDoc、Select.HtmlToPdf.NetCore 等。但是所有这些都依赖于 System.Drawing.Common
,这在Azure 函数。
该问题似乎与 "Consumption" 模式下 Azure 函数的限制有关。如果您使用 "App Mode",它应该可以工作。
请参阅 the discussion below this Gist 了解一些已成功将其 Azure 函数转换为 "App Mode" 的用户。
我正在尝试使用 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 文本的位置出现了方框(参见下面的示例)。
此外,响应速度也非常慢。有办法 improve/correct 这个吗?
附加信息:
我也在用unity IOC来解析
IConverter
。类型注册如下所示:var container = new UnityContainer(); container.RegisterType<IConverter>( new ContainerControlledLifetimeManager(), new InjectionFactory(c => new SynchronizedConverter(new PdfTools())) );
我尝试了其他几个 NuGet 包,例如 PdfSharp、MigraDoc、Select.HtmlToPdf.NetCore 等。但是所有这些都依赖于
System.Drawing.Common
,这在Azure 函数。
该问题似乎与 "Consumption" 模式下 Azure 函数的限制有关。如果您使用 "App Mode",它应该可以工作。 请参阅 the discussion below this Gist 了解一些已成功将其 Azure 函数转换为 "App Mode" 的用户。