PDF 转长图
是什么:将多页 PDF 文档合并为单张长图片。
为什么:方便在移动端查看和分享,避免翻页。
Python 示例
python
from pdf2image import convert_from_path
from PIL import Image
def pdf_to_long_image(pdf_path, output_image="long_image.jpg"):
"""PDF 转长图"""
# 1. PDF 转图片列表
images = convert_from_path(pdf_path, dpi=150)
# 2. 计算总尺寸
total_height = sum(img.height for img in images)
max_width = max(img.width for img in images)
# 3. 创建长图并拼接
long_image = Image.new('RGB', (max_width, total_height), 'white')
y_offset = 0
for img in images:
long_image.paste(img, (0, y_offset))
y_offset += img.height
# 4. 保存长图
long_image.save(output_image, 'JPEG', quality=90)
print(f"生成成功: {output_image} ({len(images)}页)")
# 使用示例
pdf_to_long_image("document.pdf", "output.jpg")
安装依赖:
bash
pip install pdf2image pillow # 系统还需安装:poppler-utils
一句话总结:将 PDF 每页转为图片后垂直拼接成长图,适合移动端查看和社交媒体分享。