Skip to content
Open
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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
^docs$
^pkgdown$
^man/figures/logo\.png$
img
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Description: Provides function to manipulate PDF files:
SystemRequirements: Java 8 or higher
License: GPL-3
LazyData: true
RoxygenNote: 7.1.1
RoxygenNote: 7.1.2
Imports:
tcltk,
stringr,
Expand Down
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(combine_pdf)
export(get_fields)
export(idenfity_form_fields)
export(identify_form_fields)
export(remove_pages)
export(rename_files)
export(rotate_pages)
Expand All @@ -11,6 +10,7 @@ export(select_pages)
export(set_fields)
export(split_from)
export(split_pdf)
export(staple_pages)
export(staple_pdf)
import(utils)
importFrom(assertthat,assert_that)
Expand Down
45 changes: 23 additions & 22 deletions R/combine_pdf.R → R/staple_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,40 @@
#' @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 input_files Vector with paths of PDF files to be combined.
#' @param output_filepath 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.
#' In the path informed in \code{output_filepath}, the PDF file resulting from the combination
#' of multiple files passed to \code{input_files} will be saved.
#' @author Pedro Rafael D. Marinho
#' @export
#' @examples
#'
#' \dontrun{
#' combine_pdf(
#' vec_input =
#' staple_pages(
#' input_files =
#' c(
#' "file_1.pdf",
#' "file_2.pdf",
#' ),
#' output = "output.pdf",
#' output_filepath = "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) {
staple_pages <- function(input_files, output_filepath = "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!")
if(length(start_pages) != length(input_files) || length(end_pages) != length(input_files))
stop("Start_pages and end_pages must be a vector of the same length as input_files!")

start_pages <-
flatten_dbl(
map_if(
purrr::flatten_dbl(
purrr::map_if(
.x = start_pages,
.f = function(x) 1,
.p = is.na
Expand All @@ -46,37 +47,37 @@ combine_pdf <- function(vec_input, output = "output.pdf", start_pages = NA, end_
f <- function(x, y)
ifelse(
is.na(x),
pdf_length(vec_input[y]),
pdftools::pdf_length(input_files[y]),
x
)

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

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

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

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

pdf_combine(input = files, output = output)
pdftools::pdf_combine(input = files, output = output_filepath)
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
output: github_document
---

# staplr <img src="man/figures/logo.png" align="right" height="150"/>
# staplr <img src="logo.png" align="right" height="210"/>

[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)
[![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html)
Expand Down
46 changes: 21 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# staplr <img src="man/figures/logo.png" align="right" height="150"/>
# staplr <img src="logo.png" align="right" height="210"/>

[![Project Status: Active - The project has reached a stable, usable
state and is being actively
Expand All @@ -8,31 +8,31 @@ developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repo
[![Build
Status](https://travis-ci.org/pridiltal/staplr.svg?branch=master)](https://travis-ci.org/pridiltal/staplr)

-----
------------------------------------------------------------------------

[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/staplr)](https://cran.r-project.org/web/packages/staplr/index.html)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/staplr)](https://cran.r-project.org/web/packages/staplr/index.html)
[![](http://cranlogs.r-pkg.org/badges/staplr)](http://cran.rstudio.com/web/packages/staplr/index.html)

-----
------------------------------------------------------------------------

[![Last-changedate](https://img.shields.io/badge/last%20change-2020--07--21-yellowgreen.svg)](/commits/master)
[![Last-changedate](https://img.shields.io/badge/last%20change-2021--09--27-yellowgreen.svg)](/commits/master)

<!-- README.md is generated from README.Rmd. Please edit that file -->

# staplr

This package provides functions to manipulate PDF files:

- fill out PDF forms: get\_fields() and set\_fields()
- merge multiple PDF files into one: staple\_pdf()
- remove selected pages from a file: remove\_pages()
- rename multiple files in a directory: rename\_files()
- rotate entire pdf document: rotate\_pdf()
- rotate selected pages of a pdf file: rotate\_pages()
- Select pages from a file: select\_pages()
- splits single input PDF document into individual pages: split\_pdf()
- splits single input PDF document into parts from given points:
split\_from()
- fill out PDF forms: get_fields() and set_fields()
- merge multiple PDF files into one: staple_pdf()
- remove selected pages from a file: remove_pages()
- rename multiple files in a directory: rename_files()
- rotate entire pdf document: rotate_pdf()
- rotate selected pages of a pdf file: rotate_pages()
- Select pages from a file: select_pages()
- splits single input PDF document into individual pages: split_pdf()
- splits single input PDF document into parts from given points:
split_from()

This package is still under development and this repository contains a
development version of the R package *staplr*.
Expand Down Expand Up @@ -107,46 +107,42 @@ set_fields(pdfFile, 'newFile.pdf', fields)

## Troubleshooting and 2.11.0 changes

- As of version 2.11.0, the package uses
- As of version 2.11.0, the package uses
[pdftk-java](https://gitlab.com/pdftk-java/pdftk) instead of using
the original
[pdftk](https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).
`pdftk-java` is included with the package so if you have a working
java installation, you shouldn’t have any problems.

- While default java options should be enough for most use cases, if
- While default java options should be enough for most use cases, if
you need to, you can change java options that is used to run pdftk
by doing

<!-- end list -->

``` r
options('staplr_java_options' = '-Xmx512m')
```

This option is not affected by `rJava` settings.

- If you don’t have a working java installation, your installation
- If you don’t have a working java installation, your installation
will fail since you can’t install rJava. Make sure you follow the
proper instructions for java installation. For openJDK on linux make
sure you get both jdk and jre and run javareconf.

<!-- end list -->
<!-- -->

sudo apt update -y
sudo apt install -y openjdk-8-jdk openjdk-8-jre
sudo R CMD javareconf

Also restart your R session after `javareconf`

- `pdftk-java` is built as a faithful representation of the original
- `pdftk-java` is built as a faithful representation of the original
`pdftk` so there shouldn’t be any major differences between the
outputs. However, for any reason you’d prefer to run a local
installation of pdftk rather than using the version that is shipped
with the package, do

<!-- end list -->

``` r
# set staplr_custom_pdftk to the path to local installation
# just setting to pdftk will do if it's already in your path
Expand All @@ -164,4 +160,4 @@ in pdftk later.

## References

- <https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/>
- <https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/>
6 changes: 6 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
destination: docs
opengraph:
image:
src: "logo.png"
template:
params:
bootswatch: united
10 changes: 5 additions & 5 deletions docs/404.html

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

Binary file modified docs/apple-touch-icon-120x120.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/apple-touch-icon-152x152.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/apple-touch-icon-180x180.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/apple-touch-icon-60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/apple-touch-icon-76x76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 9 additions & 5 deletions docs/authors.html

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

Binary file modified docs/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/favicon.ico
Binary file not shown.
Loading