annah
annah

Reputation: 1

Page break in Wkhtmltopdf.NetCore

I need to generate a PDF with tables through a WEB API in ASP.NET CORE. I currently have a method that receives the HTML in a string and using the GetPDF method from Wkhtmltopdf.NetCore, I generate the PDF and then return it in bytes.

public IActionResult Html2Byte([FromBody] string html)
{
    var data = generatePdf.GetPDF(html);
    return Ok(data);
}

The problem is that I need to manually set a page break in the HTML between the tables to split them in different sections in the PDF.

I tried adding with the tag <br> and via CSS with the following classes using @media print and without it:

@media print {
    .keep-together-page {
        page-break-inside: avoid;
    }

    .break-before-page {
        page-break-before: always;
    }

    .break-after-page {
        page-break-after: always;
    }
}

However, no matter how I add the CSS, either by classes or directly in the style attribute, it still doesn't recognize the page break. The closest way I was able to reproduce was adding a height for the divs, but that it's not ideal due to the number of rows in the tables that may vary.

Is there something I can do to make it recognize the page break? Any help is welcome

Upvotes: 0

Views: 1282

Answers (1)

bsod_
bsod_

Reputation: 941

Try placing a style attribute at the beginning of the HTML, for example -

<style type="text/css">
    .keep-together-page {
        page-break-inside: avoid;
    }

    .break-before-page {
        page-break-before: always;
    }

    .break-after-page {
        page-break-after: always;
    }
</style>

Upvotes: 1

Related Questions