Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Package: staplr
Type: Package
Title: A Toolkit for PDF Files
Version: 3.1.1
Version: 3.2.0
Depends: R (>= 3.4.0)
Authors@R: c(
person("Priyanga Dilini", "Talagala", email="pritalagala@gmail.com", role= c("aut","cre")),
person("Ogan", "Mancarci", email="ogan.mancarci@gmail.com", role='aut'),
person("Daniel", "Padfield", email= "d.padfield@exeter.ac.uk", role ='aut'),
person("Granville", "Matheson", email= "mathesong@gmail.com", role ='aut')
person("Granville", "Matheson", email= "mathesong@gmail.com", role ='aut'),
person("Pedro Rafael", "D. Marinho", email= "pedro.rafael.marinho@gmail.com", role ='ctb', comment = c(ORCID = "0000-0003-1591-8300"))
)
Description: Provides function to manipulate PDF files:
fill out PDF forms;
Expand All @@ -30,10 +31,12 @@ Imports:
assertthat,
glue,
XML,
rJava
rJava,
fs,
purrr,
pdftools
Suggests:
lattice,
testthat,
pdftools
testthat
Encoding: UTF-8
BugReports: https://github.com/pridiltal/staplr/issues
13 changes: 13 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(combine_pdf)
export(get_fields)
export(idenfity_form_fields)
export(remove_pages)
Expand All @@ -13,5 +14,17 @@ export(split_pdf)
export(staple_pdf)
import(utils)
importFrom(assertthat,assert_that)
importFrom(fs,file_temp)
importFrom(fs,file_temp_push)
importFrom(fs,path_ext_remove)
importFrom(fs,path_file)
importFrom(glue,glue)
importFrom(pdftools,pdf_combine)
importFrom(pdftools,pdf_length)
importFrom(pdftools,pdf_subset)
importFrom(purrr,flatten_dbl)
importFrom(purrr,map2_dbl)
importFrom(purrr,map_if)
importFrom(purrr,walk2)
importFrom(stringr,str_extract)
importFrom(tcltk,tk_choose.dir)
82 changes: 82 additions & 0 deletions R/combine_pdf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#' Combine multiple PDF files
#' @description Combine multiple PDF files by delimiting the sequences of pages in each file.
#' @importFrom glue glue
#' @importFrom pdftools pdf_subset pdf_combine pdf_length
#' @importFrom purrr map2_dbl walk2 flatten_dbl map_if
#' @importFrom fs file_temp file_temp_push path_ext_remove path_file
#' @param vec_input Vector with paths of PDF files to be combined.
#' @param output PDF file path result of the combination.
#' @param start_pages Vector with the initial pages of each file. If \code{NA},
#' the default, will be considered the first page.
#' @param end_pages Vector with the final pages of each file. If \code{NA}, the
#' default, will be considered the last page.
#' @return
#' In the path informed in \code{output}, the PDF file resulting from the combination
#' of multiple files passed to \code{vec_output} will be saved.
#' @export
#' @examples
#'
#' \dontrun{
#' combine_pdf(
#' vec_input =
#' c(
#' "file_1.pdf",
#' "file_2.pdf",
#' ),
#' output = "output.pdf",
#' start_pages = c(NA, NA),
#' end_pages = c(NA, NA)
#' )
#' }

combine_pdf <- function(vec_input, output = "output.pdf", start_pages = NA, end_pages = NA) {

if(length(start_pages) != length(vec_input) || length(end_pages) != length(vec_input))
stop("Start_pages and end_pages must be a vector of the same length as vec_input!")

start_pages <-
flatten_dbl(
map_if(
.x = start_pages,
.f = function(x) 1,
.p = is.na
)
)

f <- function(x, y)
ifelse(
is.na(x),
pdf_length(vec_input[y]),
x
)

end_pages <-
purrr::map2_dbl(
.x = end_pages,
.y = seq_along(vec_input),
.f = f
)

files <-
file_temp_push(
glue(
"{tempdir()}/{path_ext_remove(path_file(output))}_{1L:length(vec_input)}.pdf"
)
)

one_step <- function(x, y) {
pdf_subset(
input = x,
output = file_temp(),
pages = start_pages[y]:end_pages[y]
)
}

walk2(
.x = vec_input,
.y = 1L:length(vec_input),
.f = ~ one_step(.x, .y)
)

pdf_combine(input = files, output = output)
}
41 changes: 41 additions & 0 deletions man/combine_pdf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.