使用R合并现有的PDF文件 [英] Merging existing PDF files using R
问题描述
我想使用R合并已经存在(已经保存在我的计算机中)的PDF文件.
I want to merge PDF files that already exist (already saved in my computer) using R.
我已经尝试使用开源软件合并它们,并且效果很好,但是由于我有数百个文件可以合并在一起,所以我希望找到更快的东西(我的目标是自动创建文件-或通过运行R命令进行更新).
I already tried to use open source softwares to merge them and it works fine but since I have a couple hundreds of files to merge together, I was hoping to find something a little faster (my goal is to have the file automatically created - or updated, simply by running an R command).
我已经习惯了R,所以我想找到一种使用此程序创建这种新的多页PDF的方法.有什么功能可以帮我吗?
I am used to R so I would like to find a way to create this new multiple-sheet PDF using this program. Is there any function that could do that for me?
谢谢!
推荐答案
如果您安装pdftk
(找到此处),那么您可以使用以下功能:
If you install pdftk
(found here), then you can use the function below:
concatenate_pdfs <- function(input_filepaths, output_filepath) {
# Take the filepath arguments and format them for use in a system command
quoted_names <- paste0('"', input_filepaths, '"')
file_list <- paste(quoted_names, collapse = " ")
output_filepath <- paste0('"', output_filepath, '"')
# Construct a system command to pdftk
system_command <- paste("pdftk",
file_list,
"cat",
"output",
output_filepath,
sep = " ")
# Invoke the command
system(command = system_command)
}
可以这样称呼:
concatenate_pdfs(input_filepaths = c("My First File.pdf", "My Second File.pdf"),
output_filepath = "My Combined File.pdf")
这只是调用以下系统命令的一种用户友好方式:
This is just a user-friendly way of invoking the following system command:
pdftk "My First File.pdf" "My Second File.pdf" cat output "My Combined File.pdf"
这篇关于使用R合并现有的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!