[컴][파이썬] 파이썬에서 html page 를 pdf 로 만들기 html to pdf




pdfkit 0.6.1 : Python Package Index


Windows에서 설치

  1. pip install pdfkit
  2. install wkhtmltopdf : wkhtmltopdf

사용법


import os 
import pdfkit
options = {
    'page-size': 'A4',
    'margin-top': '0.40in',
    'margin-bottom': '0.0in',
    'margin-right': '0in',
    'margin-left': '0in',
    'encoding': "UTF-8",
    'custom-header' : [
        ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'no-outline': None
}

config = pdfkit.configuration(wkhtmltopdf='c:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')

dirPath = os.path.dirname(os.path.realpath(__file__))
pdfkit.from_file(os.path.join(dirPath, 'template.htm'), 'out.pdf', options=options, configuration=config)



path 로 local file 를 사용할 때

아래처럼 file:/// 를 붙여줘야 한다.
bgPath = 'file:///' + os.path.join(dirPath, 'bg.png')
'<div style="background-color: white;' \
'background-image: url({bgPath}); background-size: 890px 1300px; background-repeat: no-repeat; color: #222222; font-family: Arial, Tahoma, ''Helvetica, FreeSans, sans-serif; , sans-serif; font-size: 13.2px; padding: 0 20px; padding-top: 40px; margin: 0 40px;' \
'height: 1290px;' \
'">'.format(bgPath=bgPath)







댓글 없음:

댓글 쓰기