Select.Pdf - PDF page returns blank after it is created from Html

160 Views Asked by At

I am having some trouble with Select.Pdf in as such that the whole process of creating a PDF from HTML appears to be working through the entire code but when the PDF page opens, it is completely blank.

Here is my code, starting with my ajax call:

$.ajax({
        type: "POST",
        url: '@Url.Action("ActionName", "Controller")',
        data: {
            'foo': 1
        },
        success: function (data) {                                
            // Open the PDF in a new browser tab
            var blob = new Blob([data], { type: "application/pdf" });
            var link = document.createElement("a");
            link.href = window.URL.createObjectURL(blob);
            link.target = "_blank";
            link.click();
        },
        error: function (error) {
            alert(error.responseText);
        }
    });

And here is the code that converts the HTML (as a string) into a PDF document inside my controller action that returns an ActionResult (please note that the variable "viewHtml" is a string and has perfectly formatted HTML without any bells and whistles. Imagine the most simple and basic "Hello World"):

            SelectPdf.GlobalProperties.LicenseKey = ConfigurationManager.AppSettings["SelectPdf.LicenseKey"];
            // Create a PDF document
            PdfDocument pdfDocument = new PdfDocument();

            // Create an HTML to PDF converter
            HtmlToPdf converter = new HtmlToPdf();

            // Convert HTML content to PDF and add it to the document
            pdfDocument = converter.ConvertHtmlString(viewHtml)

            // Save the PDF to a memory stream
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            pdfDocument.Save(memoryStream);

            // Close the PDF document
            pdfDocument.Close();

            // Return the PDF as a file result
            return File(memoryStream.ToArray(), "application/pdf", filename);

So, all of this code runs perfectly and I can see the size of the blob change if the HTML is larger or shorter, so I know that there is data there. However, when pdf opens in a new browser tab, it is completely blank.

This has been a real headscratcher for the past couple of days, so any and all help is appreciated. Even Chat GPT is scratching its head.

Thanks!

Edited to add: I should also add the definition for the variable viewHtml since it appears to have caused some confusion. Prior to the initial C# code above, I am populating a model, passing it to a partial view and returning the HTML as a simple string, through the following method that populates viewHtml:

private string RenderViewToString(string viewName, MyViewModel vm)
    {
        ViewData.Model = vm;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }
0

There are 0 best solutions below