ropensci/magick

Attempt to perform an operation not allowed by the security policy `PDF'

dcaud opened this issue · 5 comments

dcaud commented

The error happens when trying to write to a pdf with image_write() on shinyapps.io. I think the Posit needs to change the policy.xml file on their newly updated Ubuntu servers (see https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion).

Do you have ideas to fix this issue without needing to wait for Posit to make a change?

Here's an example app that reproduces the error:

library(shiny)
library(magick)

ui <- fluidPage(
  titlePanel("Error when downloading as PDF"),
  
  sidebarLayout(
    sidebarPanel(
      radioButtons("fileType", "Choose File Type:",
                   choices = c("PDF", "JPEG"),
                   selected = "PDF"),
      downloadButton("downloadFile", "Download")
    ),
    
    mainPanel(
      imageOutput("displayImage")
    )
  )
)

server <- function(input, output, session) {
  
  img <- image_read("wizard:")
  
  output$displayImage <- renderImage({
    tmp <- tempfile(fileext = ".png")
    image_write(img, path = tmp, format = "png")
    list(src = tmp)
  }, deleteFile = TRUE)
  
  output$downloadFile <- downloadHandler(
    filename = function() {
      if (input$fileType == "PDF") {
        paste0("converted_", Sys.Date(), ".pdf")
      } else {
        paste0("converted_", Sys.Date(), ".jpeg")
      }
    },
    content = function(file) {
      if (input$fileType == "PDF") {
        # The write function w/ PDF seems to be at odds with a policy that can be revised.
        image_write(img, path = file, format = "pdf")
      } else {
        image_write(img, path = file, format = "jpeg")
      }
    }
  )
}

shinyApp(ui = ui, server = server)

Related: rstudio/shinyapps-package-dependencies#289 (comment)

jeroen commented

Yeah I don't think it is possible to work around the policy restrictions. I can try to ping the sysadmins...

jeroen commented

I actually thought they had just removed the policy: rstudio/shinyapps-package-dependencies#323

dcaud commented

They definitely removed the policy a while back, but it has reappeared.

dcaud commented

Thanks, jeroen! I think what happened is that they wrote a script to remove policy.xml for Focal that has was included (and updated) to work with Jammy.

dcaud commented

The Posit folks fixed this so new apps launched there do not continue to have this problem.