问题
Recently after moving our Website from Windows Server 2008 to Server 2016, we started facing problems exporting PDF documents from our C# code that uses MigraDoc/PDFsharp library.
Problem can also be reproduced on windows 10. Exactly same code works just fine on Windows 7 or, as mentioned earlier, any server OS prior to Server 2016.
Previously we were getting this exception:
Error while parsing an OpenType font. InnerException: InvalidOperationException: Font has no usable platform or encoding ID. It cannot be used with PDFsharp.
So we tried to upgrade the references to the latest assembly version and now we are getting a different error:
TrueType collection fonts are not yet supported by PDFsharp.
Please do not jump the gun and mark this as duplicate of some other issues where Chinese characters are being displayed as squares or ??? in the exported PDF. This is not an font embedding issue. It is different than that. Somehow on Windows 10 and Server 2016, if you try to use the Microsoft Ya Hei (微软雅黑) font then it just does not work. Strangely enough everything was working fine with the same code same version assembly before migrating to Server 2016. Here is a relevant code without the paraphernalia
.
.
.
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
.
.
Document document = new Document();
document.Info.Title = "Report";
document.Styles["Normal"].Font.Name = "微软雅黑"; // Microsoft YaHei Chinese Font
Section section = document.AddSection();
document.LastSection.AddParagraph("### This font is: 微软雅黑");
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
renderer.Document = document;
renderer.RenderDocument();
renderer.PdfDocument.Save(@"Test.pdf");
Can someone help me understand the bigger picture: What has changed with respect to Chinese fonts and what exactly is going on here?
回答1:
The error message tells it: Font collections (extension .ttc) are not yet supported, TrueType fonts (extension .ttf) are supported.
So a workaround could be using the font files from an older Windows version as they appear to be compatible with the current version of PDFsharp.
There could be problems installing fonts from older Windows versions on the Fonts folder of Windows.
The IFontResolver
interface of the WPF build of PDFsharp/MigraDoc allows using fonts that are not installed on the computer. Maybe try EZFontResolver to keep it simple: forum.pdfsharp.net/viewtopic.php?f=8&t=3244
Adding support for font collections seems to be not very complicated, but somebody must implement it. The current version of today, 1.50.4845-RC2a, does not yet support them.
来源:https://stackoverflow.com/questions/52791080/migradoc-pdfsharp-throwing-exceptions-with-chinese-yahei-font