How to dynamically adjust iframe height in Shiny app

Viewed 162

The goal is to have an iframe inside my Shiny app that has no vertical scroll bars (so the PDF displayed inside is fully visible), and automatically adjust the height based on window resizing.

I can adjust the height of the iframe to something that will almost always show the entire PDF (ex: 600px).

pdf_b64 <-
  dataURI(file = pdf_file_path, mime = "application/pdf")

tags$div(
  id = "pdf",
  tags$iframe(
    style = "height:600px;width:100%",
    src = pdf_b64,
    frameborder = "0"
  )
)

But when the browser window is reduced in size, the iframe is too long relative to the PDF and looks poor.

I came across this blog post which looks promising, but I'm not sure it's applicable or how to incorporate it into my Shiny app.

Also, the PDF is only one page. I've thought about converting the PDF to an image and using renderImage() & imageOutput(). Is this a better approach?

1 Answers

Instead of trying to adjust the iframe so that the full PDF is always visible regardless of the browser window size, I converted to a PNG with dimensions specified in pixels:

library(base64enc)

file_path <- "path/to/png"

file_b64 <-
  dataURI(file = file_path, mime = "image/png")

tags$div(
  id = "png",
  tags$img(src = file_b64, height = 7.5 * 42, width = 13.333 * 42)
)

Because the goal is to display a PowerPoint slide, I used LibreOffice command line tools to convert a PowerPoint slide directly to a PNG, which works very well!

Related