# -*- 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():
    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 = 'https://www.jianshu.com'
    # url = 'http://baidu.com'
    url2 = 'https://manage.meerkat.top'
    # pdfkit.from_url(url1, '../pdf/out5.pdf', options=options)
    # pdfkit.from_file('../templates/monthReport.html', '../pdf/out5.pdf', options=options)
    pdfkit.from_file('app/html/monthReport.html', 'app/pdf/out5.pdf', 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'])
