【问题标题】:How to dynamically adjust iframe height in Shiny app如何在 Shiny 应用程序中动态调整 iframe 高度
【发布时间】:2021-06-15 10:31:18
【问题描述】:

目标是在我的 Shiny 应用程序中拥有一个没有垂直滚动条的 iframe(因此其中显示的 PDF 完全可见),并根据窗口大小自动调整高度。

我可以将 iframe 的高度调整为几乎总是显示整个 PDF 的高度(例如:600 像素)。

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"
  )
)

但是当浏览器窗口缩小时,iframe相对于PDF来说太长了,看起来很差。

我遇到了这个看起来很有希望的blog post,但我不确定它是否适用或如何将它整合到我的 Shiny 应用程序中。

此外,PDF 只有一页。我考虑过将 PDF 转换为图像并使用 renderImage()imageOutput()。这是更好的方法吗?

【问题讨论】:

    标签: r pdf iframe shiny


    【解决方案1】:

    我没有尝试调整 iframe 以便无论浏览器窗口大小如何都始终可见完整的 PDF,而是转换为以像素为单位指定尺寸的 PNG:

    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)
    )
    

    因为目标是显示 PowerPoint 幻灯片,所以我使用 LibreOffice 命令行工具将 PowerPoint 幻灯片直接转换为 PNG,效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-23
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2020-12-17
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多