Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html2canvas not loaded when is injected as a option #2713

Closed
jlopezxs opened this issue Mar 27, 2020 · 5 comments
Closed

html2canvas not loaded when is injected as a option #2713

jlopezxs opened this issue Mar 27, 2020 · 5 comments

Comments

@jlopezxs
Copy link

Error "html2canvas not loaded." when html2canvas is not in global but is as a option.

The error is here:

https://github.com/MrRio/jsPDF/blob/master/src/modules/html.js#L489

Proposal

Worker.prototype.isHtml2CanvasLoaded = function() {
    if (!this.opt.html2canvas || !typeof global.html2canvas !== "undefined") {
        throw new Error("html2canvas not loaded.");
    }
    return result;
};

Example:

import html2canvas from 'html2canvas';
import JsPDF from 'jspdf';

const pdf = new JsPDF({
  orientation: 'p',
  unit: 'mm',
  format: 'a4',
  compress: true,
});

pdf.html(document.body, {
      html2canvas,
      callback: (doc) => doc.save('filename.pdf'),
});
@HackbrettXXX
Copy link
Collaborator

HackbrettXXX commented Mar 30, 2020

You are right. This is a bug and should be fixed. Could you prepare a pull request with your proposed solution? Maybe replace !typeof global.html2canvas !== "undefined" with typeof global.html2canvas === "undefined".

@ivankoleda
Copy link

This is what I came with:

  Worker.prototype.isHtml2CanvasLoaded = function() {
    var result = typeof this.opt.html2canvas === "function"
      || typeof global.html2canvas !== "undefined";
    if (!result) {
      throw new Error("html2canvas not loaded.");
    }
    return result;
  };

this.opt.html2canvas is {} by default so I suppose check should be on typeof this.opt.html2canvas === "function". It should work, but this.opt.html2canvas seem to be initialized after the first call of isHtml2CanvasLoaded so this function still throws an error . But I struggled to dive deeper, maybe someone will find it useful.

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

@HackbrettXXX
Copy link
Collaborator

I have to revoke my initial answer. The html2canvas property in the options object is for passing options to html2canvas not for passing the html2canvas function. With #2804, the html method will now load html2canvas dynamically using the respective module format of the loaded jsPDF bundle. With that I think it is not necessary to pass html2canvas to the html method anymore.

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants