Skip to content

Instantly share code, notes, and snippets.

@jrosell
Created December 9, 2021 10:20
Show Gist options
  • Save jrosell/7f0ac7df292c750c89582f33930e57e9 to your computer and use it in GitHub Desktop.
Save jrosell/7f0ac7df292c750c89582f33930e57e9 to your computer and use it in GitHub Desktop.
library(RPostgres)
# Get and read the PDF
path <- file.path("os2.pdf")
pdf <- readBin(con = path, what = raw(), n = file.info(path)$size)
# Open it
browseURL(path)
# Connect to default DB and put seralized raw pdf in a data.frame
# sudo -u postgres createuser jordi
# sudo -u postgres createdb jordi --owner=jordi
con <- dbConnect(RPostgres::Postgres())
pdf_serialized <- serialize(pdf, NULL)
df <- data.frame(pdfs = I(list(pdf_serialized)))
# Write the table in database
dbWriteTable(conn = con, name = "mytable", value = df, overwrite = TRUE)
# Read your table, unserialize and write the pdf in a temporary file
out <- dbReadTable(conn = con, name = "mytable")
pdf_out <- unserialize(out$pdfs[[1]])
tmp <- tempfile(fileext = ".pdf")
writeBin(object = pdf_out, con = tmp)
# Open it
browseURL(tmp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment