lib/node-highcharts-exporter/main.js
function(processingDir, hcExportRequest, callback){
    var outputChartName = hcExportRequest.filename,
        outputFormat    = hcExportRequest.type.split('/')[1],
        outputExtension = outputFormat == 'svg+xml' ? '.svg' : '.' + outputFormat,
        outputFile      = outputChartName + outputExtension,
        outputFilePath  = processingDir + outputFile,
        basePNGFile     = processingDir + outputChartName + '.png',
        baseSVGFile     = processingDir + outputChartName + '.svg',
        exportInfo      = {
                            fileName  : outputChartName,
                            file      : outputFile,
                            type      : outputExtension.replace('.',''),
                            parentDir : processingDir,
                            filePath  : outputFilePath
                        };

    fs.writeFile(baseSVGFile, hcExportRequest.svg, function(svgErr){
        if(outputFormat == 'svg+xml'){
            callback(null, exportInfo);
        }
        else{
            svg2png(baseSVGFile, basePNGFile, hcExportRequest.scale, function(err){
                switch(outputFormat){
                    case 'png':
                        callback(null, exportInfo);
                        break;
                    case 'pdf':
                        var pdf = new pdfkit({size:'A4',layout:'landscape'});
                        pdf.image(basePNGFile,{width : 700})
                            .write(outputFilePath, function(){
                                callback(null, exportInfo);
                            });
                        break;
                    case 'jpeg':
                    case 'jpg':
                        jpegConvert(basePNGFile, outputFilePath , {width : 1200}, function(jpegError){
                            if(jpegError) throw jpegError;
                            callback(null,exportInfo);
                        });
                        break;
                    default:
                        var errorMessage = ['Invalid export format requested:',outputFormat+'.','Currently supported outputFormats: svg+xml, pdf, jpeg, jpg, and png.'].join(' ');
                        callback({message: errorMessage}, null);
                }
            });
        }
    });
}
function(processingDir, hcExportRequest, callback){
    var outputChartName = hcExportRequest.filename,
        outputFormat    = hcExportRequest.type.split('/')[1],
        outputExtension = outputFormat == 'svg+xml' ? '.svg' : '.' + outputFormat,
        outputFile      = outputChartName + outputExtension,
        outputFilePath  = processingDir + outputFile,
        basePNGFile     = processingDir + outputChartName + '.png',
        baseSVGFile     = processingDir + outputChartName + '.svg',
        exportInfo      = {
                            fileName  : outputChartName,
                            file      : outputFile,
                            type      : outputExtension.replace('.',''),
                            parentDir : processingDir,
                            filePath  : outputFilePath
                        };

    fs.writeFile(baseSVGFile, hcExportRequest.svg, function(svgErr){
        if(outputFormat == 'svg+xml'){
            callback(null, exportInfo);
        }
        else{
            svg2png(baseSVGFile, basePNGFile, hcExportRequest.scale, function(err){
                switch(outputFormat){
                    case 'png':
                        callback(null, exportInfo);
                        break;
                    case 'pdf':
                        var pdf = new pdfkit({size:'A4',layout:'landscape'});
                        pdf.image(basePNGFile,{width : 700})
                            .write(outputFilePath, function(){
                                callback(null, exportInfo);
                            });
                        break;
                    case 'jpeg':
                    case 'jpg':
                        jpegConvert(basePNGFile, outputFilePath , {width : 1200}, function(jpegError){
                            if(jpegError) throw jpegError;
                            callback(null,exportInfo);
                        });
                        break;
                    default:
                        var errorMessage = ['Invalid export format requested:',outputFormat+'.','Currently supported outputFormats: svg+xml, pdf, jpeg, jpg, and png.'].join(' ');
                        callback({message: errorMessage}, null);
                }
            });
        }
    });
}
function ( processingDir , hcExportRequest , callback ) { var outputChartName = hcExportRequest . filename , outputFormat = hcExportRequest . type . split ( '/' ) [ 1 ] , outputExtension = outputFormat == 'svg+xml' ? '.svg' : '.' + outputFormat , outputFile = outputChartName + outputExtension , outputFilePath = processingDir + outputFile , basePNGFile = processingDir + outputChartName + '.png' , baseSVGFile = processingDir + outputChartName + '.svg' , exportInfo = { fileName : outputChartName , file : outputFile , type : outputExtension . replace ( '.' , '' ) , parentDir : processingDir , filePath : outputFilePath } ; fs . writeFile ( baseSVGFile , hcExportRequest . svg , function ( svgErr ) { if ( outputFormat == 'svg+xml' ) { callback ( null , exportInfo ) ; } else { svg2png ( baseSVGFile , basePNGFile , hcExportRequest . scale , function ( err ) { switch ( outputFormat ) { case 'png' : callback ( null , exportInfo ) ; break ; case 'pdf' : var pdf = new pdfkit ( { size : 'A4' , layout : 'landscape' } ) ; pdf . image ( basePNGFile , { width : 700 } ) . write ( outputFilePath , function ( ) { callback ( null , exportInfo ) ; } ) ; break ; case 'jpeg' : case 'jpg' : jpegConvert ( basePNGFile , outputFilePath , { width : 1200 } , function ( jpegError ) { if ( jpegError ) throw jpegError ; callback ( null , exportInfo ) ; } ) ; break ; default : var errorMessage = [ 'Invalid export format requested:' , outputFormat + '.' , 'Currently supported outputFormats: svg+xml, pdf, jpeg, jpg, and png.' ] . join ( ' ' ) ; callback ( { message : errorMessage } , null ) ; } } ) ; } } ) ; }
Exports chart into desired format
@param  {string}   processingDir   The processing directory to export the chart to (returned by _makeDirs() function)
@param  {object}   hcExportRequest The Highcharts POSTed export request object
@param  {Function} asyncCallback   A reference to the async callback
@return {void}                     Nothing
Notes: At this juncture, if you request anything other than svg, a PNG will be
created first and if requested, a PDF or JPEG will be then created from
that PNG.
Exports chart into desired format