0

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

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • What have you already tried? can you just do `pdf_convert("beer&cider_2bay_x_4shelf_londis_cluster1"` – Daniel_j_iii Jul 16 '20 at 16:37
  • the pdf contains more than 1 page hence the output file name will be like image(page number).png (original name with an extension of page number and .png.) so if I use the above code it will convert all my pdf pages into png and filename having pages number too. and I don't want that...Just wish to convert 1st page and with the same name of input file name – piya ingole Jul 16 '20 at 17:00
  • `pdf_convert("beer&cider_2bay_x_4shelf_londis_cluster1.pdf", pages = 1:1)` without screenshots or example outputs I can't help anymore I apologize, I haven't used pdftools before – Daniel_j_iii Jul 16 '20 at 19:54

2 Answers2

1

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()

iknow
  • 8,358
  • 12
  • 41
  • 68
0

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)
avidalvi
  • 128
  • 1
  • 6