# -*- 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
import uuid

def html_to_pdf(source_path, target_path):
    options = {
        '--enable-local-file-access': '--enable-local-file-access',
        'encoding': "utf-8",
        # 'page-size': 'Letter',
        'page-width': '280mm',
        'page-height': '330mm',
        'margin-top': '0mm',
        'margin-right': '0mm',
        'margin-bottom': '0mm',
        'margin-left': '0mm'
    }
    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('/Users/pengxiong/Desktop/fund_report/app/html/v2/monthReportV2.html', '/Users/pengxiong/Desktop/fund_report/app/pdf/%s.pdf' % str(uuid.uuid4()))
    # merge_pdf(['/Users/pengxiong/Desktop/out1.pdf', '/Users/pengxiong/Desktop/out.pdf'])
