flyingsaucerproject / flyingsaucer

XML/XHTML and CSS 2.1 renderer in pure Java
Other
2.02k stars 565 forks source link

Is automatic page break possible? #439

Open mewebstudio opened 3 weeks ago

mewebstudio commented 3 weeks ago

Hello, I opened an issue because I could not find a source related to my problem.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>PDF</title>
    <style>
        @page {
            size: letter;
            margin: 0;

            @top-right {
                content: element(header);
            }

            @bottom-center {
                content: element(footer);
            }
        }

        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
            font-size: 12px;
            color: #444;
        }

        header {
            display: block;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            padding: 30px 15px;
            height: 30px;
            background-color: #d9c597;
            border-top: 1px solid #fff;
            text-align: center;
            font-size: 22px;
            font-weight: 300;
            color: #fff;
            z-index: 1000;
        }

        footer {
            display: block;
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 30px 15px;
            height: 30px;
            background-color: #d9c597;
            border-bottom: 1px solid #fff;
            text-align: center;
            font-weight: 300;
            font-size: 22px;
            color: #fff;
        }

        section {
            display: block;
            padding: 100px 40px;
            page-break-after: always;
        }

        section:last-child {
            page-break-after: auto;
        }
    </style>
</head>
<body>

<section>
    <p>Lorem ipsum...</p>
</section>

<header>Header</header>

<footer>Footer</footer>

</body>
</html>

I'm trying to create a PDF with HTML like this. But when the page content is long, the header and footer rules are invalid on pages other than the first page.

Screenshot 2024-11-05 at 20 31 28

In order to solve the problem, when I want to define margin values ​​for page, give a negative top value for the header, and a negative bottom value for the footer, these areas remain below the layout. For example:

        @page {
            size: letter;
            display: block;
            margin: 90px 0;

            @top-right {
                content: element(header);
            }

            @bottom-center {
                content: element(footer);
            }
        }

        body {
            margin: 0;
            padding: 0;
            ...
        }

        header {
            display: block;
            position: fixed;
            top: -45px;
            left: 0;
            right: 0;
           ...
        }

        footer {
            display: block;
            position: fixed;
            bottom: -45px;
            left: 0;
            right: 0;
            ...
        }
Screenshot 2024-11-05 at 20 38 59

Do you have any suggestions to solve this problem? Thanks in advance.