如何批量转换图像为PDF?

我想批量转换图像(jpg,png等)为PDF。 将它们直接转换为PDF很容易:

convert in.jpg out.pdf 

但是我需要更多选项,例如在横向和纵向格式之间设置输出页面大小,边距和旋转。 经过一些试验和错误,我想出了:

 convert -rotate "90>" -page A4+0+0 -gravity center in.jpg out.pdf 

这使图像在A4页面上居中并在横向和纵向之间自动旋转,但它仅适用于595×842以下的小图像。 较大的图像中断,因为595×842似乎是分配给A4页面的像素分辨率。 在网上阅读, -density选项可能是增加A4页面上像素数的潜在解决方案,但我无法使其工作。

Imagemagick以外的解决方案当然也受到欢迎。

一种解决方法是拆分图像生成和PDF转换。 首先通过convert为A4 @ 300dpi(即3508×2479)转换图像,然后使用sam2p将它们转换为PDF,然后使用sam2p_pdf_scale将它们转换为A4。

 convert -rotate "90>" -scale 3508x2479 -border 64x64 -bordercolor white in.png out.png sam2p out.png out.pdf sam2p_pdf_scale 595 842 out.pdf 

编辑:更完整的脚本:

 #!/bin/sh A4_WIDTH=2479 A4_HEIGHT=3508 H_MARGIN=64 V_MARGIN=64 WIDTH=$((${A4_WIDTH} - ${H_MARGIN} * 2)) HEIGHT=$((${A4_HEIGHT} - ${V_MARGIN} * 2)) for i in "$@"; do TMP="/tmp/$(uuidgen).png" echo "$i" convert \ -rotate "90>" \ -scale "${WIDTH}x${HEIGHT}" \ -border "${H_MARGIN}x${V_MARGIN}" -bordercolor white \ -gravity center \ -extent "${A4_WIDTH}x${A4_HEIGHT}" \ -gravity center \ -font helvetica -pointsize 80 \ -fill white -draw \ "push graphic-context translate $((${A4_WIDTH}/2 - 160)), 0 rotate 90 text -2,-2 '$i' text -2,2 '$i' text 2,-2 '$i' text 2,2 '$i' pop graphic-context " \ -fill black -draw \ "push graphic-context translate $((${A4_WIDTH}/2 - 160)), 0 rotate 90 text 0,0 '$i' pop graphic-context " \ "$i" "$TMP" sam2p "$TMP" "${i}.pdf" sam2p_pdf_scale 595 842 "${i}.pdf" done # EOF # 

比其他答案更清洁:

 ls *.jpg | sed -r 's/(.*)\.(\w{3,4})/\1.\2 \1.pdf/' | xargs -n2 sam2p 2>&1 | grep OutputFile | perl -pe 's/.*: //' | xargs pdfjoin --outfile out.pdf 

请在http://convertjpgpdf.net上查看。