R batch processing of PDF files--PNG/JPG files

When doing R drawing, we usually first consider images in PDF format, which can be modified and edited. However, when we publish articles, we need to use high-definition PNG or JPG files. Do every image need to be converted with software? --It's really a waste of energy. The blogger here recommends a package called pdftools, which can be used to convert PDF files~~

Without further ado, let’s get into the code

rm(list = ls())
library(tidyverse)
library(pdftools)

setwd("D:/R.result/2.He/ww2023.9019_merge/picture") 

fileName <- dir()
dir.create("results")
pdf_list <- list()
n = 0
for(i in 1:length(fileName)){
    if(str_detect(fileName[[i]],".pdf")){
        n = n +1
        
        pdf_list[[n]] <- pdf_render_page(fileName[[i]],
                                         page = 1,
                                         dpi = 400)
        jpeg::writeJPEG(pdf_list[[n]],
                      target = paste0("results/",n,".jpg"))
    }
}
 

dpi is the picture definition, you can adjust it. This code converts it into a JPG file or a PNG file. Change the code:

        jpeg::writeJPEG(pdf_list[[n]],
                      target = paste0("results/",n,".jpg"))

become:

png::writeJPEG(pdf_list[[n]],
                      target = paste0("results/",n,".png"))

That’s it. The output results at this time are in the results folder. You can change it to the folder name you need.

It's quite clear, no matter whether you zoom in or out, okay~ This time I will analyze the code here, I hope it will be helpful to everyone.

Guess you like

Origin blog.csdn.net/Queen_yu/article/details/133168280