Skip to content

Commit

Permalink
Merge pull request #851 from lachok/master
Browse files Browse the repository at this point in the history
feat (printer) Add progress callback option
  • Loading branch information
liborm85 authored Feb 4, 2017
2 parents 10e4b21 + 950783a commit 5442b5b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/browser-extensions/pdfMake.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,46 @@ Document.prototype._openWindow = function () {
return win;
};

Document.prototype.open = function () {
Document.prototype.open = function (options) {
var win = this._openWindow();

options.autoPrint = false;

try {
var that = this;
this.getBuffer(function (result) {
var blob = that._bufferToBlob(result);
var urlCreator = window.URL || window.webkitURL;
var pdfUrl = urlCreator.createObjectURL(blob);
win.location.href = pdfUrl;
}, {autoPrint: false});
}, options);
} catch (e) {
win.close();
throw e;
}
};


Document.prototype.print = function () {
Document.prototype.print = function (options) {
var win = this._openWindow();

options.autoPrint = true;

try {
var that = this;
this.getBuffer(function (result) {
var blob = that._bufferToBlob(result);
var urlCreator = window.URL || window.webkitURL;
var pdfUrl = urlCreator.createObjectURL(blob);
win.location.href = pdfUrl;
}, {autoPrint: true});
}, options);
} catch (e) {
win.close();
throw e;
}
};

Document.prototype.download = function (defaultFileName, cb) {
Document.prototype.download = function (defaultFileName, cb, options) {
if (typeof defaultFileName === 'function') {
cb = defaultFileName;
defaultFileName = null;
Expand All @@ -145,7 +149,7 @@ Document.prototype.download = function (defaultFileName, cb) {
if (typeof cb === 'function') {
cb();
}
});
}, options);
};

Document.prototype.getBase64 = function (cb, options) {
Expand Down
11 changes: 9 additions & 2 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ PdfPrinter.prototype.createPdfKitDocument = function (docDefinition, options) {
this.pdfKitDoc.options.size = [pageSize.width, pageHeight];
}

renderPages(pages, this.fontProvider, this.pdfKitDoc);
renderPages(pages, this.fontProvider, this.pdfKitDoc, options.progressCallback);

if (options.autoPrint) {
var printActionRef = this.pdfKitDoc.ref({
Expand Down Expand Up @@ -273,9 +273,14 @@ function updatePageOrientationInOptions(currentPage, pdfKitDoc) {
}
}

function renderPages(pages, fontProvider, pdfKitDoc) {
function renderPages(pages, fontProvider, pdfKitDoc, progressCallback) {
pdfKitDoc._pdfMakePages = pages;
pdfKitDoc.addPage();

var totalItems = progressCallback && _.sumBy(pages, function(page) { return page.items.length; });
var renderedItems = 0;
progressCallback = progressCallback || function() {};

for (var i = 0; i < pages.length; i++) {
if (i > 0) {
updatePageOrientationInOptions(pages[i], pdfKitDoc);
Expand All @@ -296,6 +301,8 @@ function renderPages(pages, fontProvider, pdfKitDoc) {
renderImage(item.item, item.item.x, item.item.y, pdfKitDoc);
break;
}
renderedItems++;
progressCallback(renderedItems / totalItems);
}
if (page.watermark) {
renderWatermark(page, pdfKitDoc);
Expand Down
50 changes: 50 additions & 0 deletions tests/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,54 @@ describe('Printer', function () {
assert(Pdfkit.prototype.addPage.callCount === 3);
});

it('should report progress on each rendered item when a progressCallback is passed', function () {

printer = new Printer(fontDescriptors);

var progressCallback = sinon.spy(function(progress) {});

var docDefinition = {
pageSize: 'A4',
content: [
{
text: 'Text item 1'
},
{
image: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAGAQMAAADNIO3CAAAAA1BMVEUAAN7GEcIJAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB98DBREbA3IZ3d8AAAALSURBVAjXY2BABwAAEgAB74lUpAAAAABJRU5ErkJggg=='
},
{
text: 'Text item 2'
},
{
canvas: [{
type: 'rect',
x: 0,
y: 0,
w: 310,
h: 260
}]
}]
};

printer.createPdfKitDocument(docDefinition, {progressCallback: progressCallback});

assert(progressCallback.withArgs(0.25).calledOnce);
assert(progressCallback.withArgs(0.5).calledOnce);
assert(progressCallback.withArgs(0.75).calledOnce);
assert(progressCallback.withArgs(1).calledOnce);
});

it('should work without a progressCallback', function() {
printer = new Printer(fontDescriptors);

var docDefinition = {
pageSize: 'A4',
content: [ { text: 'Text item 1' } ]
};

assert.doesNotThrow(function() {
printer.createPdfKitDocument(docDefinition)
});
});

});

0 comments on commit 5442b5b

Please sign in to comment.