diff --git a/src/index.js b/src/index.js index e56f908..2124232 100644 --- a/src/index.js +++ b/src/index.js @@ -43,60 +43,6 @@ var html2pdf = function(source, opt) { html2canvas(container, opt.html2canvas).then(done); }; -html2pdf.makePDF = function(canvas, pageSize, opt) { - // Calculate the number of pages. - var ctx = canvas.getContext('2d'); - var pxFullHeight = canvas.height; - var pxPageHeight = Math.floor(canvas.width * pageSize.inner.ratio); - var nPages = Math.ceil(pxFullHeight / pxPageHeight); - - // Create a one-page canvas to split up the full image. - var pageCanvas = document.createElement('canvas'); - var pageCtx = pageCanvas.getContext('2d'); - var pageHeight = pageSize.inner.height; - pageCanvas.width = canvas.width; - pageCanvas.height = pxPageHeight; - - // Initialize the PDF. - var pdf = new jsPDF(opt.jsPDF); - - for (var page=0; page pageTop && link.clientRect.top < pageTop + pageSize.inner.height) { - var left = opt.margin[1] + link.clientRect.left; - var top = opt.margin[0] + link.clientRect.top - pageTop; - pdf.link(left, top, link.clientRect.width, link.clientRect.height, { url: link.el.href }); - } - }); - } - } - - // Finish the PDF. - pdf.save( opt.filename ); -} - // Expose the html2pdf function. export default html2pdf; diff --git a/src/worker.js b/src/worker.js index 68192af..8e93d61 100644 --- a/src/worker.js +++ b/src/worker.js @@ -180,7 +180,7 @@ Worker.prototype.toImg = function toImg() { return this.then(function() { return prereq(reqs); }).then(function() { - var imgData = this.canvas.toDataURL('image/' + opt.image.type, opt.image.quality); + var imgData = this.canvas.toDataURL('image/' + this.opt.image.type, this.opt.image.quality); this.img = document.createElement('img'); this.img.src = imgData; }); @@ -196,7 +196,61 @@ Worker.prototype.toPdf = function toPdf() { return this.then(function() { return prereq(reqs); }).then(function() { - // TODO: Transfer PDF code from index.js. + // Create local copies of frequently used properties. + var canvas = this.canvas; + var opt = this.opt; + + // Calculate the number of pages. + var ctx = canvas.getContext('2d'); + var pxFullHeight = canvas.height; + var pxPageHeight = Math.floor(canvas.width * this.pageSize.inner.ratio); + var nPages = Math.ceil(pxFullHeight / pxPageHeight); + + // Define pageHeight separately so it can be trimmed on the final page. + var pageHeight = this.pageSize.inner.height; + + // Create a one-page canvas to split up the full image. + var pageCanvas = document.createElement('canvas'); + var pageCtx = pageCanvas.getContext('2d'); + pageCanvas.width = canvas.width; + pageCanvas.height = pxPageHeight; + + // Initialize the PDF. + var this.pdf = new jsPDF(opt.jsPDF); + + for (var page=0; page pageTop && + link.clientRect.top < pageTop + this.pageSize.inner.height) { + var left = opt.margin[1] + link.clientRect.left; + var top = opt.margin[0] + link.clientRect.top - pageTop; + this.pdf.link(left, top, link.clientRect.width, link.clientRect.height, { url: link.el.href }); + } + }); + } + } }); };