Skip to content

Header and Footer settings break the Pdf? #198

@Diodium

Description

@Diodium

Hello,
I'm a noob :)

Goal: Add Header and Footer to generated pages.

IAction in controller responsible for the generation:

[HttpPost]
public async Task<IActionResult> BuildAndDownloadChecklistExport([FromBody] ChecklistExportInput data)
{
    try
    {
        var idUser = userMgr.GetUserId(User)!;

        var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";
        var html = await viewRenderService.RenderToStringAsync(idUser, "Checklist/_TemplateChecklist", data);

        var pdfDoc = new HtmlToPdfDocument()
        {
            GlobalSettings =
            {
                PaperSize = PaperKind.A4,
                Orientation = DinkToPdf.Orientation.Portrait,
            },
            Objects =
            {
                new ObjectSettings()
                {
                    HtmlContent = html,

                    HeaderSettings =
                    {
                        HtmUrl =  $"{baseUrl}/Templates/Resources/Checklist_Header.html",
                        Line = true,
                        Spacing = 5
                    },

                    FooterSettings =
                    {
                        HtmUrl = $"{baseUrl}/Templates/Resources/Checklist_Footer.html",
                        Line = true,
                        Spacing = 5

                    }
                },
            }
        };

        var pdf = converter.Convert(pdfDoc);
        return File(pdf, "application/pdf", "Report.pdf");
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception in BuildAndDownloadChecklistExport: ", ex.Message);
        byte[] pdf = { };
        return File(pdf, "application/pdf", "Report.pdf");

    }
}

Header and footer HTML (Not that it matter, since I tried super minimal html too):

<!DOCTYPE html>
<html>
<head>
    <style>
        /* Formato A4 */
        body {
            width: 210mm;
            min-height: 297mm;
            margin: 0;
            padding: 20mm;
            font-family: Arial, sans-serif;
            box-sizing: border-box;
            background: #FFF;
            color: #000;
        }

        .forcedFit {
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="ExportHeader">
        <img src="/Templates/Resources/Checklist_Header.png" class="forcedFit"/>
    </div>
</body>
</html>
      

If I comment these 2 out:

HeaderSettings =
                    {
                        HtmUrl =  $"{baseUrl}/Templates/Resources/Checklist_Header.html",
                        Line = true,
                        Spacing = 5
                    },

                    FooterSettings =
                    {
                        HtmUrl = $"{baseUrl}/Templates/Resources/Checklist_Footer.html",
                        Line = true,
                        Spacing = 5

Everything works, I get the PDF formatted as wanted, but when I try to fit these 2, what returns is a 0byte, unopenable .pdf (No exceptions thrown anywere)

Am I doing something wrong? How should I debug this?
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions