parallax/jsPDF · error · Error

toDataURL is not implemented.

Error message

toDataURL is not implemented.

What it means

jsPDF provides a Canvas shim object (doc.canvas) that bridges Canvas 2D API calls to jsPDF's context2d drawing commands. However, toDataURL is intentionally not implemented because jsPDF generates PDF output, not raster image data — there is no bitmap buffer to encode. The method exists only to throw a clear error if called accidentally, rather than failing silently or returning undefined.

Source

Thrown at src/modules/canvas.js:137

      return null;
    }
    for (key in contextAttributes) {
      if (this.pdf.context2d.hasOwnProperty(key)) {
        this.pdf.context2d[key] = contextAttributes[key];
      }
    }
    this.pdf.context2d._canvas = this;
    return this.pdf.context2d;
  };

  /**
   * The toDataURL() method is just a stub to throw an error if accidently called.
   *
   * @name toDataURL
   * @function
   */
  Canvas.prototype.toDataURL = function() {
    throw new Error("toDataURL is not implemented.");
  };

  jsPDFAPI.events.push([
    "initialized",
    function() {
      this.canvas = new Canvas();
      this.canvas.pdf = this;
    }
  ]);

  return this;
})(jsPDF.API);

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Do not call toDataURL on jsPDF's canvas; use doc.output('datauristring') or doc.save() for PDF output instead
  2. If a library requires toDataURL, render to a real HTML canvas first, then pass that canvas to addImage
  3. Use doc.canvas.getContext('2d') to draw, then doc.save('file.pdf') to produce the PDF
  4. If intercepting third-party library calls, monkey-patch or override toDataURL to call doc.output() instead

Example fix

// before
doc.canvas.toDataURL(); // throws - not implemented

// after - use real HTML canvas for toDataURL, jsPDF for PDF output
var realCanvas = document.createElement('canvas');
// ... draw on realCanvas ...
var dataUrl = realCanvas.toDataURL('image/png'); // works on real canvas
doc.addImage(dataUrl, 'PNG', 10, 10);
// for PDF output from jsPDF canvas:
doc.save('output.pdf');
Defensive patterns

Strategy: validation

Validate before calling

// Check if canvas is jsPDF's canvas (not HTML canvas) before calling toDataURL
function isJsPDFCanvas(canvas) {
  return canvas &&
    typeof canvas.getContext === 'function' &&
    canvas.pdf instanceof jsPDF &&
    !canvas instanceof HTMLCanvasElement;
}

// For PDF output, use doc.output() instead
doc.save('output.pdf'); // instead of doc.canvas.toDataURL()

Type guard

/**
 * @param {*} canvas
 * @returns {boolean}
 */
function isHTMLCanvas(canvas) {
  return canvas instanceof HTMLCanvasElement;
}

Try / catch

// No try-catch needed - avoid calling toDataURL on jsPDF canvas
// If a third-party library calls toDataURL, intercept it:
var origCanvas = doc.canvas;
doc.canvas.toDataURL = function() {
  return doc.output('datauristring');
};

Prevention

When it happens

Trigger: Calling doc.canvas.toDataURL() on jsPDF's built-in canvas object. Passing jsPDF's canvas to a library (e.g., html2canvas, chart libraries) that calls toDataURL internally. Code originally written for HTML canvas that was adapted to use jsPDF's canvas without removing toDataURL calls.

Common situations: Integrating charting libraries (Chart.js, etc.) that call canvas.toDataURL() to snapshot their output. Code that was written for HTML5 Canvas and later switched to jsPDF. Third-party libraries that probe canvas capabilities by calling toDataURL. Attempting to export jsPDF's canvas to an <img> tag.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/99a1c297dbbc1b73. Report an issue: GitHub.