Azure函数部署后PdfSharp转换PNG到PDF报不支持格式错误求助
问题:Azure函数中PdfSharp转换PNG到PDF失败(本地正常,云端报Unsupported image format)
我正在开发一个Azure函数,通过PdfSharp实现PNG字节数组转PDF字节数组,之后上传到Blob存储。本地运行代码完全正常,但部署到Azure后,在var image = XImage.FromStream(imageStream);行报错Unsupported image format,已确认输入是合法PNG格式。
代码如下:
public byte[] ConvertPngBytesToPdfBytes(byte[] pngBytes) { try { this.log.LogInformation("Starting conversion of PNG bytes to PDF bytes."); using var imageStream = new MemoryStream(pngBytes, 0, pngBytes.Length, false, true); this.log.LogInformation("MemoryStream created from PNG bytes."); var image = XImage.FromStream(imageStream); this.log.LogInformation("XImage created from MemoryStream."); using var pdfStream = new MemoryStream(); var document = new PdfDocument(); var page = document.AddPage(); page.Width = XUnit.FromPoint(image.PixelWidth * 72 / image.HorizontalResolution); page.Height = XUnit.FromPoint(image.PixelHeight * 72 / image.VerticalResolution); using (var gfx = XGraphics.FromPdfPage(page)) { gfx.DrawImage(image, 0, 0, page.Width.Point, page.Height.Point); } document.Save(pdfStream, false); this.log.LogInformation("PDF document saved to MemoryStream."); return pdfStream.ToArray(); } catch (Exception ex) { this.log.LogError(ex, "Error converting PNG bytes to PDF bytes: {0}", ex.Message); throw; } }
原因分析
传统PdfSharp依赖**GDI+**进行图像解码,而Azure Functions的Linux运行环境默认不支持GDI+;即使是Windows环境,也可能因为沙箱限制导致GDI+无法正常工作。本地开发环境通常有完整的GDI+支持,因此代码能正常运行,但云端环境的依赖缺失直接导致图像解码失败。
修复方案
方案1:改用跨平台的PdfSharpCore + ImageSharp(推荐)
传统PdfSharp不支持跨平台,建议替换为PdfSharpCore(PdfSharp的.NET Core版本),并搭配SixLabors.ImageSharp处理图像解码(PdfSharpCore本身不内置图像解码能力)。
卸载原PdfSharp包,安装以下NuGet包:
PdfSharpCoreSixLabors.ImageSharpPdfSharpCore.ImageSharp(用于将ImageSharp图像适配到PdfSharpCore)
修改转换代码:
using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; using PdfSharpCore.Drawing; using PdfSharpCore.Pdf; public byte[] ConvertPngBytesToPdfBytes(byte[] pngBytes) { try { this.log.LogInformation("Starting conversion of PNG bytes to PDF bytes."); // 使用ImageSharp加载PNG using var imageStream = new MemoryStream(pngBytes); using var image = Image.Load(imageStream, new PngDecoder()); this.log.LogInformation("PNG image loaded via ImageSharp."); using var pdfStream = new MemoryStream(); var document = new PdfDocument(); var page = document.AddPage(); // 计算页面尺寸(转换为PDF的点单位,72DPI) var dpi = image.Metadata.HorizontalResolution; page.Width = XUnit.FromPoint(image.Width * 72 / dpi); page.Height = XUnit.FromPoint(image.Height * 72 / dpi); using (var gfx = XGraphics.FromPdfPage(page)) { // 将ImageSharp图像转换为PdfSharpCore可用的XImage using var xImage = XImage.FromImageSharpImage(image); gfx.DrawImage(xImage, 0, 0, page.Width.Point, page.Height.Point); } document.Save(pdfStream, false); this.log.LogInformation("PDF document saved to MemoryStream."); return pdfStream.ToArray(); } catch (Exception ex) { this.log.LogError(ex, "Error converting PNG bytes to PDF bytes: {0}", ex.Message); throw; } }
方案2:切换Azure函数到Windows运行环境(临时兼容)
如果必须使用传统PdfSharp,可以将Azure函数的运行环境切换为Windows(Linux环境不支持GDI+):
- 进入Azure函数应用的配置页面
- 在「常规设置」中,将「操作系统」改为Windows
- 重启函数应用
注意:此方案仅适用于Windows环境,跨平台兼容性差,不推荐长期使用。
内容的提问来源于stack exchange,提问作者f_olivere
相关产品推荐
相关产品推荐

