Search code examples
htmlpython-2.7wkhtmltopdfpython-pdfkit

How to display a footer line above the footer using the pdfkit library in python?


Background Info

I have the following Python code that I want to use to generate a pdf file. It uses the pdfkit library.

import pdfkit               # import python module

if __name__=="__main__":
   
    
    options = {
        'page-size': 'Letter',
        'margin-top': '0.5in',
        'margin-right': '0.75in',
        'margin-bottom': '0.5in',
        'margin-left': '0.75in',
        'encoding': "UTF-8",
        'footer-left': "This is a footer",
        'footer-font-size':'7',
        'footer-right': '[page] of [topage]',
        
        'custom-header' : [
            ('Accept-Encoding', 'gzip')
        ],
        'no-outline': None
    }

    ##this is the path of the whkhtmltopdf.exe in order for the library to 
    ##work on a Windows OS
    path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
            
    pdfkit.from_url('http://google.com', 'Report.pdf',options=options,configuration=config))

The Resulting PDF is as follows. All I need simply is a line above this is a footer. The Resulting PDF

According to to the following site, I can add a footer line above the footer using the attribute footer-line but I'm not understanding the syntax on how to implement it in python

https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

The Question in Brief

How do I modify the options attribute to include footer-line?

 options = {
        'page-size': 'Letter',
        'margin-top': '0.5in',
        'margin-right': '0.75in',
        'margin-bottom': '0.5in',
        'margin-left': '0.75in',
        'encoding': "UTF-8",
        'footer-left': "This is a footer",
        'footer-font-size':'7',
        'footer-right': '[page] of [topage]',
        
        'custom-header' : [
            ('Accept-Encoding', 'gzip')
        ],
        'no-outline': None
    }

Solution

  • Apparently you just add the attribute and pass it an empty parameter

    so to the options attribute you just add 'footer-line':'' So it becomes the following

         options = {
            'page-size': 'Letter',
            'margin-top': '0.5in',
            'margin-right': '0.75in',
            'margin-bottom': '0.5in',
            'margin-left': '0.75in',
            'encoding': "UTF-8",
            'footer-left': "This is a footer",
            'footer-line':'',
            'footer-font-size':'7',
            'footer-right': '[page] of [topage]',
    
            'custom-header' : [
                ('Accept-Encoding', 'gzip')
            ],
            'no-outline': None
        }
    

    If there is a better way to do it please let me Know