Nothing
#' Remove whitespace margins from a PDF file (and maybe embed fonts)
#'
#' Remove whitespace margins using <https://ctan.org/pkg/pdfcrop> and
#' optionally embed fonts using [grDevices::embedFonts()]. You may install
#' `pdfcrop` using TinyTeX (<https://cran.r-project.org/package=tinytex>) with
#' `tinytex::tlmgr_install('pdfcrop')`.
#'
#' You may also wish to consider [extrafont::embed_fonts()]
#' (<https://cran.r-project.org/package=extrafont>).
#'
#' ```
#' library(extrafont)
#' # If you need to specify the path to Ghostscript (probably not needed in Linux)
#' Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.56.1/bin/gswin64c.exe")
#' embed_fonts("original.pdf", outfile = "new.pdf")
#' ```
#'
#' As an alternative, saving the PDF with [grDevices::cairo_pdf()] should
#' already embed the fonts.
#'
#' @param filename (`character(1)`)\cr Filename of a PDF file to crop. The file will be overwritten.
#'
#' @param mustWork (`logical1`)\cr If `TRUE`, then give an error if the file cannot be cropped.
#'
#' @param pdfcrop (`character(1)`)\cr Path to the `pdfcrop` utility.
#'
#' @param embed_fonts (`logical(1)`)\cr If `TRUE`, use [grDevices::embedFonts()] to embed fonts.
#'
#' @return No return value, called for side effects
#'
#' @seealso [grDevices::embedFonts()] [extrafont::embed_fonts()] [grDevices::cairo_pdf()]
#'
#' @examples
#' extdata_path <- system.file(package = "moocore", "extdata")
#' A1 <- read_datasets(file.path(extdata_path, "wrots_l100w10_dat"))
#' A2 <- read_datasets(file.path(extdata_path, "wrots_l10w100_dat"))
#' filename <- tempfile("eafplot", fileext=".pdf")
#' pdf(file = filename, onefile = TRUE, width = 5, height = 4)
#' eafplot(list(A1 = A1, A2 = A2), percentiles = 50, sci.notation = TRUE)
#' dev.off()
#' try(pdf_crop(filename)) # This may fail if pdfcrop is not installed.
#'
#' @export
pdf_crop <- function(filename, mustWork = FALSE, pdfcrop = Sys.which("pdfcrop"),
embed_fonts = FALSE)
{
if (!file.exists(filename)) {
stop("PDF file", shQuote(filename), "not found")
} else if (!has_file_extension(filename, "pdf")) {
stop(shQuote(filename), "is not a PDF file")
} else if (is.null(pdfcrop) || pdfcrop == "") {
if (mustWork) {
stop("pdfcrop not found!")
} else {
warning("pdfcrop not found, not cropping")
}
} else {
system2(pdfcrop, c("--pdfversion 1.5", shQuote(filename), shQuote(filename)),
timeout = 60, stdout = FALSE, stderr = FALSE)
if (embed_fonts)
embedFonts(filename)
}
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.