Jspdf: Does jspdf even work server side ?

Created on 7 Jan 2019  Â·  15Comments  Â·  Source: MrRio/jsPDF

###Does jspdf even work server side ?

Same Issue #566 #1891 :(

node -v 
v11.5.0

"jspdf": "^1.5.3",

/home/unknown/.nvm/versions/node/v11.5.0/lib/node_modules/ts-node/src/index.ts:261
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
test-jspdf.ts(18,8): error TS2339: Property 'window' does not exist on type 'Global'.
test-jspdf.ts(19,8): error TS2339: Property 'navigator' does not exist on type 'Global'.
test-jspdf.ts(20,8): error TS2339: Property 'btoa' does not exist on type 'Global'.

at createTSError (/home/unknown/.nvm/versions/node/v11.5.0/lib/node_modules/ts-node/src/index.ts:261:12)
at getOutput (/home/unknown/.nvm/versions/node/v11.5.0/lib/node_modules/ts-node/src/index.ts:367:40)
at Object.compile (/home/unknown/.nvm/versions/node/v11.5.0/lib/node_modules/ts-node/src/index.ts:558:11)
at Module.m._compile (/home/unknown/.nvm/versions/node/v11.5.0/lib/node_modules/ts-node/src/index.ts:439:43)
at Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/unknown/.nvm/versions/node/v11.5.0/lib/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)

Even if it is run directly in JS, we have following issue

ReferenceError: html2pdf is not defined
    at /home/unknown/Projects/project-api/node_modules/jspdf/dist/jspdf.min.js:202:16291
    at /home/unknown/Projects/project-api/node_modules/jspdf/dist/jspdf.min.js:1:124
    at Object.<anonymous> (/home/unknown/Projects/project-api/node_modules/jspdf/dist/jspdf.min.js:1:128)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)

Most helpful comment

All 15 comments

Would you atleast try jspdf.node.debug.js?

Would you atleast try jspdf.node.debug.js?

With this following code snippet

let jsPdfDebug = require('jspdf/dist/jspdf.node.debug')
var doc = new jsPdfDebug()

This minimalist example throws the error, (whatever we use jspdf or jspdf.node.debug)

ReferenceError: window is not defined

That's why I asked if this library works server side ?

Even if we use this workaround before the require lib

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};

We had another error:
ReferenceError: html2pdf is not defined

and where in jspdf.node is html2pdf referenced?

and where in jspdf.node is html2pdf referenced?

no that error was thrown by jspdf.

If I continue using jspdf.node.debug, the following error is:
ReferenceError: saveAs is not defined

The code I use is

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};
let jsPdfDebug = require('jspdf/dist/jspdf.node.debug')

var doc = new jsPdfDebug();
doc.text('Hello world!', 10, 10)
doc.save('a4.pdf')

And that not works if I not set global.window

Can you provide a server side working example ?

It seems that jspdf depends on browser functions? no?

Seems like you are using jspdf.debug.js and not jspdf.node.debug.js

save will not work, because you have to use node own save routines to save the data to a file.

Seems like you are using jspdf.debug.js and not jspdf.node.debug.js
save will not work, because you have to use node own save routines to save the data to a file.

I wrote the snippet code used just above and as you can see it s jspdf.node.debug which is called, nothing else.

Did you at least run this snippet code and succeed to make it works to answer that ?

I found different threads on stackoverflow or here from users facing the same issue. And it is not really a matter if jsPdf works only front side because JsPdf is already one of the greatest JS lib for generating PDF

I just would like to know if it worked even server side, and if Yes, How? with which node and jspdf versions ? And if so and once more, can you share a server side working snippet code ?

Did you solve it?

No, I had to change for another lib.
I believe that Jspdf is very well for browser pdf generation only at the moment.

@mohamed-badaoui What other lib are you using for server-side pdf generation?

I was able to get it working...but only a little further than @mohamed-badaoui. So I added the library and got the error html2pdf is not defined. So I did

global.html2pdf = require(html2pdf');

and the error went away...but now I get the following

ReferenceError: Blob is not defined
    at l.__private__.getBlob (/app/node_modules/jspdf/dist/jspdf.min.js:29:21377)
    at Object.l.save (/app/node_modules/jspdf/dist/jspdf.min.js:29:35467)

when I try to save the document.

I think I'm just going to use something else. I really wanted to use the autoTable plugin...but it seems like nobody has been able to make it work on the server...

@arasabbasi Do you have an example/sample code that DOES work on the server?

I think I'm going to use voilab-pdf-table instead

build the current code with npm run-script build and then see run the examples/node

After running npm run build, I get the following:

Error: Cannot find module '/app/node_modules/jspdf/build.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Sorry, just gonna try with something else.

You have to clone the repository and then run in your console npm run-script build. If you do it in your project-folder then there is no wonder, that it doesnt work ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

allenksun picture allenksun  Â·  3Comments

tarekis picture tarekis  Â·  4Comments

mackersD picture mackersD  Â·  4Comments

yankeeBrit picture yankeeBrit  Â·  3Comments

Pinank picture Pinank  Â·  3Comments