Azure Blob storage octet-stream to PDF

Azure Blob storage saves PDFs in application/octet-stream mime type. They can not be opened as is, as they first need to be base64 decoded. This script reads the invalid file using the base64topdf library, decodes it, and saves it as a PDF that can be opened with any tool capable of reading PDF files.

import base64topdf from 'base64topdf'; import fs from 'fs'; const file = fs.readFileSync('file', { encoding: 'utf-8' }); base64topdf.base64Decode(file, 'file.pdf');

base64topdf uses the following code

base64Decode: (base64str, file) => { // create buffer object from base64 encoded string, it is important to tell the constructor at the string is base64 encoded var bitmap = new Buffer(base64str, 'base64'); // write buffer to file fs.writeFileSync(file, bitmap); },

so that could also be used instead of the library