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
- Do not call toDataURL on jsPDF's canvas; use doc.output('datauristring') or doc.save() for PDF output instead
- If a library requires toDataURL, render to a real HTML canvas first, then pass that canvas to addImage
- Use doc.canvas.getContext('2d') to draw, then doc.save('file.pdf') to produce the PDF
- 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
- Never call toDataURL on jsPDF's canvas object (doc.canvas)
- Use doc.output('datauristring') or doc.save() for PDF output
- Render to a real HTML canvas first if you need raster image output
- When integrating charting libraries, provide a real canvas, not jsPDF's canvas
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
- Given canvas must have data. Canvas width: {element.width},
- arcTo not implemented.
- toDataUrl not implemented.
AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13).
Data as JSON: /api/errors/99a1c297dbbc1b73.
Report an issue: GitHub.