Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (printer) Add progress callback option #851

Merged
merged 1 commit into from Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/browser-extensions/pdfMake.js
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
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 @@ -265,9 +265,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 @@ -288,6 +293,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
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)
});
});

});