Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const pdfMerge = require('pdf-merge');
  3. const async = require('async');
  4. const path = require('path');
  5.  
  6. fs.readFile('./config.json', 'utf8', function(err, config) {
  7.   if (err) {
  8.     throw new Error('Eror while loading config file');
  9.   }
  10.  
  11.   config = JSON.parse(config.replace(/\\/g, '/'));
  12.  
  13.   if (config.TRANSFLO_DIR[config.TRANSFLO_DIR.length - 1] !== '/' && config.TRANSFLO_DIR) {
  14.     config.TRANSFLO_DIR += '/';
  15.   }
  16.  
  17.   if (config.INVOICES_DIR[config.INVOICES_DIR.length - 1] !== '/' && config.INVOICES_DIR) {
  18.     config.INVOICES_DIR += '/';
  19.   }
  20.  
  21.   if (config.RATE_CONFIRMATION_DIR[config.RATE_CONFIRMATION_DIR.length - 1] !== '/' && config.RATE_CONFIRMATION_DIR) {
  22.     config.RATE_CONFIRMATION_DIR += '/';
  23.   }
  24.  
  25.   const TRANSFLO_DIR = config.TRANSFLO_DIR || './files/TRANSFLO/';
  26.   const INVOICES_DIR = config.INVOICES_DIR || './files/INVOICES/';
  27.   const RATE_DIR = config.RATE_CONFIRMATION_DIR || './files/RATE CONFIRMATIONS/';
  28.  
  29.   async.parallel([
  30.     function(done) {
  31.       fs.readdir(TRANSFLO_DIR, function(err, files) {
  32.         if (err) {
  33.           throw new Error('Can\'t find TRANSFLO directory');
  34.         }
  35.  
  36.         done(null, files);
  37.       });
  38.     },
  39.     function(done) {
  40.       fs.readdir(INVOICES_DIR, function(err, files) {
  41.         if (err) {
  42.           throw new Error('Can\'t find INVOICES directory');
  43.         }
  44.  
  45.         done(null, files);
  46.       });
  47.     },
  48.     function(done) {
  49.       fs.readdir(RATE_DIR, function(err, files) {
  50.         if (err) {
  51.           throw new Error('Can\'t find RATE CONFIRMATIONS directory');
  52.         }
  53.  
  54.         done(null, files);
  55.       });
  56.     }
  57.     ], function(err, results) {
  58.       if (err) {
  59.         throw new Error('An unknown error has occurred');
  60.       }
  61.  
  62.       const pairs = [];
  63.  
  64.       results[0].forEach(function(factor) {
  65.         const match = { factor: TRANSFLO_DIR + factor };
  66.  
  67.         factor = factor.substr(0, factor.length - 4);
  68.  
  69.         results[1].forEach(function(invoice) {
  70.           if (invoice.indexOf(factor) !== -1) {
  71.             match.invoice = INVOICES_DIR + invoice;
  72.           }
  73.         });
  74.  
  75.         results[2].forEach(function(rate) {
  76.           if (rate.indexOf(factor) !== -1) {
  77.             match.rate = RATE_DIR + rate;
  78.           }
  79.         });
  80.  
  81.         if (match.factor && match.invoice && match.rate) {
  82.           match.label = factor;
  83.           pairs.push(match);
  84.         }
  85.       });
  86.  
  87.       async.each(pairs, function(pair, done) {
  88.         const paths = [ normalize(pair.invoice), normalize(pair.rate), normalize(pair.factor) ];
  89.         const pdf = new pdfMerge(paths);
  90.  
  91.         pdf.merge(function(err, result) {
  92.           if (err) {
  93.             throw new Error('An error has occurred while merging the PDF');
  94.           }
  95.  
  96.           fs.writeFile('./output/' + pair.label + '.pdf', result, function(err) {
  97.             if (err) {
  98.               throw new Error('An error has occurred while saving the PDF');
  99.             }
  100.  
  101.             done();
  102.           });
  103.         });
  104.       }, function() {
  105.         console.log('All PDFs has been merged successfuly.');
  106.         process.exit();
  107.       });
  108.     }
  109.   );
  110. })
  111.  
  112. const normalize = function(string) {
  113.   return '"' + string + '"';
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement