rpdfmagick

Convert each page of a multi-paged pdf into separate png files in R


I have seen a few questions asked involving trying to convert a pdf into a png but none of the answers show how to save each page of a multi-paged pdf as a different png file.

Starting out with an example 13-page pdf:

# exmaple pdf 
example_pdf <- "https://arxiv.org/ftp/arxiv/papers/1312/1312.2789.pdf"

How can I save each page of the pdf as a different png file?


Solution

  • We can create a png of each page using the image_read_pdf function from the magick package:

    #install magick package
    install.packages("magick")
    library("magick")
    
    # creating magick-image class with a png for each page of the pdf
    pages <- magick::image_read_pdf(example_pdf)
    pages
    
    
    # saving each page of the pdf as a png
    j <- 1:13
    for (i in j){
    pages[i] %>% image_write(., path = paste0("image",i,".png"), format = "png")
    
    }  
    

    This would save each page as "image(page number).png" in your main directory file.