pdf_apply() renders each page of a pdf as a raster image and
on selected pages additionally calls bm_fn() to modify those raster images
as well as grid_fn overlay custom graphics on top.
Arguments
- input
Input pdf filename.
- output
Output pdf filename.
NULLdefaults totempfile(fileext = ".pdf").- ...
Ignored.
- pages
A positive numeric vector of pages to include, a negative numeric vector of pages to exclude, or a string:
- all
Include all the pages.
- even
Include just the even pages.
- odd
Include just the odd pages.
The functions
pdf_pages()andpdf_subset()also support the string:- 2-up saddle stitch
The order of the pages to create a saddle-stitch booklet if printing 2-up.
- interleave
Interleave the first and second halves of the pages. Requires an even number of pages.
- interleave_first
Repeat the first page
n - 1times and interleave with the remainingn - 1pages.- interleave_last
Repeat the last page
n - 1times and interleave with the firstn - 1pages.
- rasterize, rasterise
If
TRUErasterize the original content usingpdftools::pdf_render_page(). IfFALSEdon't rasterize the original content and throw an error if a requested feature can not yet be implemented without rasterization. IfNULLonly rasterize if a requested feature requires it. Currently requires rasterization in the following cases:(
!is.null(paper)orscale != 1) and ghostscript is unavailable (seepdf_gs())any(pages != "all")and!identical(grid_fn, grid::grid.null)!identical(bm_fn, identity)
rasteriseis an alias forrasterize.- dpi
Dots per inch. Passed to
pdftools::pdf_render_page()when we rasterize the original content. Defaults togetOption("pnpmisc.dpi", 300).- paper
If
NULL(default) the output pdf will be the same size as the input pdf. Otherwise the output pdf will be created with this paper size (seepdf_pad_paper()for supported values but most commonly "letter" or "a4").- bg
outputpdf background color.- scale
A numeric scalar to scale the content by. Values less than 1 shrink the content within the page; values greater than 1 enlarge it. By default the original content is not rescaled. Note this also adjusts the size of the
grid::viewport()thegrid_fnwill draw in.- bm_fn
A one-argument function called to edit the selected pages rasterized. The input will be a "raster" object and the output must be an object coercible to a "raster" object by
grDevices::as.raster()- grid_fn
A zero-argument function called to draw graphics on selected pages. Defaults to
grid::grid.null().
Details
The original pdf document may be rasterized depending on the value of
bm_fn,pages,paper,scale, and/orrasterizeas well as whether ghostscript is available (seepdf_gs()).
Examples
f1 <- pdf_create_blank(length = 2L, paper = "letter", bg = "magenta")
if (requireNamespace("bittermelon", quietly = TRUE)) {
f2 <- pdf_apply(f1, pages = "even", dpi = 75,
bm_fn = bittermelon::bm_invert,
grid_fn = function() {
grid::grid.text("DRAFT", gp = grid::gpar(col = "red",
fontsize = 72, alpha = 0.3))
})
unlink(f2)
}
#> The original pdf contents were rasterized.
#> ℹ `any(pages != "all")` but `qpdf::pdf_overlay_stamp()` does not support a `pages` argument as of v1.4.1.
#> ℹ We needed to edit the contents with a bitmap function.`
#> ℹ Suppress this message with `suppressMessages(expr, classes = "pnpmisc_message_rasterized")`.
unlink(f1)