Search code examples
rpdfpdftools

r pdftools: Combine multiple pages into a single page


The pdf_combine function from package can be used to combine different pdf documents.

pdftools::pdf_combine(
          input    = list(
                            "Page1.pdf"
                          , "Page2.pdf"
                          , "Page3.pdf"
                          , "Page4.pdf"
                          )
        , output   = "Pages1234.pdf"
        , password = ""
        )

Wondering if there is a way to combine these four pages into a single page something like Print multiple pages per sheet.


Solution

  • You can do this with the magick package...

    library(magick)
    files <- list.files(pattern = "\\.pdf")          #get pdf filenames
    pdfs <- Reduce(c, lapply(files, image_read_pdf)) #read in and combine
    montage <- image_montage(pdfs, tile = '2x2', geometry = "x1200") #create pages of 4
    image_write(montage, format = "pdf", "pages1234.pdf") #save as single pdf
    

    This works with any number of pdfs, outputting them in pages four to a page. You might want to play with the settings to get the margins, dimensions or quality to your liking.