SelectPDF not maintaining CSS in Razor

63 Views Asked by At

The PDF generated by selectPDF does not maintain the CSS styles. This code works fine on webforms, but it does not work on Razor pages.

Head:

<base href="http://localhost:5237/" />

<link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />

C#

public async Task<IActionResult> OnGetAsync()
{
    var htmlData = @"<p class=""h1 text-center fw-bold"">Test</p>";

    HtmlToPdf converter = new();
    converter.Options.PdfCompressionLevel = PdfCompressionLevel.Best;
    converter.Options.MarginTop = 10;
    converter.Options.MarginLeft = 10;
    converter.Options.MarginRight = 10;

    var baseUrl = $"{Request.Scheme}://{Request.Host}";

    PdfDocument doc = converter.ConvertHtmlString(htmlData, baseUrl);
    var pdfBytes = doc.Save();
    doc.Close();

    Response.ContentType = "application/pdf";
    Response.Headers.Add("Content-Disposition", "inline");

    return File(pdfBytes, "application/pdf");
}
0

There are 0 best solutions below