How to display header and footer all pages of the pdf while converting jinja template to pdf using pdfkit

91 Views Asked by At

I'm new to Python and Jinja Templates. I have a header and body on a jinja template. I used pdfkit.from_string(html_content, False, config='wkhtmltopdf.exe path') to convert this template to PDF format. The PDF conversion was completed, however header displayed only on the first page of the pdf. I want to display the header every pages of the PDF. Give me the solution, please.

Code details mentioned blow

from flask import Flask, make_response
import pandas as pd
import pdfkit 

app = Flask(__name__)

@app.route("/")
def home():

html_content = '''
                <!DOCTYPE html>
                <html>

                <head>
                    <title>Body</title>
                    <style>
                        .header{
                        position: running(header)
                        background-color: #f2f2f2;
                        padding:10px;
                        text-align:center;
                    }
                    @page {
                        @top-center {
                            content: element(header)
                        }
                    }
                    </style>
                </head>
                <body>
                    <div class='header'><h1>Header content</h1></div>
                                    
                    <table class='table'>
                        <tr class='table__header'>
                            <th style='background-color: #2F75B5;'>Name</th>
                            <th style='background-color: #2F75B5;'>Role</th>
                            <th style='background-color: #2F75B5;'>Salary</th>
                        </tr>
                        <tr class='table__row'>
                            <td class='table__cell'>Rahul</td>
                            <td class='table__cell'>SoftwareEngineer</td>
                            <td class='table__cell'>55000</td>
                        </tr>
                        <tr class='table__row'>
                            <td class='table__cell'>Vinod</td>
                            <td class='table__cell'>Product Engineer</td>
                            <td class='table__cell'>66000</td>
                        </tr>
                        <tr class='table__row'>
                            <td class='table__cell'>Amala</td>
                            <td class='table__cell'>Design Engineer</td>
                            <td class='table__cell'>43000</td>
                        </tr>
                        </table>
                    
                </body>

                </html>
            '''
responsestring = pdfkit.from_string(html_content, False, configuration=config)

    response = make_response(responsestring)

    response.headers['Content-Type'] = 'application/pdf'

    response.headers['Content-Disposition'] = 'attachment; filename=report.pdf'

    return response
if __name__ == "__main__":
    app.run(debug=True)

Note: I can able to generate the pdf successfully. My problem is header displayed only the first page. I want to show it's all the pages of pdf.

0

There are 0 best solutions below