Upload and view a PDF in Shiny


#1

I am trying to replicate the example from https://shiny.rstudio.com/articles/upload.html
However, instead of loading structured PDF data, I want to upload a PDF file that I would like to preview in the next pane.
I created this sample simple shiny app but I can’t seem to get it to work. On the shiny server in the www directory I see a 1 KB file with the name “myreport.pdf” that just has the first character

library(shiny)

ui <- shinyUI(fluidPage(

titlePanel(“Testing File upload”),

sidebarLayout(
sidebarPanel(
fileInput(‘file_input’, ‘upload file ( . pdf format only)’, accept = c(’.pdf’))
),

mainPanel(
  uiOutput("pdfview")
)

)
))

server <- shinyServer(function(input, output) {

observe({
req(input$file_input)
test_file <- readBin(input$file_input$datapath, what=“character”)
writeBin(test_file, “www/myreport.pdf”)
})

output$pdfview <- renderUI({
tags$iframe(style=“height:600px; width:100%”, src=“myreport.pdf”)
})

})

shinyApp(ui = ui, server = server)


#2

Hello vverma,
change the read/write to

    test_file <- readBin(con=input$file_input$datapath,what = 'raw',n=input$file_input$size)
    writeBin(test_file,'www/myreport.pdf')

Kind regards,
Peter