使用bash转换附加到PDF文件(Append to PDF file with convert in bash)

编程入门 行业动态 更新时间:2024-10-28 06:33:24
使用bash转换附加到PDF文件(Append to PDF file with convert in bash)

我基本上是使用wget从网站下载一些图像,然后使用命令行程序“convert”将它们附加到PDF文件中。 但最后这件事似乎不起作用。

我正在获取所有.jpg图像并将它们存储到一个文件夹中没有任何问题,但是当我尝试将它们合并到PDF文件中时,它总是提醒最后添加的图像。 我已经阅读了convert的-append参数,但它仍然无效。

这就是我的代码的样子:

for file in *.jpg do convert "${file}" -append "myfile.pdf" done

但是看起来很合乎逻辑, myfile.pdf总是最后只有最后一个jpg附加图像。 我知道使用转换像:

convert img1.jpg img2.jpg img3.jpg myfile.pdf

会做的伎俩。 但由于我不知道下载目录中有多少图像,我无法对参数进行硬编码,所以我想在我尝试的目录中为每个图像设置一个循环是最好的解决方案。

有人知道如何实现我的目标吗? 任何帮助都感激不尽。

提前致谢。

I'm basically downloading some images from a website using wget to then append them into a PDF file using the command line program "convert". But this last thing seems not to work.

I'm getting all the .jpg images and storing them into one folder with no problems, but when I try to merge them into the PDF file, it always reminds with the last appended image. I've read of the convert's -append argument, but it still won't work.

This is how my code looks like:

for file in *.jpg do convert "${file}" -append "myfile.pdf" done

But as logical as it seems, myfile.pdf always ends up having only the last jpg appended image. I know that using convert like:

convert img1.jpg img2.jpg img3.jpg myfile.pdf

Would do the trick. But as I don't know how many images will I have in the download directory, I cannot hardcode the arguments, so I guess a loop for each image in that directory as I'm trying would be the best solution.

Does anybody know how to achieve my goal? Any help will be much appreciated.

Thanks in advance.

最满意答案

bash自动扩展通配符参数(除非它们被引用或转义),所以即使convert不支持通配符扩展, bash也可以。 所以你可以做到

convert *.jpg myfile.pdf

请注意,如果文件太多,可能会导致“arglist太长”。 但是对于几百个文件来说应该没问题。

bash automatically expands wildcard arguments (unless if they are quoted or escaped) so even if convert does not support wildcard expansion, bash does. So you could just do

convert *.jpg myfile.pdf

note that if there are too many files, this can result with "arglist too long". But that should be OK for several hundred files.

更多推荐

本文发布于:2023-08-07 22:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   PDF   bash   convert   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!