Search code examples
rpdfmagick

Save cropped pdf image from R imagemagick to a new pdf


I'm using R with imagemagick to crop some borders from a pdf file. I'm executing the following commands:

library(magick)

pdf_total <- image_read_pdf(path =  "file1.pdf")
pdf_cropped <- image_crop(pdf_total,"3000x1500")

After this process I have a perfect cropped file, but my problem occurs when I try to save the file to a new pdf file. What is the correct procedure to save this converted pdf?


Solution

  • My final solution is:

    library(magick)
    
    pdf_total <- image_read_pdf(path =  "file1.pdf")
    pdf_cropped <- image_crop(pdf_total,"3000x1500")
    
    
    for(i in seq(1,length(pdf_cropped))){
    
         plot(pdf_cropped[i])
    
    }
    
    dev.off()
    

    In this case I made a for loop to save all the pages, if you pass plot(pdf_cropped) the result is a pdf with a single page (first picture).