2

I have an API function that takes an input, obtains necessary data using that input, calls a .rmd file, and using that .rmd file produces a pdf file in a specified directory. I need the remaining part of my API to take the newly produced pdf file and have it posted through HTTP. However, whenever I try to do that, I end up getting a corrupt file as an output.

This is my current code:

#* @serializer contentType list(type="application/pdf")
#* parameter_selection
#* @param a1
#* @get /pdf

function( a1) {
  
  #Please assume that the filename location is correct. 

  filename = paste0( a1, '_','report','.pdf', sepp='')

  locale_filename = paste0('notebooks/',filename, sepp= '')

  readBin(locale_filename, "raw", n=file.info(locale_filename)$size)


}

Thank you.

Alokin
  • 461
  • 1
  • 4
  • 22
  • FYI, `sepp=''` is being ignored since it matches no arguments yet adds nothing; if you mean `sep=''`, it is not required when using `paste0`. In fact, if you try `paste0('a','b',sep=' ')`, you'll see that it doesn't recognize it as an argument so it adds the literal one-char string, as in `ab ` (there's a space there). – r2evans Jun 29 '21 at 19:36
  • Your code closely mimics https://www.rplumber.io/articles/rendering-output.html, and when I use that (in FF from R-4.0.5), it works. Are you certain that the file can be viewed outside of (before, not going through) your plumber environment? – r2evans Jun 29 '21 at 19:41
  • r2evans is right first. Then do you have this in your code ? ` res$setHeader("Content-Disposition", paste0("attachment; filename=", basename(locale_filename)))` to link and expose pdf file in you browser? For me, this code works. Another thing to consider with plumber is to use full path and not relative path to access files, it will always work whereas with relative path it depends. – Guillaume Jun 29 '21 at 19:49
  • Yeah. The file renders without an issue in it's specified folder, but when I try to push that file through the AP interface I get this error: Response body Unrecognized response type; displaying content as text. I get something what looks like a pdf file in the body response, but when I try to open it with a pdf viewer i get a corrupt file error. The response headers I get are these: content-length: 1050292 content-type: application/pdf date: Tue29 Jun 2021 19:46:37 GMT – Alokin Jun 29 '21 at 19:51
  • Where in the code would I place the line: res$setHeader("Content-Disposition", paste0("attachment; filename=", basename(locale_filename))) ? – Alokin Jun 29 '21 at 19:55

1 Answers1

1

It is something like this:

#* @serializer contentType list(type="application/pdf")
#* parameter_selection
#* @param a1
#* @get /pdf

function( a1, res) {

  #Please assume that the filename location is correct. 

  filename = paste0( a1, '_','report','.pdf', sepp='')

  locale_filename = paste0('notebooks/',filename, sepp= '')

  tmp <- tempfile()

  res$setHeader("Content-Disposition", paste0("attachment; filename=", basename(locale_filename)))

  tmp <- rmarkdown::render(file_location ,output_file = paste0(this_title, sepp=''), envir = new.env(parent = globalenv()))


  readBin(locale_filename, "raw", n=file.info(locale_filename)$size)

}

Alokin
  • 461
  • 1
  • 4
  • 22