2

I am trying to access a PDF through an HTTP post request with R Plumber, read it with the tabulizer package, and respond with the PDF in JSON format. I am posting a 53kb PDF through Postman to my route and receiving the error:

Error in normalizePath(path.expand(path), winslash, mustWork).

My R API route code is below:

#' @post /tab
#' @json
function(req){
  library("tabulizer")
  f <- req$postBody
  extract_tables(f)

}

When I use the extract_tables() function with a local path to the PDF that I am using it works perfectly as a get route.

#' @get /tab
#' @json
function(){
  library("tabulizer")
  f <- "C:/Users/kelse/Desktop/Rscripts/Tessaract/table.pdf"
  extract_tables(f)
}

Does anybody know how to post a pdf file through Plumber and access it in a function?

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

2

You can try by using @ serializer to get posted through HTTP

#* @serializer contentType list(type="application/pdf")
#* @get /pdf
function(){
  tmp <- tempfile()
  pdf(tmp)
  plot(1:10, type="b")
  text(4, 8, "PDF from plumber!")
  text(6, 2, paste("The time is", Sys.time()))
  dev.off()

  readBin(tmp, "raw", n=file.info(tmp)$size)
}
Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22