Reputation: 31
I try to output the 1st page of pdf to png using “pdf_convert” function present in pdftools-library. I get the png but the output file name having "image(page number).png". how to get the output file exactly same to the input file name Pdf name:- beer&cider_2bay_x_4shelf_londis_cluster1.pdf Png name:- beer&cider_2bay_x_4shelf_londis_cluster1_1.png
Upvotes: 0
Views: 804
Reputation: 128
Adapt this code for your needs to extract all images needed from a pdf to a specific folder in png
with the same name as the original pdf file:
library(pdftools)
library(glue)
library(tidyverse)
pdf_file <- "your_pdf.pdf"
output_folder <- "./output"
pdf_info <- pdf_info(pdf_file) # to get number of pages
file_name <- str_remove(string = basename(pdf_file), pattern = '\\.pdf' # basename of the file without extension
folder_name <- glue("{output_folder}/{file_name}")
full_names <- glue("{folder_name}_{1:pdf_info$pages}.png")
pdf_convert(pdf = pdf_file, format = "png", pages = 1:pdf_info$pages, filenames = full_names)
Upvotes: 0
Reputation: 39
The 'pdftools' package info is avaliable at https://docs.ropensci.org/pdftools and https://github.com/ropensci/pdftools#readme.
library(pdftools)
pdf_convert("Some/file/path/filename.pdf",
format = "png",
pages = 1, # use page number
filenames = "filename" # Set output file name
)
This will generate the file in your working directory.
Save the [png] to a specific folder using full file path or here::here()
Upvotes: 1