I am trying to download PDF's via an API where I recieve the data as Base64-encoded binary in JSON format. Is there a way to convert that to a pdf using R?
My approach is the following but that generated "PDF" can't be read properly by a PDF reader. By looking at it in Notepad it also looks like it is missing something like additional metadata?, as it should create the following PDF
file <- fromJSON("data.txt")
decoded <- base64_dec(file$data)
save(decoded, "file.pdf")
File: data.txt
CodePudding user response:
You should use writeBin
to write out the binary raw data to a file
data <- jsonlite::fromJSON("data.txt")
raw <- openssl::base64_decode(data$data)
writeBin(raw, "output.pdf")