使用新的 Adob​​e Acrobat 11.0 在 Shiny 中将 .Rmd 转换为 PDF

Convert .Rmd to PDF in Shiny with new Adobe Acrobat 11.0

有 r Shiny 应用程序和 Rmd 文件。尝试使用 downloadHandler() 下载但出现 Adob​​e Acrobat 错误:

"Acrobat could not open 'rstudio-iV1460.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

发现这是由于使用 Acrobat v11.0 并且需要确保文档的头部格式为 %PDF 字符串。

参考:https://helpx.adobe.com/acrobat/kb/pdf-error-1015-11001-update.html

问题:我应该把这个 %PDF 放在哪里,我该如何解决这个问题?是放在rmd文件里还是放在tex文件里?

这是我在我的 r Shiny 应用程序中从 Rmd 生成 pdf 的方式:

server.r

shinyServer(function(input, output) {

# dowload from Rmd file
output$downloadReport <- downloadHandler(
  filename = 'input.pdf',

  content = function(file) {
    src <- normalizePath('input.Rmd')

    # temporarily switch to the temp dir, in case you do not have write
    # permission to the current working directory
    owd <- setwd(tempdir())
    on.exit(setwd(owd))
    file.copy(src, 'input.Rmd')

    library(rmarkdown)
    out <- render('input.Rmd', pdf_document())
    file.rename(out, file)
  }
)
}

input.Rmd

---
always_allow_html: yes
output: 
  pdf_document:
    keep_tex: yes
---

```{r}
test
```

ui.r

fluidPage(
 downloadButton('downloadReport')
)
---
output:
  pdf_document: default
  html_document: default
  word_document: default
allow_html: yes
---

使用上面的 allow_html: yes ,它会将 HTML 图转换为静态图,并且可以在 PDF没问题。