eKoopmans / html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.
MIT License
4.17k stars 1.39k forks source link

Cannot set property 'getPageSize' of undefined in version 0.10.0 #464

Closed markusv closed 3 years ago

markusv commented 3 years ago

Hi,

I just updated to jspdf version 0.10.0. Unfortunately it does not work for me. I get this error:

Cannot set property 'getPageSize' of undefined

Skjermbilde 2021-08-31 kl  12 46 28

this is my code:

const input = document.getElementById('root'); html2pdf(input);

I have installed html2pdf.js with npm and use webpack to package my project

bhiapp4 commented 3 years ago

Getting same error in our angular project. when running npm audit fix it pulls version 0.10.0.

ghost commented 3 years ago

getting similar error as well

Screen Shot 2021-08-31 at 9 28 16 PM

new

markusv commented 3 years ago

My webpack bundles the html2pdf.js file. This file does not seem to contain the jspdf library. I think that is due to the webpack config in use in html2pdf.js. The html2pdf.js bundle created here has jspdf and html2canvas marked as externals here . This means webpack will not include them in the bundle called html2pdf.js. What is the html2pdf.js bundle meant for? Using the html2pdf.bundle.js works.

eKoopmans commented 3 years ago

Okay, so an update. First some info:

Now I'm seeing two big issues. First is what @markusv points out, which is that Webpack is consuming html2pdf.js when importing/requiring, which it absolutely shouldn't be - it should be respecting the main and module fields of package.json to tell it which file to consume.

Second is that, independent of that (?), the externals are not working as intended. In the .es.js and .cjs.js versions, dependencies are not bundled directly, but are left as imports/requires. This way consumers of html2pdf have some control over the specific versions of jsPDF and html2canvas being used, we don't end up with multiple versions, and things can be more optimally bundled.

Again, all of this was working with rollup in v0.9.3. I'm working on finding out what's going wrong with the Webpack config.

eKoopmans commented 3 years ago

I think I've found a solution. My understanding of how Webpack consumes browser/main/module packages was a bit off, so in many cases it's going to use the browser file (html2pdf.js). This was true on v0.9.3 with the rollup packages as well.

With rollup, html2pdf.js was packaged as UMD, and the externals were done in a way that worked both directly in the browser (expecting globals) and in Node (expecting requires). I'm able to modify how externals are done in Webpack to match that pattern, which should resolve the issue.

I'll try to do an update this evening.

ghost commented 3 years ago

thanks @eKoopmans for looking into this! looking forward to the fix.

markusv commented 3 years ago

It works fine now. Thanks for the quick fix, @eKoopmans 👍