Search code examples
javascriptphantomjscasperjs

Binary base64encode at CasperJS


Is there a way to base64 encode binary data at CasperJS?
I mean something like that:

var pdfFile = require('fs').open('some.pdf', 'br');
var pdf = pdfFile.read();
var encoded = _some_func_for_encode(pdf);

Thanks.


Solution

  • You can certainly use native js method like btoa() and atob() .

    Here is a very basic phantomjs script :

    var fs = require('fs');
    var filedata = fs.read('thefilehere');
    var res = btoa(filedata);
    console.log(res);
    phantom.exit();
    

    Base64 encoding/decoding is not so complex and you can easily find js function such as this one.