{"size": 1484, "ext": "js", "max_stars_repo_path": "test/extract.js", "max_stars_repo_name": "johnlindquist/pdf-extractor", "max_stars_repo_stars_event_min_datetime": "2018-11-10T13:34:46.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-19T15:39:10.000Z", "max_issues_repo_path": "test/extract.js", "max_issues_repo_name": "johnlindquist/pdf-extractor", "max_issues_repo_issues_event_min_datetime": "2018-11-06T12:31:49.000Z", "max_issues_repo_issues_event_max_datetime": "2021-07-27T09:17:48.000Z", "max_forks_repo_path": "test/extract.js", "max_forks_repo_name": "johnlindquist/pdf-extractor", "max_forks_repo_forks_event_min_datetime": "2018-11-04T16:47:36.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-10T03:05:07.000Z"}
{"max_stars_count": 47, "max_issues_count": 8, "max_forks_count": 15, "avg_line_length": 23.935483871, "max_line_length": 94, "alphanum_fraction": 0.6543126685}
const crypto = require('crypto');
const fs = require('fs');
const ejs = require('ejs');
const Promise = require("bluebird");
const PdfExtractor = require('../index').PdfExtractor;

global.Promise = Promise;
Promise.longStackTraces();

// Relative path of the PDF file.
let pdfPath = process.argv[2] || './pdfs/c_tutorial.pdf',
	numPagesLimit = parseInt(process.argv[3]) || Infinity,
	fileBuffer = fs.readFileSync(pdfPath),
	fileHash = crypto.createHash('md5').update(fileBuffer).digest('hex'),
	outputDir = `./output/${fileHash}`;

try {
	fs.mkdirSync(outputDir);
} catch (e) {
	if (e.code !== 'EEXIST') {
		throw e;
	}
}

console.log('Output to: ' + outputDir);

let pdfExtractor = new PdfExtractor(outputDir, {
	viewportScale: (width, height) => {
		if (width > height) {
			return 1100 / width;
		}
		return 800 / width;
	},
	pageRange: [1, numPagesLimit]
});

pdfExtractor.parseFromFileBuffer(fileBuffer).then(function (doc) {

	console.log('# End of Document');

	console.log('Parsing html preview');

	setTimeout(() => {
		let fileInfo = JSON.parse(fs.readFileSync(outputDir + '/info.json', {encoding: 'utf8'}));

		ejs.renderFile('./template.ejs', {dir:outputDir, info: fileInfo}, {}, function(err, result){
			fs.writeFile(outputDir + '/preview.html', result, { encoding: 'utf8' }, function(err) {
				if (err) {
					return console.log(err);
				}

				console.log('Done :' + outputDir);
			});
		})
	}, 20);



}).catch(function (err) {
	console.error('Error: ' + err);
});
