# -*- encoding: utf-8 -*-
# -----------------------------------------------------------------------------
# @File Name  : html_to_pdf.py
# @Time       : 2020/11/19 上午11:07
# @Author     : X. Peng
# @Email      : acepengxiong@163.com
# @Software   : PyCharm
# -----------------------------------------------------------------------------

import pdfkit
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter

def html_to_pdf(source_path, target_path):
    options = {
        '--enable-local-file-access': '--enable-local-file-access',
        'encoding': "utf-8",
        # 'page-size': 'A5',
        # 'page-width': '300mm',
        # 'page-height': '200mm',
        'margin-top': '0mm',
        'margin-right': '0mm',
        'margin-bottom': '0mm',
        'margin-left': '0mm'
    }
    url1 = 'http://www.qimontech.com'
    # pdfkit.from_url(url1, '/Users/pengxiong/Desktop/out5.pdf', options=options)
    pdfkit.from_string(source_path, target_path, options=options)
    # pdfkit.from_file(source_path, target_path, options=options)
def merge_pdf(pdfFiles, target_file='/Users/pengxiong/Desktop/combine.pdf'):
    """"""
    merger = PdfFileMerger()
    pdfWriter = PdfFileWriter()  # 生成一个空白的pdf文件
    for fileName in pdfFiles:
        pdfReader = PdfFileReader(open(fileName, 'rb'))  # 以只读方式依次打开pdf文件
        for pageNum in range(pdfReader.numPages):
            print(pdfReader.getPage(pageNum))
            pdfWriter.addPage(pdfReader.getPage(pageNum))  # 将打开的pdf文件内容一页一页的复制到新建的空白pdf里
    pdfOutput = open(target_file, 'wb')  # 生成combine.pdf文件
    pdfWriter.write(pdfOutput)  # 将复制的内容全部写入combine.pdf


if __name__ == '__main__':
    html_to_pdf()
    # merge_pdf(['/Users/pengxiong/Desktop/out1.pdf', '/Users/pengxiong/Desktop/out.pdf'])
