Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (root, factory) {
- if (typeof define === 'function' && define.amd) {
- define('html2pdf', ['html2canvas', 'jspdf'], factory);
- } else {
- root.html2pdf = factory(root.html2canvas, root.jsPDF);
- }
- }(typeof self !== 'undefined' ? self : this, function (html2canvas, jsPDF) {
- var func = {
- parseInput: function (source, opt) {
- opt.jsPDF = opt.jsPDF || {};
- opt.html2canvas = opt.html2canvas || {};
- opt.filename = opt.filename && utils.objType(opt.filename) === 'string' ? opt.filename : 'file.pdf';
- opt.enableLinks = opt.hasOwnProperty('enableLinks') ? opt.enableLinks : true;
- opt.image = opt.image || {};
- opt.image.type = opt.image.type || 'jpeg';
- opt.image.quality = opt.image.quality || 0.95;
- switch (utils.objType(opt.margin)) {
- case 'undefined':
- opt.margin = 0;
- case 'number':
- opt.margin = [opt.margin, opt.margin, opt.margin, opt.margin];
- break;
- case 'array':
- if (opt.margin.length === 2) {
- opt.margin = [opt.margin[0], opt.margin[1], opt.margin[0], opt.margin[1]];
- }
- if (opt.margin.length === 4) {
- break;
- }
- default:
- throw 'Invalid margin array.';
- }
- if (!source) {
- throw 'Missing source element or string.';
- } else if (utils.objType(source) === 'string') {
- source = utils.createElement('div', {
- innerHTML: source
- });
- } else if (utils.objType(source) === 'element') {
- source = utils.cloneNode(source, opt.html2canvas.javascriptEnabled);
- } else {
- throw 'Invalid source - please specify an HTML Element or string.';
- }
- return source;
- },
- makeContainer: function (source, pageSize) {
- var overlayCSS = {
- position: 'fixed',
- overflow: 'hidden',
- zIndex: 1000,
- left: 0,
- right: 0,
- bottom: 0,
- top: 0,
- backgroundColor: 'rgba(0,0,0,0.8)'
- };
- var containerCSS = {
- position: 'absolute',
- width: pageSize.inner.width + pageSize.unit,
- left: 0,
- right: 0,
- top: 0,
- height: 'auto',
- margin: 'auto',
- backgroundColor: 'white'
- };
- overlayCSS.opacity = 0;
- var overlay = utils.createElement('div', {
- className: 'html2pdf__overlay',
- style: overlayCSS
- });
- var container = utils.createElement('div', {
- className: 'html2pdf__container',
- style: containerCSS
- });
- container.appendChild(source);
- overlay.appendChild(container);
- document.body.appendChild(overlay);
- var pageBreaks = source.querySelectorAll('.html2pdf__page-break');
- var pxPageHeight = pageSize.inner.height * pageSize.k / 72 * 96;
- Array.prototype.forEach.call(pageBreaks, function (el) {
- el.style.display = 'block';
- var clientRect = el.getBoundingClientRect();
- el.style.height = pxPageHeight - (clientRect.top % pxPageHeight) + 'px';
- }, this);
- return container;
- },
- makePDF: function (canvas, pageSize, opt) {
- var ctx = canvas.getContext('2d');
- var pxFullHeight = canvas.height;
- var pxPageHeight = Math.floor(canvas.width * pageSize.inner.ratio);
- var nPages = Math.ceil(pxFullHeight / pxPageHeight);
- var pageCanvas = document.createElement('canvas');
- var pageCtx = pageCanvas.getContext('2d');
- var pageHeight = pageSize.inner.height;
- pageCanvas.width = canvas.width;
- pageCanvas.height = pxPageHeight;
- var pdf = new jsPDF(opt.jsPDF);
- for (var page = 0; page < nPages; page++) {
- if (page === nPages - 1) {
- pageCanvas.height = pxFullHeight % pxPageHeight;
- pageHeight = pageCanvas.height * pageSize.inner.width / pageCanvas.width;
- }
- var w = pageCanvas.width;
- var h = pageCanvas.height;
- pageCtx.fillStyle = 'white';
- pageCtx.fillRect(0, 0, w, h);
- pageCtx.drawImage(canvas, 0, page * pxPageHeight, w, h, 0, 0, w, h);
- if (page) pdf.addPage();
- var imgData = pageCanvas.toDataURL('image/' + opt.image.type, opt.image.quality);
- pdf.addImage(imgData, opt.image.type, opt.margin[1], opt.margin[0],
- pageSize.inner.width, pageHeight);
- if (opt.enableLinks) {
- var pageTop = page * pageSize.inner.height;
- opt.links.forEach(function (link) {
- if (link.clientRect.top > 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
- });
- }
- });
- }
- }
- pdf.save(opt.filename);
- }
- };
- var utils = {
- objType: function (obj) {
- if (typeof obj === 'undefined') return 'undefined';
- else if (typeof obj === 'string' || obj instanceof String) return 'string';
- else if (typeof obj === 'number' || obj instanceof Number) return 'number';
- else if (!!obj && obj.constructor === Array) return 'array';
- else if (obj && obj.nodeType === 1) return 'element';
- else if (typeof obj === 'object') return 'object';
- else return 'unknown';
- },
- createElement: function (tagName, opt) {
- var el = document.createElement(tagName);
- if (opt.className) el.className = opt.className;
- if (opt.innerHTML) {
- el.innerHTML = opt.innerHTML;
- var scripts = el.getElementsByTagName('script');
- for (var i = scripts.length; i-- > 0; null) {
- scripts[i].parentNode.removeChild(scripts[i]);
- }
- }
- for (var key in opt.style) {
- el.style[key] = opt.style[key];
- }
- return el;
- },
- cloneNode: function (node, javascriptEnabled) {
- var clone = node.nodeType === 3 ? document.createTextNode(node.nodeValue) : node.cloneNode(false);
- for (var child = node.firstChild; child; child = child.nextSibling) {
- if (javascriptEnabled === true || child.nodeType !== 1 || child.nodeName !== 'SCRIPT') {
- clone.appendChild(utils.cloneNode(child, javascriptEnabled));
- }
- }
- if (node.nodeType === 1) {
- if (node.nodeName === 'CANVAS') {
- clone.width = node.width;
- clone.height = node.height;
- clone.getContext('2d').drawImage(node, 0, 0);
- } else if (node.nodeName === 'TEXTAREA' || node.nodeName === 'SELECT') {
- clone.value = node.value;
- }
- clone.addEventListener('load', function () {
- clone.scrollTop = node.scrollTop;
- clone.scrollLeft = node.scrollLeft;
- }, true);
- }
- return clone;
- },
- unitConvert: function (obj, k) {
- var newObj = {};
- for (var key in obj) {
- newObj[key] = obj[key] * 72 / 96 / k;
- }
- return newObj;
- },
- getPageSize: function (orientation, unit, format) {
- if (typeof orientation === 'object') {
- var options = orientation;
- orientation = options.orientation;
- unit = options.unit || unit;
- format = options.format || format;
- }
- unit = unit || 'mm';
- format = format || 'a4';
- orientation = ('' + (orientation || 'P')).toLowerCase();
- var format_as_string = ('' + format).toLowerCase();
- var pageFormats = {
- 'a0': [2383.94, 3370.39],
- 'a1': [1683.78, 2383.94],
- 'a2': [1190.55, 1683.78],
- 'a3': [841.89, 1190.55],
- 'a4': [595.28, 841.89],
- 'a5': [419.53, 595.28],
- 'a6': [297.64, 419.53],
- 'a7': [209.76, 297.64],
- 'a8': [147.40, 209.76],
- 'a9': [104.88, 147.40],
- 'a10': [73.70, 104.88],
- 'b0': [2834.65, 4008.19],
- 'b1': [2004.09, 2834.65],
- 'b2': [1417.32, 2004.09],
- 'b3': [1000.63, 1417.32],
- 'b4': [708.66, 1000.63],
- 'b5': [498.90, 708.66],
- 'b6': [354.33, 498.90],
- 'b7': [249.45, 354.33],
- 'b8': [175.75, 249.45],
- 'b9': [124.72, 175.75],
- 'b10': [87.87, 124.72],
- 'c0': [2599.37, 3676.54],
- 'c1': [1836.85, 2599.37],
- 'c2': [1298.27, 1836.85],
- 'c3': [918.43, 1298.27],
- 'c4': [649.13, 918.43],
- 'c5': [459.21, 649.13],
- 'c6': [323.15, 459.21],
- 'c7': [229.61, 323.15],
- 'c8': [161.57, 229.61],
- 'c9': [113.39, 161.57],
- 'c10': [79.37, 113.39],
- 'dl': [311.81, 623.62],
- 'letter': [612, 792],
- 'government-letter': [576, 756],
- 'legal': [612, 1008],
- 'junior-legal': [576, 360],
- 'ledger': [1224, 792],
- 'tabloid': [792, 1224],
- 'credit-card': [153, 243]
- };
- var k;
- switch (unit) {
- case 'pt':
- k = 1;
- break;
- case 'mm':
- k = 72 / 25.4;
- break;
- case 'cm':
- k = 72 / 2.54;
- break;
- case 'in':
- k = 72;
- break;
- case 'px':
- k = 72 / 96;
- break;
- case 'pc':
- k = 12;
- break;
- case 'em':
- k = 12;
- break;
- case 'ex':
- k = 6;
- break;
- default:
- throw ('Invalid unit: ' + unit);
- }
- var pageHeight, pageWidth;
- if (pageFormats.hasOwnProperty(format_as_string)) {
- pageHeight = pageFormats[format_as_string][1] / k;
- pageWidth = pageFormats[format_as_string][0] / k;
- } else {
- try {
- pageHeight = format[1];
- pageWidth = format[0];
- } catch (err) {
- throw new Error('Invalid format: ' + format);
- }
- }
- var tmp;
- if (orientation === 'p' || orientation === 'portrait') {
- orientation = 'p';
- if (pageWidth > pageHeight) {
- tmp = pageWidth;
- pageWidth = pageHeight;
- pageHeight = tmp;
- }
- } else if (orientation === 'l' || orientation === 'landscape') {
- orientation = 'l';
- if (pageHeight > pageWidth) {
- tmp = pageWidth;
- pageWidth = pageHeight;
- pageHeight = tmp;
- }
- } else {
- throw ('Invalid orientation: ' + orientation);
- }
- var info = {
- 'width': pageWidth,
- 'height': pageHeight,
- 'unit': unit,
- 'k': k
- };
- return info;
- }
- };
- var html2pdf = function (source, opt) {
- opt = utils.objType(opt) === 'object' ? opt : {};
- source = func.parseInput(source, opt);
- var pageSize = utils.getPageSize(opt.jsPDF);
- pageSize.inner = {
- width: pageSize.width - opt.margin[1] - opt.margin[3],
- height: pageSize.height - opt.margin[0] - opt.margin[2]
- };
- pageSize.inner.ratio = pageSize.inner.height / pageSize.inner.width;
- var container = func.makeContainer(source, pageSize);
- var overlay = container.parentElement;
- if (opt.enableLinks) {
- opt.links = [];
- var links = container.querySelectorAll('a');
- var containerRect = utils.unitConvert(container.getBoundingClientRect(), pageSize.k);
- Array.prototype.forEach.call(links, function (link) {
- var clientRects = link.getClientRects();
- for (var i = 0; i < clientRects.length; i++) {
- var clientRect = utils.unitConvert(clientRects[i], pageSize.k);
- clientRect.left -= containerRect.left;
- clientRect.top -= containerRect.top;
- opt.links.push({
- el: link,
- clientRect: clientRect
- });
- }
- });
- }
- var onRendered = opt.html2canvas.onrendered || function () {};
- opt.html2canvas.onrendered = function (canvas) {
- onRendered(canvas);
- document.body.removeChild(overlay);
- func.makePDF(canvas, pageSize, opt);
- };
- html2canvas(container, opt.html2canvas);
- };
- return html2pdf;
- }));
Add Comment
Please, Sign In to add comment