Skip to content

Instantly share code, notes, and snippets.

@thelbouffi
Created October 6, 2016 14:48
Show Gist options
  • Save thelbouffi/d3a1010edbc3bfd2d4b7f7898f795307 to your computer and use it in GitHub Desktop.
Save thelbouffi/d3a1010edbc3bfd2d4b7f7898f795307 to your computer and use it in GitHub Desktop.
Coding and decoding a file in base64 (for example a pdf file)
var fs = require('fs');
// Reade file synchronously and put the result in buffer
var file = fs.readFileSync('fileName.pdf');
// Code the file in base64
var base64EncodedPDF = new Buffer(file).toString('base64');
// Decode the file
var baseDecodedPDF = new Buffer(base64EncodedPDF, 'base64');
// Get the decoded file in its original format
fs.writeFileSync('decodedPDF.pdf', baseDecodedPDF);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment