You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将固定尺寸的多张JPG合并为单页A4 PDF(Linux命令行方案)

Linux命令行实现多张JPG合并为单页A4 PDF(统一图片宽度9cm)

方法一:使用ImageMagick(简单直接,推荐)

ImageMagick是Linux下常用的批量图像处理工具,能快速完成格式转换与布局调整。

1. 安装ImageMagick

Debian/Ubuntu系:

sudo apt-get update && sudo apt-get install imagemagick

Fedora/RHEL系:

sudo dnf install imagemagick

2. 修复PDF写入权限(若报错)

部分系统默认限制ImageMagick生成PDF,需修改配置文件:
打开/etc/ImageMagick-6/policy.xml(或对应版本路径),找到:

修改为:

或直接注释该行。

3. 垂直排列图片到单页A4

执行命令将所有JPG垂直拼接后居中放置在A4页面,每张图片宽度固定为9cm:

convert *.jpg \
  -resize "9cmx" \          # 强制宽度9cm,高度按原比例自动适配
  -append \                 # 垂直拼接所有图片(水平拼接用+append)
  -background white \       # 设置画布背景为白色
  -extent 21cmx29.7cm \    # 设定画布为A4标准尺寸(21cm宽,29.7cm高)
  -gravity center \         # 将拼接后的图片居中放置
  output.pdf

4. 多列网格排列(例如每行2张)

若图片数量较多,用montage工具实现网格布局,A4宽度可容纳2张9cm图片(两侧各留1.5cm空隙):

montage *.jpg \
  -resize "9cmx" \          # 统一宽度为9cm
  -tile 2x \                # 每行放2张,行数自动计算
  -geometry +1.5cm+1.5cm \ # 图片间水平/垂直间距各1.5cm
  -page A4 \                # 指定输出页面为A4
  -background white \       # 背景设为白色
  -gravity center \         # 整体居中对齐
  output.pdf

方法二:使用pdfjam(基于LaTeX,布局更精准)

pdfjam依托LaTeX,适合对间距、对齐有严格要求的场景。

1. 安装pdfjam

Debian/Ubuntu系:

sudo apt-get install pdfjam

2. 批量转换JPG为固定宽度的单页PDF

先将每张JPG转为单独的PDF,确保宽度为9cm:

for img in *.jpg; do
  convert "$img" -resize "9cmx" "${img%.jpg}.pdf"
done

3. 合并为单页A4 PDF

垂直排列(1列)

# 统计图片数量
count=$(ls -1 *.pdf | wc -l)
# 合并为1列n行的单页A4 PDF
pdfjam --nup 1x$count \
       --paper a4paper \
       --no-landscape \
       --delta "0cm 1cm" \ # 设置行间距为1cm
       --outfile output.pdf \
       *.pdf

两列排列

count=$(ls -1 *.pdf | wc -l)
# 计算行数(向上取整)
rows=$(( (count + 1) / 2 ))
# 合并为2列rows行的单页A4 PDF
pdfjam --nup 2x$rows \
       --paper a4paper \
       --no-landscape \
       --delta "1.5cm 1cm" \ # 列间距1.5cm,行间距1cm
       --outfile output.pdf \
       *.pdf

内容的提问来源于stack exchange,提问作者user1583209

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:15:43