7

I am using pdfkit with python/django to convert rendered html to pdf. How can page numbers be added in the pdf liike page 1 of 4. I am converting html to string then passing it to from_string method of pdfkit.

Vinayak
  • 87
  • 1
  • 1
  • 6

2 Answers2

24

To add to the above anwser

  options = { 
      'margin-bottom': '0.75in', 
      'footer-right': '[page] of [topage]',
     }  
pdfkit.from_string('Hello World', 'out.pdf', options=options)

Will give the output 1 of 2

the following are the other variables can be substituted.

  • [page] Replaced by the number of the pages currently being printed
  • [frompage] Replaced by the number of the first page to be printed
  • [topage] Replaced by the number of the last page to be printed
  • [webpage] Replaced by the URL of the page being printed
  • [section] Replaced by the name of the current section
  • [subsection] Replaced by the name of the current subsection
  • [date] Replaced by the current date in system local format
  • [time] Replaced by the current time in system local format
arasub
  • 447
  • 5
  • 10
  • Is it possible to modify the vertical positioning of the numbers? I have some text in the footer (2 lines of text). The page numbers are always at the top, but I would like to move them down (in line with the bottom line). – elano7 Mar 27 '20 at 18:48
  • Yes, we need to play with margin. In case your text is static we can. append before page number. try these options. – arasub Mar 27 '20 at 21:11
  • Is it possible to get the page number in the header? – Milon Mahato Jun 06 '23 at 09:26
14

Adding options should allow for creation of page numbers as follows:

options = {
   'margin-bottom': '0.75in',
   'footer-right': '[page]'
   }
pdfkit.from_string('Hello World', 'out.pdf', options=options)

According to PDFKit's documentation you can use all of wkhtmltopdf's options

Woodsy
  • 3,177
  • 2
  • 26
  • 50