Trouble creating a readable/Downloaded DOCX File from Plumber API

I'm currently developing a Plumber API in R that generates and sends back DOCX files. While the API appears to be working correctly, I'm facing an issue when attempting to open the downloaded DOCX file. I would appreciate any insights or suggestions to help resolve this matter.

  1. I have a Plumber endpoint that generates a Word document (DOCX) and serves it as a binary response.
  2. The generated DOCX file can be downloaded successfully, but there's an issue when attempting to open it. it can't be read even though the rmd script is able to generate a readable report docx. the report could be either docx or zipped. I'm fine with either.

as for my code :
1) plumber.R

#* @param test le deuxieme doc
#* @post /generate_report_word
function(test, res) {
  
  tmp1 <- tempfile(tmpdir = getwd())
  
  output_path <- paste0(getwd(), "/report_test.docx")
  
  rmarkdown::render("report_test.Rmd", output_file = output_path, 
                    params = list(team1 = test))
  

  file_content <- readBin(output_path, "raw", n = file.info(output_path)$size)
  res$setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  res$setHeader("Content-Disposition", sprintf("attachment; filename=%s", "report_test.docx"))
  res$status(200)
  res$body(file_content)
  
}

run_this.R

library(plumber)
library(rmarkdown)

r <- plumb("plumber.R")  
r$run(port=8000)

Use a serializer content type. If you want to return a file download, use as attachment.

There is an automatic content type setting according to extension, you can what docx will be here.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.