是否可以使用Ruby将一系列PDF合并为一个? [英] Is it possible to combine a series of PDFs into one using Ruby?

查看:63
本文介绍了是否可以使用Ruby将一系列PDF合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列依次命名的PDF,如下所示:

I have a series of PDFs named sequentially like so:

  • 01_foo.pdf
  • 02_bar.pdf
  • 03_baz.pdf

使用Ruby,是否有可能在将它们按顺序排列的同时将它们组合成一个大PDF?我不介意安装任何必要的宝石来完成这项工作.

Using Ruby, is it possible to combine these into one big PDF while keeping them in sequence? I don't mind installing any necessary gems to do the job.

如果在Ruby中无法做到这一点,那么另一种语言呢?如果可能的话,没有商业组件.

If this isn't possible in Ruby, how about another language? No commercial components, if possible.

更新:

Update: Jason Navarrete's suggestion lead to the perfect solution:

将需要合并的PDF文件与 pdftk 一起放置在目录中(或确保pdftk在您的PATH中),然后运行以下脚本:

Place the PDF files needing to be combined in a directory along with pdftk (or make sure pdftk is in your PATH), then run the following script:

pdfs = Dir["[0-9][0-9]_*"].sort.join(" ")
`pdftk #{pdfs} output combined.pdf`

或者我甚至可以在命令行中以单行代码的方式完成此操作:

Or I could even do it as a one-liner from the command-line:

ruby -e '`pdftk #{Dir["[0-9][0-9]_*"].sort.join(" ")} output combined.pdf`'

很棒的建议杰森,完美的解决方案,谢谢. 给他一个投票赞成的人.

Great suggestion Jason, perfect solution, thanks. Give him an up-vote people.

推荐答案

A Ruby-Talk 帖子建议使用 pdftk 工具包合并PDF.

A Ruby-Talk post suggests using the pdftk toolkit to merge the PDFs.

pdftk 作为外部过程进行调用并使其处理合并应该相对简单. PDF :: Writer 可能会过大,因为您要完成的只是一个简单的附加操作.

It should be relatively straightforward to call pdftk as an external process and have it handle the merging. PDF::Writer may be overkill because all you're looking to accomplish is a simple append.

这篇关于是否可以使用Ruby将一系列PDF合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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