Skip to content

Commit

Permalink
Move html2pdf initialization code out of Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
eKoopmans committed Feb 18, 2018
1 parent 9de659e commit 43b7043
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
18 changes: 16 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'es6-promise/auto';
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';

import Worker from './worker.js';
import './plugin/jspdf-plugin.js';
import { objType, createElement, cloneNode, unitConvert } from './utils.js';

/**
* Generate a PDF from an HTML element or string using html2canvas and jsPDF.
Expand All @@ -12,6 +13,20 @@ import { objType, createElement, cloneNode, unitConvert } from './utils.js';
* 'image' ('type' and 'quality'), and 'html2canvas' / 'jspdf', which are
* sent as settings to their corresponding functions.
*/
var html2pdf = function html2pdf(src, opt) {
// Create a new worker with the given options.
var worker = new html2pdf.Worker(opt);

if (src) {
// If src is specified, perform the traditional 'simple' operation.
return worker.from(src).save();
} else {
// Otherwise, return the worker for new Promise-based operation.
return worker;
}
}
html2pdf.Worker = Worker;

var html2pdf = function(source, opt) {
// Render the canvas and pass the result to makePDF.
var onRendered = opt.html2canvas.onrendered || function() {};
Expand All @@ -24,6 +39,5 @@ var html2pdf = function(source, opt) {
html2canvas(container, opt.html2canvas).then(done);
};


// Expose the html2pdf function.
export default html2pdf;
32 changes: 7 additions & 25 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
/* ----- BELOW CODE BELONGS IN INDEX.JS ----- */

import Worker from './worker.js';

var html2pdf = function html2pdf(src, opt) {
// opt = src ? Object.assign(opt || {}, {src: src}) : opt;
var worker = new html2pdf.Worker(opt);

if (src) {
// If src is specified, perform the traditional 'simple' operation.
worker.from(src).save();
} else {
// Otherwise, return the worker for new Promise-based operation.
return worker;
}
}
html2pdf.Worker = Worker;

/* ----- ABOVE CODE BELONGS IN INDEX.JS ----- */






import { objType, createElement, cloneNode, unitConvert } from './utils.js';

/* ----- CONSTRUCTOR ----- */
Expand Down Expand Up @@ -431,9 +406,16 @@ Worker.prototype.error = function error(msg) {
});
};


/* ----- ALIASES ----- */

Worker.prototype.using = Worker.prototype.set;
Worker.prototype.saveAs = Worker.prototype.save;
Worker.prototype.output = Worker.prototype.export;
Worker.prototype.run = Worker.prototype.then;


/* ----- FINISHING ----- */

// Expose the Worker class.
export default Worker;

0 comments on commit 43b7043

Please sign in to comment.