R将pdf合并为一个pdf [英] R merge pdf into one pdf

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

问题描述

我有一个文件夹,其中包含约400个.pdf文件,每个.pdf文件共2页.

I have a folder which contains around 400 .pdf files, each .pdf file is 2 pages total.

所有.pdf文件名均采用以下格式:

All .pdf file names are under the following format:

148-c16631-27-02-2018.pdf

我想要合并到一个pdf文件中,该pdf文件在R中的文件名中具有特定代码.例如:

What I would like is to merge under one pdf file, the pdf files that have a certain code in the file name in R. For instance:

148-c16631-27-02-2018.pdf
148-c14369-27-02-2018.pdf
148-c101593-27-02-2018.pdf

这些具有唯一的代码148,可以合并为1个pdf文件(共8页).

These have the code 148 which is unique, to be merged into 1 pdf file with 8 pages.

在此先感谢您的输入.

推荐答案

安装 pdftk 并确保它在您的路径上(或者,如果不在路径上,请在下面的system命令中引用它时使用完整路径名).然后运行下面的代码.不使用任何软件包.

Install pdftk and ensure it is on your path (or if not on your path use the full pathname when referring to it in the system command below). Then run the code below. No packages are used.

setwd("...directory where pdf files are located...") ##
infiles <- Sys.glob("148-*.pdf") ##
outfile <- "148.pdf" ##
system(paste("pdftk", paste(infiles, collapse = " "), "cat output", outfile))

有些软件包可以为pdftk提供包装:

There are some packages that provide wrappers around pdftk:

  • staplr软件包.不幸的是,它在这里可能不太有用,因为它不允许指定文件或它们在输出中的顺序-一个人只能指定输入和输出目录. **更新**当前版本的staplr允许按照注释中的说明指定文件.

  • the staplr package. Unfortunately, it is probably not too useful here because it does not allow specification of the files or their order in the output -- one can only specify the input and output directories. ** Update ** The current version of staplr now allows specification of the files as mentioned in the comments.

动画包.与直接使用systempdftk相比,此软件包提供的pdftk命令是​​一个稍微简单的选择.为了进行串联,我们假设上面标记为##的3行已经运行.

the animation package. The pdftk command that this package provides is a slightly simpler alternative than using system with pdftk directly. For concatenating it would be the following where we assume that the 3 lines marked ## above have already been run.

library(animation)
ani.options(pdftk = "/path/to/pdftk") # or if on path: ani.options(pdftk = "pdftk")
pdftk(infiles, "cat", outfile, "")

链接举例说明了如何使用带有pdftk的动画包来分页.

This link has an example of using the animation package with pdftk to burst pages.

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

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