如何在闪亮的 golem 应用程序中显示动态生成的 PDF 文件

我正在使用 officer 在 R 中生成一个 powerpoint 演示文稿,然后将其转换为 PDF,以便用户可以在闪亮的 golem 应用程序中预览幻灯片。但是在参考了 this threadthis SO answer to a similar question 之后,我仍然不太确定如何实现这一点。我可以在 iframe 内显示位于 inst/app/www 中的外部 PDF,但不确定如何对应用程序本身内部生成的 PDF 执行此操作。

这是我的 app_server.R 文件中的相关代码片段:

  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",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))
}

运行应用程序时,我在 iframe 中收到“未找到”错误。但是我可以看到在www/目录下正确生成了PDF文件。

qzheng123 回答:如何在闪亮的 golem 应用程序中显示动态生成的 PDF 文件

由于我不知道如何按照您的要求处理文件,我会将这个文件编码为 base64 字符串并将该字符串设置为 src 属性:>

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
  )
  
}
本文链接:https://www.f2er.com/1040327.html

大家都在问