Error after using pagedown to generate a pdf from html in a shinyapp

I have written an app that creates a downloadable report, I was hoping to use the pagedown package to create a pdf from a html which is better formatted and more presentable. It tested fine locally on my machine. After deployment, the document failed to download with the following error from the logs:

Warning: Error in find_chrome: Cannot find Chromium or Google Chrome
[No stack trace available]

I am using Google Chrome to view the app but I am guessing there's more to it than this?

The code to handle the download is as follows:

output$downloadDrug <- downloadHandler(
    filename = function() {("drug-instructions.pdf)},
    
    content = function(file) {
      src <- normalizePath('report_drugHTML.Rmd')
      src2 <- normalizePath("printout.css")
      label <- normalizePath("pxLabel.png")
      logo <-normalizePath("logo.png")
      fasting <- normalizePath("fasting.png")
      
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report_drugHTML.Rmd', overwrite = TRUE)
      file.copy(src2, "printout.css", overwrite = TRUE)
      file.copy(label, "pxLabel.png", overwrite = TRUE)
      file.copy(logo, "logo.png", overwrite = TRUE)
      file.copy(fasting, "fasting.png", overwrite = TRUE)
      
      library(rmarkdown)
      out <- render('report_drugHTML.Rmd', 
                    params = list(name = input$px_name, dob = input$dob),
                    'html_document')
      library(pagedown)
      out <- pagedown::chrome_print(out, "drug-instructions.pdf")
      file.rename(out, file)
    }
  )

Any suggestions would be greatly appreciated.

It seems that pagedown cannot find chrome or chromium on the server that runs the Shiny app.
First, check that Chrome or Chromium is installed on this server.
The Chrome or Chromium must also be available through the PATH environment variable.
Last thing: when using pagedown::chrome_print() in Shiny, you have to use async = TRUE.

You can see a recent convo with @smsenkier on this subject here https://github.com/rstudio/pagedown/issues/91#issuecomment-558331313

3 Likes

Thanks for taking the time to reply. I've only been coding for a few months, so forgive me if some things seem obvious to others. The server I've uploaded to is just the shinyapps server, would this cause problem with pagedown? Please can you explain what you mean by "through the PATH environment variable"?

My mistake, I haven't noticed that you uploaded your app on shinyapps.io.
In that case, someone has to send the installation script for the pagedown package dependencies to the shinyapps.io maintainers.
I did it, this is here: https://github.com/rstudio/shinyapps-package-dependencies/pull/220

Now, we have to wait for this pull request to be merged. It may take a few weeks.

I am sorry for the use of technical terms. An environment variable is a special variable that characterize the context of your R session. You can print the current environment variables with Sys.getenv(). The PATH is a special one that contains all the directories where programs can be found. This is essential to lookup for specific programs. But if you use shinyapps.io, you haven't to worry about these technical details. When the previously mentioned pull request will be merged, the pagedown::chrome_print() function should work.

2 Likes

Wonderful. So I only need to sit and wait then. Will there be some way of getting notified about this? I have only started learning R for a few months, initially for data analysis, and saw the potential use for an app in relation to my work. I am very grateful for any education there is to learn more about this.

1 Like

If you have a Github account, you can subscribe to the notifications on this page add packages websocket & pagedown support by RLesur · Pull Request #220 · rstudio/shinyapps-package-dependencies · GitHub (on the right hand side of the page, there is a "Subscribe" button).

1 Like

@rlesur I had a look at this page https://github.com/rstudio/shinyapps-package-dependencies/pull/220 as you suggested but it seems that the issue is meant to have been resolved. However I've encountered exactly the same issue as above. Has it been totally resolved or is it still in progress? Thanks

IMO, this comes from the fact that Travis failed https://travis-ci.org/rstudio/shinyapps-package-dependencies/builds/623473620?utm_source=github_status&utm_medium=notification
By inspecting the Travis log, the error does not come from the pull request but from the magick package installation. That means the image was not deployed to shinyapps.

@josh, is my analysis correct?

1 Like

Hello, I have a similar problem with pagedown::chrome_print(). Were you able to solve this ??

Sorry, not checked into here for a long time. Essentially no, and I've resorted to generate an html and print as pdf through chrome.

Really nice post, it’s already helping me.

Hi Leighton,

Did you manage to get chrome_print() to work in shiny online and not just on the local machine?

You could try using downloadHandler( or renderUi({ instead of chrome_print.

Hi, that was what I started using with R Markdown before exploring chrome_print. chrome_print seemed a more elegant solution allowing pdf for what I need from an html output that is more flexible in formatting. But I have not ried again to see if the issue is resolved.

1 Like

Hi, I have written a small shiny apps to explain how to use pagedown::chrome_print() in shinyapps.io.

You can find its source code here: GitHub - RLesur/chrome_print_shiny: How to use pagedown::chrome_print() in a Shiny app? and it is deployed at this address: https://romain-lesur.shinyapps.io/chrome_print_shiny/

1 Like