Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Worker.template = {
},
opt: {
filename: 'file.pdf',
trimPages: false,
margin: [0,0,0,0],
image: { type: 'jpeg', quality: 0.95 },
enableLinks: true,
Expand Down Expand Up @@ -212,10 +213,22 @@ Worker.prototype.toPdf = function toPdf() {
pageCtx.drawImage(canvas, 0, page*pxPageHeight, w, h, 0, 0, w, h);

// Add the page to the PDF.
if (page) this.prop.pdf.addPage();
var pageWidth = this.prop.pageSize.inner.width;
const [mTop, mLeft, mBottom, mRight] = opt.margin || [];
const adjustedPageWidth = pageWidth + mLeft + mRight;
const adjustedPageHeight = pageHeight + mTop + mBottom;

this.prop.pdf.addPage(
...(opt.trimPages ? [[adjustedPageWidth, adjustedPageHeight], adjustedPageWidth > adjustedPageHeight ? 'l' : 'p'] : [])
);

var imgData = pageCanvas.toDataURL('image/' + opt.image.type, opt.image.quality);
this.prop.pdf.addImage(imgData, opt.image.type, opt.margin[1], opt.margin[0],
this.prop.pageSize.inner.width, pageHeight);
this.prop.pdf.addImage(imgData, opt.image.type, mLeft, mTop, pageWidth, pageHeight);
}

if (!this.prop._firstPageDeleted) {
this.prop.pdf.deletePage(1);
this.prop._firstPageDeleted = true;
}
});
};
Expand Down