node.js - Node Cannot pipe pdf response -


trying write test (mocha) check pdf returned api endpoint holds correct data , looks should. pdf generated on sever. returns 'corrrectly' when hit endpoint manually, write tests. uploaded example 'correct' pdf test suite able parse hummus js , pull out necessary aspects comparison.

i make request endpoint (with superagent) , pipe response (a pdf) temp pdf. i'll parse both pdf's (the uploaded perfect one, , 1 returned endpoint) , make sure match.

my code request:

    it('should proposed pdf', function (done) {          request(app)            .get(url) //var sets path earlier            .expect(200)            .end(function (err, res) {                if(err) return done(err);                  var writestream = fs.createwritestream('./test/materials/tmp.pdf');                writestream.pipe(res); //error can't pipe                writestream.on('end', function() {                    console.log('complete');                    done();                 })           });        }); 

when run tests get: uncaught error: cannot pipe. not readable. new node not sure causing error. when console out res, large binary encoded jumble maybe issue? i've tried couple things - using hummus pdfwriter, trying decode

new buffer(res, 'base64')

etc... still no luck. believe have necessary packages installed these operations , seems piping/decoding/superagent issue. help!

edit: misunderstanding piping. can write response file:

fs.writefile('./test/materials/test_tmp.pdf', new buffer(res.text, 'ascii')); 

converting in case ascii. closer now, still hung on encoding piece. creates blank pdf. when observe file contents in sublime appear same pdf comparing against, have different encoding. know how match encoding of original pdf or figure out how encoded? or if possible? used pdfkit build pdf.

i don't think (or know) of way natively. if you're open using other modules, take look @ pdfkit

you can

var pdf = new pdfdocument;   pdf.pipe(fs.createwritestream('./test/materials/tmp.pdf'));   

edit
apologies, read question wrong. module has examples though. instance right docs -

pdfdocument = require 'pdfkit'  # create document doc = new pdfdocument  # pipe it's output somewhere, file or http response # see below browser usage doc.pipe fs.createwritestream('output.pdf')  # embed font, set font size, , render text doc.font('fonts/palatinobold.ttf')    .fontsize(25)    .text('some text embedded font!', 100, 100)  # add page doc.addpage()    .fontsize(25)    .text('here vector graphics...', 100, 100)  # draw triangle doc.save()    .moveto(100, 150)    .lineto(100, 250)    .lineto(200, 250)    .fill("#ff3300")  # apply transforms , render svg path 'even-odd' fill rule doc.scale(0.6)    .translate(470, -380)    .path('m 250,75 l 323,301 131,161 369,161 177,301 z')    .fill('red', 'even-odd')    .restore()  # add text annotations doc.addpage()    .fillcolor("blue")    .text('here link!', 100, 100)    .underline(100, 100, 160, 27, color: "#0000ff")    .link(100, 100, 160, 27, 'http://google.com/')  # finalize pdf file doc.end() 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -