diff --git a/src/worker.js b/src/worker.js index 47ee472..424480f 100644 --- a/src/worker.js +++ b/src/worker.js @@ -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, @@ -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; } }); };