Leaflet map is cut off when rendering qmd to PDF, wrong view of snapshot is taken. #2276
Closed
Description
The problem may be reproduced by the following qmd code, please delete the '\' in the '`\``' mark inside for running the code, , it seems that a wrong view of the map is set when it's rendered to PDF.
---
format:
pdf:
documentclass: scrreprt
---
## htmlwidgets
It's O.K. with HTML & Word, but the marker is cut off from the screenshot when rendered to PDF:
```{r}
#| fig-alt: Leaflet map of Maungawhau / Mount Eden.
#| error: true
library(leaflet)
map<-leaflet(width="90%") |>
setView(lng = 116.3125774825, lat = 39.9707249401, zoom = 16) |>
addTiles(urlTemplate = # Tencent tile map provider URL
"http://rt0.map.gtimg.com/realtimerender?z={z}&x={x}&y={-y}&type=vector&style=0") |>
addMarkers(lng=116.3125774825, lat=39.9707249401, popup="The birthplace of COS")
map
```
It's O.K. by taking a snapshot and saving to png, then being loaded later.
```{r}
library(htmlwidgets)
library(webshot2)
## save html to png
saveWidget(map, "leaflet_map.html", selfcontained = FALSE)
webshot("leaflet_map.html", file = "leaflet_map.png",
cliprect = "viewport")
## optionally display image when using knitr
knitr::include_graphics("leaflet_map.png")
```
Here's the rendered PDF output:
test.pdf