0

I'm generating a powerpoint presentation in R using officer, and then converting this to a PDF so a user can preview slides inside the shiny golem app. But after referencing this thread and this SO answer to a similar question I'm still not quite sure how to accomplish this. I am able to display external PDFs located in inst/app/www inside an iframe, but not sure how to do this for PDFs generated inside the app itself.

Here is the relevant code snippet from my app_server.R file:

  output$preview <- renderUI({

  # Creates rpptx object with one slide
  preview_rpptx <- add_title_slide(x = NULL, title = input_list[["title"]]) 

  # Creates www/ directory if it doesn't exist
  if (!dir_exists("www")) dir_create("www")

  # Generates .pptx file 
  pptx_file_path <- file_temp("preview", tmp_dir = "www", ext = ".pptx")
  print(preview_rpptx, pptx_file_path)
  
  # Converts .pptx to .pdf 
  pdf_file_path <- file_temp("preview", tmp_dir = "www", ext = ".pdf")
  convert_to_pdf(path = pptx_file_path, pdf_file = pdf_file_path)
  
  tags$iframe(style = "height:600px; width:100%", src = str_sub(pdf_file_path, 5))
}

Running the app, I get a "Not Found" error inside the iframe. But I can see that the PDF file was generated properly in the www/ directory.

Giovanni Colitti
  • 1,982
  • 11
  • 24
  • 1
    If you want to embed a pdf in a prettier way than the iframe way, I can show you how to do with the [pdfobject](https://pdfobject.com/) library. – Stéphane Laurent Jan 26 '21 at 09:17

1 Answers1

1

Since I don't know how to do with a file as you ask, I would encode this file to a base64 string and set this string to the src attribute:

library(base64enc)

output$preview <- renderUI({

  ......
    
  pdf_file_path <- "PATH/TO/PDF_FILE"
  b64 <- dataURI(file = pdf_file_path, mime = "application/pdf")
  
  tags$iframe(
    style = "height: 600px; width: 100%;", src = b64
  )
  
}
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225