Skip to content

Commit

Permalink
Fix latest changes (parallax#2390)
Browse files Browse the repository at this point in the history
* Correct parameter initialization

* Update node-examples
  • Loading branch information
Uzlopak authored Apr 5, 2019
1 parent 3fdfbbf commit 6cacd7f
Show file tree
Hide file tree
Showing 26 changed files with 117 additions and 82 deletions.
3 changes: 2 additions & 1 deletion definitely_typed/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,8 @@ declare module 'jspdf' {
//jsPDF plugin: Outline
outline: Outline;
// jsPDF plugin: fileloading
loadFile(url: string): string;
loadFile(url: string, sync?: true): string;
loadFile(url: string, sync: false, callback: (data: string) => string): void;

// jsPDF plugin: html
html(src: string | HTMLElement, options?: HTMLOptions): Promise<HTMLWorker>;
Expand Down
7 changes: 7 additions & 0 deletions definitely_typed/jspdf-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,11 @@ function test_addImage() {

var doc = new jsPDF();
doc.addImage({imageData: '/image.png', x: 0, y: 0, width: 100, height: 100});
}

function test_loadFile() {

var doc = new jsPDF();
doc.loadFile('../image.png');
doc.loadFile('../image.png', false, function (data) {return data;});
}
Binary file added examples/MathJax/mathjax_caligraphicregular.ttf
Binary file not shown.
Binary file added examples/MathJax/mathjax_frakturregular.ttf
Binary file not shown.
Binary file added examples/MathJax/mathjax_mainitalic.ttf
Binary file not shown.
Binary file added examples/MathJax/mathjax_mainregular.ttf
Binary file not shown.
Binary file added examples/MathJax/mathjax_mathitalic.ttf
Binary file not shown.
Binary file added examples/MathJax/mathjax_sansserifregular.ttf
Binary file not shown.
Binary file added examples/images/Octocat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/Octocat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/images/Octonyan.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions examples/js/images.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions examples/node/Octocat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require('fs')
const jsPDF = require('../../dist/jspdf.node.min')

var Octocat = fs.readFileSync('../images/Octocat.png', {encoding: 'latin1'});

var doc = new jsPDF();

doc.setFontSize(40);
doc.text("Octocat loves jsPDF", 40, 30, 4);
doc.addImage(Octocat, 'test', 10, 40, 180, 180, undefined, 'SLOW');

fs.writeFileSync('./Octocat.pdf', doc.output(), 'ascii');
8 changes: 4 additions & 4 deletions examples/node/arabic.js

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions examples/node/japanese.js

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions examples/node/simple.js

This file was deleted.

16 changes: 6 additions & 10 deletions examples/outline/test_outline.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

<title>Outline Test</title>

<script src='../../libs/require/require.js'></script>
<script src='../../dist/jspdf.debug.js'></script>
<script src='../js/test_harness.js'></script>

<script>
require_baseUrl_override = '../..';
require(['../../libs/require/config'], function(){
require(['plugins/outline', 'examples/js/test_harness'], function(){

</head>

<body style='background-color: silver; margin: 0;'>
<script>
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.text(20, 20, 'Hello');
pdf.addPage();
Expand All @@ -42,13 +43,8 @@

pdf_test_harness_init(pdf, "Open in Reader (or Firefox) to see the PDF outline");

}); // require
}); // require

</script>

</head>

<body style='background-color: silver; margin: 0;'>
</body>
</html>
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@babel/core": "^7.2.2",
"@babel/plugin-external-helpers": "7.2.0",
"@babel/preset-env": "^7.4.2",
"acorn": "^6.0.0",
"babel-plugin-rewire-exports": "1.0.1",
"codeclimate-test-reporter": "0.5.1",
"diff": "3.5.0",
Expand All @@ -46,9 +47,7 @@
"inquirer": "^6.2.2",
"jasmine": "^3.3.1",
"jasmine-core": "3.4.0",
"jasmine-diff": "^0.1.3",
"jasmine-expect": "^4.0.0",
"jasmine-matchers": "^0.2.3",
"js-yaml": "3.13.0",
"jsdoc": "3.5.5",
"karma": "4.0.1",
Expand Down
28 changes: 22 additions & 6 deletions spec/fileloading.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@

/* global describe, it, jsPDF, comparePdf, expect */
/* global describe, it, jsPDF, expect */

describe('Module: FileLoad', () => {
var global = (typeof self !== "undefined" && self || typeof window !== "undefined" && window || typeof global !== "undefined" && global || Function('return typeof this === "object" && this.content')() || Function('return this')());

if (global.isNode === true) {
if (typeof global === 'object' && global.isNode === true) {
return;
}

var successURL = '/base/spec/reference/success.txt';
it('should load a file (sync)', () => {
const doc = jsPDF()
var file = doc.loadFile('/base/spec/reference/success.txt', true, 1);
var file = doc.loadFile(successURL, undefined, undefined);
expect(file).toEqual('success');
})

it('should fail to load a file (sync)', () => {
const doc = jsPDF()
var file = doc.loadFile('fail.txt', undefined, undefined);
expect(file).toEqual(undefined);
})


it('should load a file (async)', (done) => {
const doc = jsPDF()
doc.loadFile('/base/spec/reference/success.txt', false, function (data) {
doc.loadFile(successURL, false, function (data) {
expect(data).toEqual('success');
done();
});
})

it('should fail to load a file (async)', (done) => {
const doc = jsPDF()
doc.loadFile('fail.txt', false, function (data) {
expect(data).toEqual(undefined);
done();
});
})
})
4 changes: 0 additions & 4 deletions spec/helpers/DiffHelper.js

This file was deleted.

Binary file added spec/reference/xmpmetadata.pdf
Binary file not shown.
1 change: 0 additions & 1 deletion spec/support/jasmine.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
],
"helpers": [
"helpers/NodeHelper.js",
"helpers/DiffHelper.js",
"utils/compare.js"
],
"stop-on-failure": false,
Expand Down
9 changes: 9 additions & 0 deletions spec/xmpmetadata.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* global describe, it, jsPDF, comparePdf */

describe('Module: xmp_metadata', () => {
it('make some metadata', () => {
var doc = new jsPDF({ putOnlyUsedFonts: true });
doc.addMetadata("My metadata as a string.", "http://my.namespace.uri/");
comparePdf(doc.output(), 'xmpmetadata.pdf')
});
});
32 changes: 23 additions & 9 deletions src/modules/addimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,15 +597,29 @@
*
* @returns jsPDF
*/
jsPDFAPI.addImage = function (imageData, format, x, y, w, h, alias, compression, rotation) {
// backwards compatibility
if (typeof format !== 'string') {
var tmp = h;
h = w;
w = y;
y = x;
x = format;
format = tmp;
jsPDFAPI.addImage = function () {
var imageData, format, x, y, w, h, alias, compression, rotation;

imageData = arguments[0];
if (typeof arguments[1] === 'number') {
format = UNKNOWN;
x = arguments[1];
y = arguments[2];
w = arguments[3];
h = arguments[4];
alias = arguments[5];
compression = arguments[6];
rotation = arguments[7];
} else {
format = arguments[1];
x = arguments[2];
y = arguments[3];
w = arguments[4];
h = arguments[5];
alias = arguments[6];
compression = arguments[7];
rotation = arguments[8];

}

if (typeof imageData === 'object' && !isDOMElement(imageData) && "imageData" in imageData) {
Expand Down
20 changes: 9 additions & 11 deletions src/modules/fileloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
jsPDFAPI.loadFile = function (url, sync, callback) {
sync = (sync === false) ? false : true;
callback = typeof callback === 'function' ? callback : function () { };
var result;
var result = undefined;

var xhr = function (url, sync, callback) {
var request = new XMLHttpRequest();
Expand All @@ -48,25 +48,23 @@

if (sync === false) {
request.onload = function () {
callback(sanitizeUnicode(this.responseText));
if (request.status === 200) {
callback(sanitizeUnicode(this.responseText));
} else {
callback(undefined);
}
};
}
request.send(null);

if (sync) {
if (request.status !== 200) {
// eslint-disable-next-line no-console
console.warn('Unable to load file "' + url + '"');
return;
}
if (sync && request.status === 200) {
return sanitizeUnicode(request.responseText);
}
}
try {
result = xhr(url, sync, callback);
} catch (e) {
result = undefined;
}
// eslint-disable-next-line no-empty
} catch (e) {}
return result;
};

Expand Down

0 comments on commit 6cacd7f

Please sign in to comment.