pdf上的所有文本都呈现为正方形/块□

时间:2019-04-15 16:33:50

标签: c# .net azure-functions openhtmltopdf

为什么只用正方形生成我的输出?

我正在尝试将html转换为pdf,并且得到了一个奇怪的输出,其中所有字符都是正方形,如下所示:

enter image description here

我的功能如下:

   public static class ConstructPDFHttpTriggered
    {
        [FunctionName("ConstructPDFHttpTriggered")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("Processing PDF Request");

            string dataDetails = string.Empty;
            bool isError = false;

            try
            {
                string name = req.GetQueryNameValuePairs()
                            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                            .Value;

                if (name == null)
                {
                    // Get request body
                    dynamic data = await req.Content.ReadAsAsync<object>();
                    name = data?.name;
                }

                //Constrcut PDF object
                var pdf = Pdf

                    .From(name)
                    .WithObjectSetting("web.printMediaType", "true")
                    .Content();

                log.Info($"PDF Generated. Length={pdf.Length}");

                //Convert PDF stream to base64 string
                dataDetails = Convert.ToBase64String(pdf);
            }
            catch (Exception ex)
            {
                isError = true;
                dataDetails = ex.Message;
            }

            return isError
                ? req.CreateResponse(HttpStatusCode.BadRequest, dataDetails)
                : req.CreateResponse(HttpStatusCode.OK, dataDetails);
        }
    }

为什么只用正方形生成我的输出?我在做什么错了?

1 个答案:

答案 0 :(得分:0)

事实证明,我们无法在consumption plan上运行它。 OpenHtmlToPdf库使用字体,该字体要求在该环境中存在操作系统。将功能切换到App Sevice plan后,此问题就解决了。