#!/usr/bin/env python
# coding: utf-8
import fitz
import os
import glob
import sys

class PdfToPng():
    def __init__(self,tagret_size=2):
        """

        :param zoom:    压缩比例
        :param tagret_size:  压缩目标大小,单位为 M
        :param file_path:    目标压缩文件
        :param save_path:    pdf解码后临时存放png的文件夹
        """
        self.zoom = 200
        self.target_size=tagret_size
        self.file_path = r'C:\Users\feida\Desktop\123\测试用的\2.pdf'
        # self.save_path = r'C:\Users\feida\Desktop\123\测试用的\save_path\\'
        self.save_path = os.path.dirname(os.path.abspath(__file__)) + '\\pdf_compress\\'
        if not os.path.exists(self.save_path):
            os.mkdir(self.save_path)
        pass

    def pdf_2to_png(self):
        # pdffile = glob.glob(self.path)[0]
        doc = fitz.open(self.file_path)
        strat = 0
        totaling = doc.pageCount
        while 1:
            for pg in range(strat, totaling):
                page = doc[pg]
                zoom = int(self.zoom)
                rotate = int(0)
                trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).preRotate(rotate)
                pm = page.getPixmap(matrix=trans, alpha=False)
                pm.writePNG(self.save_path + '%s.png' % str(pg + 1))
            list_files= sorted(glob.glob(self.save_path+'*.png'))
            size_files = 0
            for i in list_files:
                size_files += os.path.getsize(i)
            if (size_files/1024000) < self.target_size :
                break
            self.zoom -= 10
        doc.close()
        # return totaling

    def jpg_2to_pdf(self, pdf_name, n):
        list_file_path = sorted(glob.glob(self.save_path+'*.png'))
        n = n
        doc = fitz.open()
        # 从规定路径下读取图片,确保按文件名排序
        for img in list_file_path:
            # print(img)
            imgdoc = fitz.open(img)
            # 使用图片创建单页的 PDF
            pdfbytes = imgdoc.convertToPDF()
            # 将当前页插入文档
            imgpdf = fitz.open("pdf", pdfbytes)
            doc.insertPDF(imgpdf)
        path_fine = self.save_path + pdf_name + str(n) + '.pdf'
        doc.save(path_fine)
        doc.close()
        for img in list_file_path:
            os.remove(img)
        return path_fine

    def work(self):
        self.pdf_2to_png()
        new_file_path = self.jpg_2to_pdf('asd',0)
        return new_file_path


if __name__ == '__main__':
    a = PdfToPng()
    a.work()