文字PDF转图片PDF,适合pdf防复制

完整代码已传至github平台:
https://github.com/yaunsine/text_pdf_to_image_pdf

分成两步操作:

1、将文字pdf输出成图片

2、将所有图片合成为pdf

将PDF文件输出为图片的形式
python 复制代码
"""
    pdf转图片
"""
def pyMuPDF_fitz(pdfPath, imagePath):
    startTime_pdf2img = datetime.datetime.now()  # 开始时间
    create_directory(".\\output_img")
    print("imagePath=" + imagePath)
    pdfDoc = fitz.open(pdfPath)
    for pg in range(pdfDoc.pageCount):
        page = pdfDoc[pg]
        rotate = int(0)
        # 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。
        # 此处若是不做设置,默认图片大小为:792X612, dpi=96
        zoom_x = 1.33333333  # (1.33333333-->1056x816)   (2-->1584x1224)
        zoom_y = 1.33333333
        mat = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
        pix = page.get_pixmap(matrix=mat, alpha=False)

        if not os.path.exists(imagePath):  # 判断存放图片的文件夹是否存在
            os.makedirs(imagePath)  # 若图片文件夹不存在就创建

        pix.save(imagePath + '/' + 'images_%s.png' % pg)  # 将图片写入指定的文件夹内

    endTime_pdf2img = datetime.datetime.now()  # 结束时间
    print('pdf2img时间=', (endTime_pdf2img - startTime_pdf2img).seconds)
将所有的图片合并成PDF
python 复制代码
"""
    合并图片成pdf
"""
def merge_img_to_pdf(path: str, name: str):
    create_directory(".\\output_pdf")
    img_open_list = []                                 # 创建打开后的图片列表
    for root, dirs, files in os.walk(path):
        for i in files:
            file = os.path.join(root, i)               # 遍历所有图片,带绝对路径
            img_open = Image.open(file)                # 打开所有图片
            if img_open.mode != 'RGB':
                img_open = img_open.convert('RGB')     # 转换图像模式
            img_open_list.append(img_open)             # 把打开的图片放入列表
    # pdf_name = name + '.pdf'                           # pdf文件名
    pdf_name = name
    img_1 = img_open_list[0]                           # 打开的第一张图片
    # 把img1保存为PDF文件,将另外的图片添加进来,列表需删除第一张图片,不然会重复
    img_open_list = img_open_list[1:]
    img_1.save(pdf_name, "PDF", resolution=100.0, save_all=True, append_images=img_open_list)
    print('转换成功!pdf文件在当前程序目录下!')
    clear_imgs(path)
相关推荐
艾莉丝努力练剑29 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle6 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程