0

HiqPdf - 使用带有“评估水印”的 HiqPDF,一切都可以在我的本地工作,但是当它部署在服务器(Azure)中时,它会引发错误:

错误:无法转换为 PDF。无法添加演示水印。字体无效。

我正在发送 html 以转换为 pdf。

有什么帮助吗?

HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
                htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
                switch (htmlModel.PageOrientation)
                {
                    case Constants.PAGE_ORIENTATION_PORTRAIT:
                        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
                        break;
                    default:
                        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Landscape;
                        break;
                }
                htmlToPdfConverter.Document.Margins = new PdfMargins(10, 10, 10, 10);
                htmlToPdfConverter.SerialNumber = "";
                if (!string.IsNullOrEmpty(htmlModel.ElementSelector))
                {
                    htmlToPdfConverter.ConvertedHtmlElementSelector = htmlModel.ElementSelector;
                }
                string decodedHtml = htmlModel.HtmlString.Replace("$ReplaceSign1$", "<").Replace("$ReplaceSign2$", ">");
                htmlBytes = htmlToPdfConverter.ConvertHtmlToMemory(decodedHtml, HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority);
4

1 回答 1

0

TL;博士:

Add-Font.ps1从这里使用: https ://github.com/wormeyman/FindFonts 将 Microsoft Sans Serif 字体安装C:\Windows\Fonts\micross.ttf到您的 Docker 容器或 Windows Server 中。

如果它不起作用,HiqPdf 可能需要不同的字体。使用SysInternals的 Process Monitor 找出需要哪一个。

解释

当我尝试在 Windows Server 2019 Docker 容器中使用 Winnovative HTML-to-PDF 时遇到了同样的错误。原因是 Windows Server 的 docker 基础映像不包含 Windows 10 包含的字体,并且转换需要特定的字体。就我而言,它是 Microsoft Sans Serif。我使用Process Monitor来找出需要哪些字体。

使用 Process Monitor 找出所需的字体

首先使用“过滤器”按钮为“进程名称为 fontdrvhost.exe”添加过滤器:

为 fontdrvhost.exe 添加过滤器

然后在 PDF 转换期间捕获活动。您将看到在 Path 列中访问了哪些字体:

转换期间访问的字体

现在我们知道我们需要哪种字体了。

安装字体

Add-Font.ps1从这里下载: https ://github.com/wormeyman/FindFonts并将其复制到您的服务器/容器。为了方便起见,我创建了一个C:\fonts文件夹并将脚本复制到其中。

然后将所需的字体复制到同一个文件夹中。就我而言,它是C:\Windows\Fonts\micross.ttf

运行脚本来安装字体:

Add-Font.ps1 micross.tff

现在 Html-PDF 转换应该可以正常工作了。

于 2020-09-11T08:47:38.463 回答