Saturday, September 23, 2023
 Popular · Latest · Hot · Upcoming
rated 0 times [  9] [ 0]  / answers: 1 / hits: 8768  / 1 Year ago, mon, april 25, 2022, 3:15:15

I want to batch convert images (jpg, png, etc.) to PDF. Converting them directly to PDF is easy with:

convert in.jpg out.pdf

However I need some more options, such as setting the output page size, margins and rotation between landscape and portrait format. After some trial and error I have come up with:

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

This centers the image on an A4 page and automatically rotates between landscape and portrait, however it only works with small images below 595x842. Larger images break, as 595x842 seems to be the resolution in pixel that is assigned to an A4 page. Reading around on the net, -density option might be a potential solution to increase the pixel count on an A4 page, but I couldn't make it work.

Solutions outside of Imagemagick are of course welcome as well.

More From » pdf


One workaround is to split the image generation and the PDF conversion. First convert the images via convert to A4@300dpi (i.e. 3508x2479), then use sam2p to convert them to PDF and then use sam2p_pdf_scale to convert them to 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

Edit: A more complete script:



WIDTH=$((${A4_WIDTH} - ${H_MARGIN} * 2))
HEIGHT=$((${A4_HEIGHT} - ${V_MARGIN} * 2))

for i in "$@"; do
echo "$i"
-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"

# EOF #

[#43464] Tuesday, April 26, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 129
Total Questions: 125
Total Answers: 127

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
9 Months ago