Skip to contents

CRAN Status Badge R-CMD-check Coverage Status

Overview

pnpmisc is an R package to edit pdf files. Includes functions to resize, scale, rotate, and combine PDF pages, add crop marks, crosshairs, and origami fold guides, and create plastic storage box jackets and origami card wallets. Also provides layout functions for splitting pages into individual component images and reassembling them.

The pdf editing functions in this package have the following conventions which allow them to be chained:

  • The input filename is the first positional argument.
  • The output filename is the second positional argument and if unspecified defaults to tempfile(fileext=".pdf").
  • All other arguments must be named.
  • Returns the output filename invisibly.

While some of the pdf editing functions are of general use the motivating goal of this package is to help me tweak existing print-and-play files to a format a little bit easier for me to physically manufacture using my preferred techniques and tools.

My personal print-and-play preferences:

  • I prefer crop marks and/or cross hairs instead of full cut lines. Cross hairs should only be on one side of a duplex print.
  • I prefer my pdf files to be letter sized.
  • I prefer to print cards duplex. To forgive a bit of front-back printing drift there should be enough bleed around the cards and any cross hairs should just be on one side.
  • I prefer when folding to know where I should line up the (trimmed) paper edge.

Installation

You can install the development version using my R-universe universe:

install.packages('pnpmisc', repos = c('https://trevorld.r-universe.dev', 'https://cloud.r-project.org'))

or by using the remotes package:

remotes::install_github("trevorld/pnpmisc")

Examples

bm_split_layout() and grid_add_layout()

  • The provided rules for Button Shy games are formatted as a mini-zine which is great for printing and making into a concise booklet but it is hard to read the rules when looking at them on the computer.
  • You can use functions like bm_split_layout() and grid_add_layout() to create a pdf of the booklet pages all oriented up.
library("bittermelon")
library("pnpmisc")
# Download "A Nice Cuppa"
# <https://buttonshygames.itch.io/a-nice-cuppa>
input <- "A Nice Cuppa - PNP.pdf"
output <- "a_nice_cuppa_rules.pdf"
layout <- layout_preset("button_shy_rules")
bml <- input |> pdf_render_bm_pixmap() |>
    bm_split_layout(layout = layout)

layout <- transform(layout, angle = 0, name = paste0("page_", 1:8))
pdf(output, width = 11, height = 8.5)
grid_add_layout(bml, layout = layout)
grid_add_rects(layout = layout, gp = grid::gpar(lwd = 4))
dev.off()

pdf_add_crosshairs()

  • The old rotary trimmer I use to cut cards doesn’t let me cut only the interior of a laminated sheet of cards so it is nice if one side has crosshairs at the corner of each card to help guide cuts after the outside crop marks have been cut away.
  • pdf_add_crosshairs() adds such crosshairs to the corners of the cards on the indicated pages.
library("piecepackr")
library("pnpmisc")
# Download "A Nice Cuppa"
# <https://buttonshygames.itch.io/a-nice-cuppa>
input <- "A Nice Cuppa - PNP.pdf"
output <- "a_nice_cuppa_cards.pdf"
input |> pdf_subset(pages = -1L) |>
    pdf_add_crosshairs(output, layout = "button_shy_cards", pages = "even")
rm_temp_pdfs()

pdf_add_rects()

  • Border lines don’t look very clean when manually cutting cards out. Instead I’d prefer no border lines and instead some crosshairs on the card corners of one side (which will get cleanly removed after cutting the corners off).
  • If the PnP layout has a white background you sometimes can “remove” unwanted border lines by covering them up with new white border lines.
library("piecepackr")
library("pnpmisc")
# Download Mini Rogue (Original)
# <https://boardgamegeek.com/boardgame/199242/mini-rogue-a-roguelike-microgame/files>
input <- "Mini_Rogue_-_Cards_v1.2.1.pdf"
output <- "mini_rogue_cards.pdf"
input |> pdf_subset(pages = 1:2) |>
    pdf_add_rects(layout = "poker_3x3",
                  gp = grid::gpar(fill = NA, col = "white", lwd = 2)) |>
    pdf_add_crosshairs(output, pages = 2L, layout = "poker_3x3")
rm_temp_pdfs()

pdf_add_origami()

library("pnpmisc")
# Download Boardgame Barrio Small Box Game Jackets from
# <https://sites.google.com/view/boardgamebarrio/home>
input <- "SBG_Jacket_-_Animal_Upon_Animal.pdf"
output <- "animal_upon_animal_jacket.pdf"
input |> pdf_gs() |>
    pdf_subset(pages = 1L) |>
    pdf_add_origami(output)
rm_temp_pdfs()

pdf_create_wallet()

  • I like to store my smaller print-and-play card games (i.e. up to 18 poker-sized cards plus rulebook) in an origami card wallet.
  • pdf_create_wallet() can create a customizable print-and-play layout for an origami card wallet.
library("bittermelon")
library("grid")
library("gridpattern")
library("pnpmisc")
# Download "Numbsters"
# <https://buttonshygames.itch.io/numbsters>
input <- "Numbsters_PNP_letter_v1.2.pdf"
output <- "numbsters_wallet.pdf"
bm_cover <- pdf_render_bm_pixmap(input, page = 1L) |>
  bm_crop_layout(layout = "button_shy_rules", row = 1L, col = 4L)
bm_back <- pdf_render_bm_pixmap(input, page = 1L) |>
  bm_crop_layout(layout = "button_shy_rules", row = 1L, col = 3L)
bm_mouths <- pdf_render_bm_pixmap(input, page = 3L) |>
  bm_crop_layout(layout = "button_shy_cards", row = 1L, col = 2L, bleed = TRUE) |>
  bm_trim(bottom = 100, top = 100, left = 100, right = 100)

pat <- rasterGrob(bm_mouths, height = unit(8.25, "in"))
front <- gList(pat, rasterGrob(bm_cover, width = unit(1.8, "in")))
back <- gList(pat, rasterGrob(bm_back, width = unit(1.8, "in")))
spine <- rasterGrob(bm_rotate(bm_mouths, 90), width = unit(8.25, "in"))
pdf_create_wallet(output, front = front, back = back, spine = spine, bleed = 0.125)

pdf_pad_paper()

  • Since “letter” sized paper is shorter but wider than “A4” sized paper some print-and-play files have a weird shared page size that is a bit smaller than both “letter” and “A4” paper sizes.
  • pdf_pad_paper() pads the size of such files to reach the full letter or A4 paper size without scaling the original component images and making sure the original images stay centered in the page (e.g. for “duplex” printing).
library("pnpmisc")
# Download Birdscaping from <https://boardgamegeek.com/boardgame/366484/birdscaping/files>
input <- list.files(pattern = "^Birdscaping_1.7")
output <- "birdscaping_cards.pdf"
pdf_combine(input) |>
    pdf_pad_paper(output)

pdf_rm_crosshairs()

  • Since there may be some font-back printing drift when duplex printing cards I prefer only crosshairs on one side of the double-sided leaf so the both sides of the cards look clean after removing the corners.
  • pdf_rm_crosshairs() removes the crosshairs from a PnP layout with a solid color bleed.
library("pnpmisc")
# Download Galdor's Grip from <https://greggjewell.itch.io/galdors-grip>
input <- "GaldorsGrip_PnP_Cards_TGCBundle_EN_USL_v1.pdf"
output <- "galdors_grip_cards.pdf"
pdf_rm_crosshairs(input, output, pages = "odd", layout = "poker_3x2_bleed")

R packages

  • papersize has some functionality to create print-and-play playing card layouts.
  • pdftools, qpdf, staplr, and xmpdf are some pdf manipulation packages.
  • piecepackr has some graphical functionality for creating print-and-play layouts.
  • sbgjackets uses these functions to create jackets for small box games in a consistent style.