Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header and Footer not Displaying in PDF Output #258

Open
VirajPoriya2202 opened this issue Jan 24, 2024 · 3 comments
Open

Header and Footer not Displaying in PDF Output #258

VirajPoriya2202 opened this issue Jan 24, 2024 · 3 comments

Comments

@VirajPoriya2202
Copy link

Issue Description:
I am using the pdfkit library to convert HTML content to a PDF file with specified header and footer HTML. However, the header and footer are not showing up in the generated PDF.Also, it is not working when I pass HTML local paths in 'header-html' and 'footer-html' options.

Code Snippet:

import pdfkit

def convert_html_to_pdf(html_content, output_pdf, header_html, footer_html):
    options = {
        'page-size': 'A4',
        'no-images': '',
        'margin-top': '15mm',
        'margin-right': '15mm',
        'margin-bottom': '15mm',
        'margin-left': '15mm',
        'header-html': header_html,
        'footer-html': footer_html,
    }

    pdfkit.from_string(html_content, output_pdf, options=options)

if __name__ == "__main__":
    # Dummy HTML content
    html_content = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Sample HTML to PDF</title>
    </head>
    <body>
        <h1>Hello, this is the main content of the page!</h1>
        <p>This is a sample HTML content.</p>
    </body>
    </html>
    """

    # Dummy header HTML content
    header_html = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Header</title>
    </head>
    <body>
        <header>
            <h2>This is the header of the page</h2>
        </header>
    </body>
    </html>
    """

    # Dummy footer HTML content
    footer_html = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Footer</title>
    </head>
    <body>
        <footer>
            <p>This is the footer of the page</p>
        </footer>
    </body>
    </html>
    """

    # Output PDF file
    output_pdf = "output_file.pdf"

    # Convert HTML to PDF
    convert_html_to_pdf(html_content, output_pdf, header_html, footer_html)

    print(f"PDF generated: {output_pdf}")
@BRGustavo
Copy link

Hello, I'm facing the same issue when adding header elements to the generated PDF files. The simplest way I found was to add the header directly in the HTML and control it using the CSS @media property.

@media print {
    #header {
        position: fixed;
        top: 0; 
        width: 100%;
    }
}

@br-bit3194
Copy link

Getting the same issue. Tried giving footer.html path as well as the footer_str. But still nothing is coming on the footer.

@HichamDz38
Copy link

problem solved here: https://stackoverflow.com/questions/34274637/pdfkit-headers-and-footers
pdfkit require .html files as header/footer, so making temp files will solve the problem
it's not the perfect solution, but it's how it will make it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants