Simon
Simon

Reputation: 310

MIME Binary Data to PDF

I'm receiving some data from an API call that returns some XML and associated files (PDFs):

var req = http.request(HTTPOPTIONS, function(resp){
var rawData = '';
//Build list from response data
resp.on('data', function (chunk) {
    rawData+= chunk;
});
//Process List
resp.on('end', function () {
        var breakStr = rawData.split('\n')[0];
        var fileSections = rawData.split(breakStr);
        for(var i in fileSections){
            var content = fileSections[i].split((/Content-Length: [0-9]*/));
            var fileName = content[0].split('filename=')[1].trim();
            var file = {Bucket : 'MyBucket', Key: ROOT+'/'+FOLDER+'/'+SUBFOLDER+'/'+fileName, Body: content[1]};
            console.log('Creating file: '+file.Key );
            promises.push(S3.upload(file).promise());
        }
        Promise.all(promises).then(...);
    });
});
req.write(XMLREQUEST);
req.end();

But I when I try to open the file created I get [an error message saying the file is damaged

Any ideas on where I'm going wrong?

UPDATE:

In addition to the above error message I also get [an error saying the 'document has been changed'

On these files I get the metadata (Page size/formatting and Font data) but no content.

Upvotes: 1

Views: 628

Answers (1)

Simon
Simon

Reputation: 310

It appears the problem was because I was storing the data in a string and trying to manipulate it from there. The incoming data chunk is a buffer and using it in this form mean that, once you figure out how to remove the headers, you can create the PDF files.

var req = http.request(HTTPOPTIONS, function(resp){
var respChunks =[];
var respChunksLength = 0;

resp.on('data', function (chunk) {
    respChunks.push(chunk);
    respChunksLength+=chunk.length;
});

resp.on('end', function () {
    var confResp = Buffer.concat(respChunks, respChunksLength);
    var breakStr = confResp.slice(0,confResp.indexOf('\n'));
    var bufferArray = [];
    var startBreak = confResp.indexOf(breakStr);
    while(startBreak>-1){
        bufferArray.push(confResp.slice(startBreak+breakStr.length+1,confResp.indexOf(breakStr,startBreak+1)));
        startBreak = confResp.indexOf(breakStr,startBreak+1);
    }
    var trim=0;
    for(var i = 1; i<bufferArray.length;i++){
        trim = bufferArray[i].toString().indexOf('%');
        fs.writeFile('testFile'+i+'.pdf',bufferArray[1].slice(trim));
    });
});
req.write(generateProposalDetailXML());
req.end();

the bufferArray is concatenated into a single buffer, this is then divided based on the MIME delimiter (breakStr) and the headers (clumsily) removed and written to a file.

Upvotes: 1

Related Questions