<commit_before>/* jslint node: true */
/* jslint browser: true */
/* global saveAs */
/* global BlobBuilder */
'use strict';

var PdfPrinter = require('../printer');

var defaultClientFonts = {
	Roboto: {
		normal: 'Roboto-Regular.ttf',
		bold: 'Roboto-Medium.ttf',
		italics: 'Roboto-Italic.ttf',
		bolditalics: 'Roboto-Italic.ttf'
	}
};

function Document(docDefinition, fonts, vfs) {
	this.docDefinition = docDefinition;
	this.fonts = fonts || defaultClientFonts;
	this.vfs = vfs;
}

Document.prototype._createDoc = function(options, callback) {
	var printer = new PdfPrinter(this.fonts);
	printer.fs.bindFS(this.vfs);

	var doc = printer.createPdfKitDocument(this.docDefinition, options);
	var chunks = [];
	var result;

	doc.on('data', function(chunk) {
		chunks.push(chunk);
	});
	doc.on('end', function() {
		result = Buffer.concat(chunks);
		callback(result);
	});
	doc.end();
};

Document.prototype.open = function(message) {
	// we have to open the window immediately and store the reference
	// otherwise popup blockers will stop us
	var win = window.open('', '_blank');

	try {
		this.getDataUrl(function(result) {
			win.location.href = result;
		});
	} catch(e) {
		win.close();
		return false;
	}
};


Document.prototype.print = function(timeout) {
	timeout = timeout || 2000;

	this.getDataUrl(function(dataUrl) {
		var iFrame = document.createElement('iframe');
		iFrame.style.display = 'none';
		iFrame.src = dataUrl;
		iFrame.onload = function() {
			setTimeout(function() {
				document.body.removeChild(iFrame);
			}, timeout);
		};

		document.body.appendChild(iFrame);
	}, { autoPrint: true });
};

Document.prototype.download = function(defaultFileName) {
	defaultFileName = defaultFileName || 'file.pdf';
	this.getBuffer(function(result) {
		saveAs(new Blob([result], {type: 'application/pdf'}), defaultFileName);
	});
};

Document.prototype.getBase64 = function(cb, options) {
	if (!cb) throw 'getBase64 is an async method and needs a callback argument';<commit_msg>This PR fixes an issue with generating pdf's on client side with autoPrint turned on. Previous verion inserted an iframe with the pdf and then removed it in a timeout. This resulted that Firefox couldn't print it from an iframe that has already been removed. Also switching between printers yielded in an error. Currently the iframe is removed upon first click somewhere on the page.<commit_after>/* jslint node: true */
/* jslint browser: true */
/* global saveAs */
/* global BlobBuilder */
'use strict';

var PdfPrinter = require('../printer');

var defaultClientFonts = {
	Roboto: {
		normal: 'Roboto-Regular.ttf',
		bold: 'Roboto-Medium.ttf',
		italics: 'Roboto-Italic.ttf',
		bolditalics: 'Roboto-Italic.ttf'
	}
};

function Document(docDefinition, fonts, vfs) {
	this.docDefinition = docDefinition;
	this.fonts = fonts || defaultClientFonts;
	this.vfs = vfs;
}

Document.prototype._createDoc = function(options, callback) {
	var printer = new PdfPrinter(this.fonts);
	printer.fs.bindFS(this.vfs);

	var doc = printer.createPdfKitDocument(this.docDefinition, options);
	var chunks = [];
	var result;

	doc.on('data', function(chunk) {
		chunks.push(chunk);
	});
	doc.on('end', function() {
		result = Buffer.concat(chunks);
		callback(result);
	});
	doc.end();
};

Document.prototype.open = function(message) {
	// we have to open the window immediately and store the reference
	// otherwise popup blockers will stop us
	var win = window.open('', '_blank');

	try {
		this.getDataUrl(function(result) {
			win.location.href = result;
		});
	} catch(e) {
		win.close();
		return false;
	}
};


Document.prototype.print = function() {
  this.getDataUrl(function(dataUrl) {
    var iFrame = document.createElement('iframe');
    iFrame.style.position = 'absolute';
    iFrame.style.left = '-99999px';
    iFrame.src = dataUrl;
    iFrame.onload = function() {
      function removeIFrame(){
        document.body.removeChild(iFrame);
        document.removeEventListener('click', removeIFrame);
      }
      document.addEventListener('click', removeIFrame, false);
    };

    document.body.appendChild(iFrame);
  }, { autoPrint: true });
};

Document.prototype.download = function(defaultFileName) {
	defaultFileName = defaultFileName || 'file.pdf';
	this.getBuffer(function(result) {
		saveAs(new Blob([result], {type: 'application/pdf'}), defaultFileName);
	});
};

Document.prototype.getBase64 = function(cb, options) {
	if (!cb) throw 'getBase64 is an async method and needs a callback argument';
{'fraction_non_alphanumeric': 0.14298421169668668, 'fraction_numerical': 0.00378029797642873, 'mean_word_length': 4.285546415981199, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6698880', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1429, 'n_words': 351}