pymupdf 合成pdf

张映 发表于 2018-08-08

分类目录: 技术其他

标签:, ,

fpdf是一个基于php的pdf处理工具,但是有一个很严重的缺点,就是无法识别annots。pymupdf是基本mupdf的,基于python开发,功能比较强大,能识别annots

1,下载pymupdf

https://github.com/rk700/PyMuPDF

2,安装pymupdf

下载完成后,pymupdf/installation/centos,有安装方法

# wget https://mupdf.com/downloads/mupdf-1.13.0-source.tar.gz
# tar -zxvf mupdf-1.13.0-source.tar.gz

# cd mupdf-1.13.0-source
# export CFLAGS="-fPIC -std=gnu99"

# make HAVE_X11=no HAVE_GLFW=no HAVE_GLUT=no prefix=/usr/local
# sudo make HAVE_X11=no HAVE_GLFW=no HAVE_GLUT=no prefix=/usr/local install

# cd ..

# rm -rf PyMuPDF
# git clone https://github.com/rk700/PyMuPDF.git
# cd PyMuPDF

# sudo python setup.py build
# sudo python setup.py install

3,测试代码

# cat /root/mergePdf.py
#! python
'''
@author: tank
@blog: blog.51yip.com

v1.0 Created on Feb 25, 2018
'''
# coding:utf-8

from __future__ import print_function

import os, sys,fitz, json

data = json.loads(sys.argv[1])

doc = fitz.open(data['pdf'])
pageCount = doc.pageCount

for index in range(pageCount):
	str_index = str(index)
	# if data['images'].has_key("'" + str(index) + "'"):
	if str_index in data['images'].keys():

		page = doc[index]
		pix = fitz.Pixmap(data['images'][str_index])
		page.insertImage(page.rect, pixmap=pix, overlay=True)

doc.save(sys.argv[2])

传参数如下:

argv[1]对应参数如下:

{
 "pdf": "绝对径/1.pdf",
 "images": {
 "0": "绝对径/2.png",
 "2": "绝对径/3.png"
 }
}

argv[2]对应参数如下:

绝对径/2.pdf

调用方法:

# python mergePdf.py argv[1] argv[2]

上面的这个例子,是将二张图片,合成到pdf的第一页和第三页,并且生成新的pdf



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/other/1944.html

留下评论

留下评论
  • (必需)
  • (必需) (will not be published)
  • (必需)   8X2=?