Leaflet map won't export to pdf

Hello community, I want to export my Leaflet map with custom size in a pdf. Im facing an error in my code. Can anyone help fix it ?

...
library(shiny)
library(leaflet)
library(leaflet.extras)
library(webshot)

ui <- fluidPage(
leafletOutput("map"),
downloadButton("download_pdf", "Download PDF")
)

server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(-122.4194, 37.7749, zoom = 10) #set initial view
})

output$download_pdf <- downloadHandler(
filename = "map.pdf",
content = function(file) {
# Create a temporary HTML file with the map
tmp <- tempfile(fileext = ".html")
on.exit(unlink(tmp))
htmlwidgets::saveWidget(output$map, tmp, selfcontained = TRUE)

  # Convert the HTML file to PDF using rmarkdown and webshot
  rmarkdown::render(tmp, output_format = "pdf_document",
                    output_file = file, 
                    params = list(width = "21cm", height = "29.7cm"))
}

)
}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.