在Linux中将多个jpg合并为单个pdf [英] Merge multiple jpg into single pdf in Linux

查看:458
本文介绍了在Linux中将多个jpg合并为单个pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令将目录中的所有jpg文件转换并合并为单个pdf文件.

I used the following command to convert and merge all the jpg files in a directory to a single pdf file.

convert *.jpg file.pdf

目录中的文件从1.jpg123.jpg编号.转换进行得很好,但是在转换之后,页面都混合了.我希望pdf的页面从1.jpg123.jpg的命名顺序相同.我也尝试了以下命令:

The files in the directory are numbered from 1.jpg to 123.jpg. The convertion went fine but after converting the pages were all mixed up. I wanted the pdf to have pages from 1.jpg to 123.jpg in the same order as they are named. I tried with the following command as well:

cd 1 
FILES=$( find . -type f -name "*jpg" | cut -d/ -f 2)
mkdir temp && cd temp 
for file in $FILES; do 
    BASE=$(echo $file | sed 's/.jpg//g');
    convert ../$BASE.jpg $BASE.pdf; 
    done && 
pdftk *pdf cat output ../1.pdf && 
cd .. 
rm -rf temp

但是仍然没有运气.操作平台Linux.

But still no luck. Operating platform Linux.

推荐答案

问题是因为您的外壳程序以纯字母顺序扩展了通配符,并且由于数字的长度不同,因此顺序不正确:

The problem is because your shell is expanding the wildcard in a purely alphabetical order, and because the lengths of the numbers are different, the order will be incorrect:

$ echo *.jpg
1.jpg 10.jpg 100.jpg 101.jpg 102.jpg ...

解决方案是根据需要在文件名中添加零,以使它们具有相同的长度,然后再运行您的转换命令:

The solution is to pad the filenames with zeros as required so they're the same length before running your convert command:

$ for i in *.jpg; do num=`expr match "$i" '\([0-9]\+\).*'`;
> padded=`printf "%03d" $num`; mv -v "$i" "${i/$num/$padded}"; done

现在,文件将由通配符以正确的顺序进行匹配,并准备好执行convert命令:

Now the files will be matched by the wildcard in the correct order, ready for the convert command:

$ echo *.jpg
001.jpg 002.jpg 003.jpg 004.jpg 005.jpg 006.jpg 007.jpg 008.jpg ...

这篇关于在Linux中将多个jpg合并为单个pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆