问题描述
这在本地运行时在ASP.NET MVC应用程序中有效,但在Azure上部署时不是:
This works in an ASP.NET MVC application when run locally, but not when deployed on 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.我没有看到任何引发的异常或其他错误消息.我找到了一些SO答案,说明PDFsharp的GDI版本在Azure上不起作用,所以我改用WPF版本-相同的结果.
Locally, I get a nice PDF. On Azure, I get a blank PDF. I'm not seeing any exceptions thrown or other error messages. I found some SO answers stating that the GDI version of PDFsharp doesn't work on Azure, so I'm using the WPF version instead - same result.
我发现了这个问题,但是我不清楚如何将其应用于MVC应用程序:
I found this SO question, but I'm not clear how to apply it to an MVC app: Why is MigraDoc generating a blank pdf in my asp.net application?
很抱歉,如果这是一个明显的问题,我就被卡住了!
Sorry if this is an obvious question, I'm just stuck!
推荐答案
如果完整的PDF到达客户端,很可能是字体问题(我要求评论中的格式,但还没有答案).
It's likely a font problem if a complete PDF arrives at the client (I asked for conformation in a comment but got no answer yet).
PDFsharp必须有权访问TTF文件以提取信息.您在%windir%\ fonts文件夹中使用的字体吗?您的进程是否具有读取这些字体的特权?
PDFsharp must have access to the TTF files to extract information. Are the fonts you use in the %windir%\fonts folder and does your process have privileges to read them?
Azure是IFontResolver的候选对象,因为缺少许多字体并且通常不授予特权.
Azure is a candidate for IFontResolver because many fonts are missing and privileges are usually not granted.
使用IFontResolver,您可以直接使用PDFsharp访问TTF文件(作为byte []).
With IFontResolver you give PDFsharp direct access to the TTF files (as byte[]).
您可以将我的类EZFontResolver用于此目的:
http://developer.th-soft.com/developer/2015/12/11/ezfontresolver-a-generic-font-resolver-for-pdfsharp-and-migradoc/
You can use my class EZFontResolver for that purpose:
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/
I also have a sample that shows how to implement your own IFontResolver:
http://developer.th-soft.com/developer/2015/09/21/using-private-fonts-with-pdfsharp-1-50-beta-2-or-migradoc/
这篇关于PDFsharp在Azure中生成空白页,但可在本地使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!