python2
1. 安装
PyMuPDF==1.16.6
2.
import fitz
def _pdf_to_image(self, pdf_path, file_data, image_dir):
# 打开pdf
with open(pdf_path, 'w') as f:
f.write(file_data)
pdf_obj = fitz.open(pdf_path)
# 图片地址
image_paths = []
for page_index in range(pdf_obj.pageCount):
image_path = os.path.join(image_dir, '{}.png'.format(page_index + 1))
page = pdf_obj[page_index]
# 595*842 默认也就是基数1 需求2480*3508 (像素)
mat = fitz.Matrix(4.16806723, 4.16627078).preRotate(0)
pix = page.getPixmap(matrix=mat, alpha=False)
# 创建图片
pix.writePNG(image_path)
image_paths.append(image_path)
return image_paths