Home > Back-end >  space images in PDF using R
space images in PDF using R

Time:11-11

I'm using R to generate PDFs that contain QR code images. The images have to be arranged in a 4x6 pattern like this mockup that i made in Word:

enter image description here

I've written a loader that i plan to write into a loop later on (i've got over 2000 images). However, i can't quite get the spacing right. This is my current result:

enter image description here

It's not a bad thing that the images are smaller, but the large margins on top and bottom and right are a problem. I've been trying to set par(mar = c(4,0,0,0) in the loop to see if the images would get a bottom margin, but that doesn't happen. Also, i think i need to use padding instead of margin, but i haven't found a parameter for padding.

Is there a way to do this? The code i've written so far:

# import libraries
library(qrcode)
library(imager)

# clear workspace
rm(list = ls())

filenames <- list.files(path = "./QR/2/")

im <- load.image(paste0("./QR/2/",filenames[1]))

im <- list()
for(i in 1:24){
  im[[i]] <- load.image(paste0("./QR/2/",filenames[i]))
}

pdf("./QR/2/stickervellen/1.pdf", paper = "a4")
par(mfrow = c(6,4), mar=c(0,0,0,0))
for(i in 1:24){
  par(mar = c(4,0,0,0))
  plot(im[[i]], axes = F)
}
dev.off()

the images i'm using can be found here: QR codes

Please note that I added box() in your code so that the extent of the individual plots and the margins around them are visible.

In ?pdf you may also find that if you specify the paper argument

if either width or height is less than 0.1 or too large to give a total margin of 0.5 inch, it is reset to the corresponding paper dimension minus 0.5.

That's why there will always be a small margin around the whole page when you specify paper. If you don't want that margin, just leave out paper="a4" and you will get this:

QR codes without margin

  • Related