diff --git a/build.js b/build.js new file mode 100644 index 000000000..a691e9ba3 --- /dev/null +++ b/build.js @@ -0,0 +1,93 @@ +var fs = require('fs'); +var rollup = require('rollup'); +var uglify = require('uglify-js'); +var babel = require('rollup-plugin-babel'); +var execSync = require('child_process').execSync; + +bundle({ + minified: 'dist/jspdf.min.js', + debug: 'dist/jspdf.debug.js' +}); + +// Monkey patching adler32 and filesaver +function monkeyPatch() { + return { + transform: function (code, id) { + var file = id.split('/').pop(); + if (file === 'adler32cs.js') { + code = code.replace(/this, function/g, 'jsPDF, function'); + code = code.replace(/require\('buffer'\)/g, '{}'); + } + return code; + } + } +} + +// Rollup removes local variables unless used within a module. +// This plugin makes sure specified local variables are preserved +// and kept local. This plugin wouldn't be necessary if es2015 +// modules would be used. +function rawjs(opts) { + opts = opts || {}; + return { + transform: function (code, id) { + var variable = opts[id.split('/').pop()]; + if (!variable) return code; + + var keepStr = '/*rollup-keeper-start*/window.tmp=' + variable + ';/*rollup-keeper-end*/'; + return code + keepStr; + }, + transformBundle: function (code) { + for (var file in opts) { + var r = new RegExp(opts[file] + '\\$\\d+', 'g'); + code = code.replace(r, opts[file]); + } + var re = /\/\*rollup-keeper-start\*\/.*\/\*rollup-keeper-end\*\//g; + return code.replace(re, ''); + } + } +} + +function bundle(paths) { + rollup.rollup({ + entry: './main.js', + plugins: [ + monkeyPatch(), + rawjs({ + 'jspdf.js': 'jsPDF', + 'filesaver.tmp.js': 'saveAs', + 'deflate.js': 'Deflater', + 'zlib.js': 'FlateStream', + 'css_colors.js': 'CssColors', + 'html2pdf.js': 'html2pdf' + }), + babel({ + presets: ['es2015-rollup'], + exclude: ['node_modules/**', 'libs/**'] + }) + ] + }).then(function (bundle) { + var code = bundle.generate({format: 'umd', moduleName: 'jspdf'}).code; + code = code.replace(/Permission\s+is\s+hereby\s+granted[\S\s]+?IN\s+THE\s+SOFTWARE\./, 'Licensed under the MIT License'); + code = code.replace(/Permission\s+is\s+hereby\s+granted[\S\s]+?IN\s+THE\s+SOFTWARE\./g, ''); + fs.writeFileSync(paths.debug, renew(code)); + + var minified = uglify.minify(code, {fromString: true, output: {comments: /@preserve|@license|copyright/i}}); + fs.writeFileSync(paths.minified, renew(minified.code)); + }).catch(function (err) { + console.error(err); + }); +} + +function renew(code) { + var date = new Date().toISOString(); + var version = require('./package.json').version; + var whoami = execSync('whoami').toString().trim(); + var commit = execSync('git rev-parse --short=10 HEAD').toString().trim(); + + code = code.replace('${versionID}', version + ' Built on ' + date); + code = code.replace('${commitID}', commit); + code = code.replace(/1\.0\.0-trunk/, version + ' ' + date + ':' + whoami); + + return code; +} \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 000000000..994746f65 --- /dev/null +++ b/main.js @@ -0,0 +1,31 @@ +import './jspdf'; + +import './plugins/acroform'; +import './plugins/addhtml'; +import './plugins/addimage'; +import './plugins/annotations'; +import './plugins/autoprint'; +import './plugins/canvas'; +import './plugins/cell'; +import './plugins/context2d'; +import './plugins/from_html'; +import './plugins/javascript'; +import './plugins/outline'; +import './plugins/png_support'; +import './plugins/prevent_scale_to_fit'; +import './plugins/split_text_to_size'; +import './plugins/standard_fonts_metrics'; +import './plugins/svg'; +import './plugins/total_pages'; + +import './node_modules/cf-blob.js/Blob.js'; +import './node_modules/filesaver.js/FileSaver.js'; +import './node_modules/adler32cs/adler32cs.js'; +import './libs/css_colors.js'; +import './libs/deflate.js'; +import './libs/html2canvas/dist/html2canvas.js'; +import './libs/png_support/png.js'; +import './libs/png_support/zlib.js'; +import './libs/polyfill.js'; + +export default jsPDF; \ No newline at end of file diff --git a/package.json b/package.json index 5a3ea4e93..935360c5c 100644 --- a/package.json +++ b/package.json @@ -16,17 +16,21 @@ "type": "git", "url": "https://github.com/MrRio/jsPDF.git" }, - "dependencies": {}, - "devDependencies": { + "dependencies": { "adler32cs": "github:chick307/adler32cs.js", "cf-blob.js": "0.0.1", - "filesaver.js": "github:andyinabox/FileSaver.js", + "filesaver.js": "github:andyinabox/FileSaver.js" + }, + "devDependencies": { + "babel-preset-es2015-rollup": "^1.1.1", "local-web-server": "^0.5.19", + "rollup": "^0.25.4", + "rollup-plugin-babel": "^2.4.0", "uglify-js": "^2.6.2" }, "scripts": { "start": "ws", - "build": "npm install && bower install && ./build.sh", + "build": "npm install && bower install && node build.js", "version": "npm run build && git add -A dist" } } diff --git a/plugins/acroform.js b/plugins/acroform.js index 9d8fe8156..ee85c1298 100644 --- a/plugins/acroform.js +++ b/plugins/acroform.js @@ -6,8 +6,10 @@ * http://opensource.org/licenses/mit-license */ -(AcroForm = function (jsPDFAPI) { +(window.AcroForm = function (jsPDFAPI) { 'use strict'; + + var AcroForm = window.AcroForm; AcroForm.scale = function (x) { return (x * (acroformPlugin.internal.scaleFactor / 1));// 1 = (96 / 72) @@ -217,10 +219,10 @@ var key = i; var form = fieldArray[i]; // Start Writing the Object - this.internal.newObjectDeferredBegin(form.objId); + this.internal.newObjectDeferredBegin(form && form.objId); var content = ""; - content += (form.getString()); + content += form ? form.getString() : ''; this.internal.out(content); delete fieldArray[key]; @@ -402,6 +404,8 @@ }; })(jsPDF.API); +var AcroForm = window.AcroForm; + AcroForm.internal = {}; AcroForm.createFormXObject = function (formObject) { @@ -757,10 +761,10 @@ AcroForm.internal.toPdfString = function (string) { string = string || ""; // put Bracket at the Beginning of the String - if (String.indexOf('(', 0) !== 0) { + if (string.indexOf('(') !== 0) { string = '(' + string; } - + if (string.substring(string.length - 1) != ')') { string += '('; } @@ -1043,7 +1047,8 @@ AcroForm.Field = function () { Object.defineProperty(this, 'hasAppearanceStream', { enumerable: false, - configurable: true + configurable: true, + writable: true }); }; AcroForm.Field.FieldNum = 0; @@ -1088,9 +1093,11 @@ AcroForm.ChoiceField = function () { configurable: false }); this.hasAppearanceStream = true; - this.V.get = function () { - AcroForm.internal.toPdfString(); - }; + Object.defineProperty(this, 'V', { + get: function() { + AcroForm.internal.toPdfString(); + } + }); }; AcroForm.internal.inherit(AcroForm.ChoiceField, AcroForm.Field); window["ChoiceField"] = AcroForm.ChoiceField; diff --git a/plugins/from_html.js b/plugins/from_html.js index ec9b447eb..1bdccc261 100644 --- a/plugins/from_html.js +++ b/plugins/from_html.js @@ -33,6 +33,7 @@ DrillForContent, FontNameDB, FontStyleMap, + TextAlignMap, FontWeightMap, FloatMap, ClearMap, @@ -943,7 +944,7 @@ } //if we have to move the cursor to adapt the indent var indentMove = 0; - var indentMore = 0; + var wantedIndent = 0; //if a margin was added (by e.g. a text-alignment), move the cursor if (line[0][1]["margin-left"] !== undefined && line[0][1]["margin-left"] > 0) { wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]);