3

I am trying to create PDF files from HTML. Now I want to apply a Background on my pages. But @page {background: blue;} seems to be not supported. I tried to add the background to the body but this causes other issues. At first, I need to specify backgrounds for first page, and another for all other pages.

Further more, the resulting Document looks like this:
enter image description here

This is not what I want. I need the background to cover the full page but the content should be inside the page border.

It should look something like this:
enter image description here

For this, I added padding to the body. But there are still problems. The background is not printed on the second page, the text is not inside the margin then breaking the page and I cannot set special background for the first page.

This is my HTML & CSS for the second result. For the first, the only difference is, that margin was added to @page instead on body.

html = """
<html>
    <head>
        <style>
            @page {
                size: A4;
                margin: 0;
            }
            
            body {
                background: #002244;
                color: white;
                padding: 2cm;
                box-sizing: border-box;
            }
        </style>
    </head>
    <body>
        """ + get_text() + """
    </body>
</html>

""" It is rendered and printed using pyppeteer`s page.pdf method.

How can I create a background, which covers the full page with text which is inside the page borders? And how can I set separate backgrounds for the first page?

Luxaaa
  • 119
  • 1
  • 4

0 Answers0