{"record":{"id":"99a1c297dbbc1b73","repo":"parallax/jsPDF","slug":"todataurl-is-not-implemented","errorCode":null,"errorMessage":"toDataURL is not implemented.","messagePattern":"toDataURL is not implemented\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/canvas.js","lineNumber":137,"sourceCode":"      return null;\n    }\n    for (key in contextAttributes) {\n      if (this.pdf.context2d.hasOwnProperty(key)) {\n        this.pdf.context2d[key] = contextAttributes[key];\n      }\n    }\n    this.pdf.context2d._canvas = this;\n    return this.pdf.context2d;\n  };\n\n  /**\n   * The toDataURL() method is just a stub to throw an error if accidently called.\n   *\n   * @name toDataURL\n   * @function\n   */\n  Canvas.prototype.toDataURL = function() {\n    throw new Error(\"toDataURL is not implemented.\");\n  };\n\n  jsPDFAPI.events.push([\n    \"initialized\",\n    function() {\n      this.canvas = new Canvas();\n      this.canvas.pdf = this;\n    }\n  ]);\n\n  return this;\n})(jsPDF.API);\n","sourceCodeStart":119,"sourceCodeEnd":150,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/canvas.js#L119-L150","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ndoc.canvas.toDataURL(); // throws - not implemented\n\n// after - use real HTML canvas for toDataURL, jsPDF for PDF output\nvar realCanvas = document.createElement('canvas');\n// ... draw on realCanvas ...\nvar dataUrl = realCanvas.toDataURL('image/png'); // works on real canvas\ndoc.addImage(dataUrl, 'PNG', 10, 10);\n// for PDF output from jsPDF canvas:\ndoc.save('output.pdf');","handlingStrategy":"validation","validationCode":"// Check if canvas is jsPDF's canvas (not HTML canvas) before calling toDataURL\nfunction isJsPDFCanvas(canvas) {\n  return canvas &&\n    typeof canvas.getContext === 'function' &&\n    canvas.pdf instanceof jsPDF &&\n    !canvas instanceof HTMLCanvasElement;\n}\n\n// For PDF output, use doc.output() instead\ndoc.save('output.pdf'); // instead of doc.canvas.toDataURL()","typeGuard":"/**\n * @param {*} canvas\n * @returns {boolean}\n */\nfunction isHTMLCanvas(canvas) {\n  return canvas instanceof HTMLCanvasElement;\n}","tryCatchPattern":"// No try-catch needed - avoid calling toDataURL on jsPDF canvas\n// If a third-party library calls toDataURL, intercept it:\nvar origCanvas = doc.canvas;\ndoc.canvas.toDataURL = function() {\n  return doc.output('datauristring');\n};","preventionTips":["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"],"tags":["canvas","not-implemented","api-mismatch","stub"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}