Search code examples
c#.netazure-functionsopenhtmltopdf

all the text on the pdf is rendering as squares/blocks □


Why is my output being generated with just squares?

I'm attempting to convert html to a pdf, and I'm getting a weird output, where all the characters are squares, as in the following:

enter image description here

My function is below:

   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);
        }
    }

Why is my output being generated with just squares? What am I doing wrong?


Solution

  • Turns out that we were not able to run this on the consumption plan. The OpenHtmlToPdf library uses fonts, which requires an OS to be present within that environment. After switching the function to an App Sevice plan, this issue was simply resolved.