<commit_before>var path = require('path')
var htmlPdf = require('html-pdf-chrome')
var uuid = require('uuid')
var debug = require('debug')('pdf:generator')
var error = require('./error')
var uuid = require('uuid')
var utils = require('./utils')

function createPdfGenerator(storagePath, options = {}, storagePlugins = {}) {
  return function createPdf (url, job) {
    debug('Creating PDF for url %s with options %s', url, JSON.stringify(options))

    var generationId = uuid()
    var generated_at = utils.getCurrentDateTimeAsString()

    function createResponseObject() {
      return {
        id: generationId,
        generated_at: generated_at
      }
    }

    return htmlPdf
      .create(url, options)
      .then((pdf) => {
        var pdfPath = path.join(storagePath, 'pdf', (uuid() + '.pdf'))

        debug('Saving PDF to %s', pdfPath)

        pdf.toFile(pdfPath)

        var storage = {
          local: pdfPath
        }
        var storagePluginPromises = []
        for (var i in storagePlugins) {
          // Because i will change before the promise is resolved
          // we use a self executing function to inject the variable
          // into a different scope
          var then = (function(type) {
            return function (response) {
              return Object.assign(response, {
                type: type
              })
            }
          })(i)

          storagePluginPromises.push(
            storagePlugins[i](path, job).then(then)
          )
        }

        return Promise.all(storagePluginPromises).then(responses => {
          for(var i in responses) {
            var response = responses[i]

            storage[response.type] = {
              path: response.path,
              meta: response.meta || {}
            }
          }

          return Object.assign(
            createResponseObject(),
            {
              storage: storage
            }
          )
        })
      })
      .catch(msg => {
        var response = error.createErrorResponse(error.ERROR_HTML_PDF_CHROME_ERROR)

        response.message += ' ' + msg

        return Object.assign(createResponseObject(), response)
      })<commit_msg>pdf.toFile is a promise<commit_after>var path = require('path')
var htmlPdf = require('html-pdf-chrome')
var uuid = require('uuid')
var debug = require('debug')('pdf:generator')
var error = require('./error')
var uuid = require('uuid')
var utils = require('./utils')

function createPdfGenerator(storagePath, options = {}, storagePlugins = {}) {
  return function createPdf (url, job) {
    debug('Creating PDF for url %s with options %s', url, JSON.stringify(options))

    var generationId = uuid()
    var generated_at = utils.getCurrentDateTimeAsString()

    function createResponseObject() {
      return {
        id: generationId,
        generated_at: generated_at
      }
    }

    return htmlPdf
      .create(url, options)
      .then((pdf) => {
        var pdfPath = path.join(storagePath, 'pdf', (uuid() + '.pdf'))

        debug('Saving PDF to %s', pdfPath)

        return pdf
          .toFile(pdfPath)
          .then(function(response){
            var storage = {
              local: pdfPath
            }
            var storagePluginPromises = []
            for (var i in storagePlugins) {
              // Because i will change before the promise is resolved
              // we use a self executing function to inject the variable
              // into a different scope
              var then = (function(type) {
                return function (response) {
                  return Object.assign(response, {
                    type: type
                  })
                }
              })(i)

              storagePluginPromises.push(
                storagePlugins[i](path, job).then(then)
              )
            }

            return Promise.all(storagePluginPromises).then(responses => {
              for(var i in responses) {
                var response = responses[i]

                storage[response.type] = {
                  path: response.path,
                  meta: response.meta || {}
                }
              }

              return Object.assign(
                createResponseObject(),
                {
                  storage: storage
                }
              )
            })
          })
      })
      .catch(msg => {
        var response = error.createErrorResponse(error.ERROR_HTML_PDF_CHROME_ERROR)

        response.message += ' ' + msg

        return Object.assign(createResponseObject(), response)
      })
{'fraction_non_alphanumeric': 0.10751748251748251, 'fraction_numerical': 0.0, 'mean_word_length': 1.7131001778304682, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3995185', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1161, 'n_words': 298}