当本地运行时,这适用于 ASP.NET MVC 应用程序,但部署在 Azure 上时则不适用:
Document doc = new Document();
Section section = doc.AddSection();
section.AddParagraph("Some text to go into a PDF");
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
pdfRenderer.Document = doc;
pdfRenderer.RenderDocument();
System.IO.MemoryStream stream = new System.IO.MemoryStream();
pdfRenderer.PdfDocument.Save(stream, false);
Byte[] documentBytes = stream.ToArray();
return File(documentBytes, "application/pdf");
在本地,我得到了一个不错的 PDF。在 Azure 上,我得到一个空白 PDF。我没有看到任何抛出的异常或其他错误消息。我发现一些答案指出 PDFsharp 的 GDI 版本无法在 Azure 上运行,因此我使用 WPF 版本 - 相同的结果。
我发现了这个问题,但我不清楚如何将其应用到 MVC 应用程序:Why is MigraDoc generating a blank pdf in my asp.net application?
抱歉,如果这是一个显而易见的问题,我只是被困住了!
最佳答案
如果完整的 PDF 到达客户端,则可能是字体问题(我在评论中要求确认,但尚未得到答复)。
PDFsharp 必须有权访问 TTF 文件才能提取信息。您使用的字体是在 %windir%\fonts 文件夹中吗?您的进程是否有权读取它们?
Azure 是 IFontResolver 的候选者,因为许多字体丢失并且通常不授予权限。
使用 IFontResolver,您可以让 PDFsharp 直接访问 TTF 文件(作为 byte[])。
您可以使用我的类 EZFontResolver 来实现此目的:
http://developer.th-soft.com/developer/2015/12/11/ezfontresolver-a-generic-font-resolver-for-pdfsharp-and-migradoc/
我还有一个示例,展示如何实现您自己的 IFontResolver:
http://developer.th-soft.com/developer/2015/09/21/using-private-fonts-with-pdfsharp-1-50-beta-2-or-migradoc/
关于c# - PDFsharp 在 Azure 中生成空白页面,但在本地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051937/