{"record":{"id":"5f90b096800e773e","repo":"parallax/jsPDF","slug":"todataurl-not-implemented","errorCode":null,"errorMessage":"toDataUrl not implemented.","messagePattern":"toDataUrl not implemented\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/modules/context2d.js","lineNumber":1213,"sourceCode":"    if (doStackPop && this.ctxStack.length !== 0) {\n      this.ctx = this.ctxStack.pop();\n      this.fillStyle = this.ctx.fillStyle;\n      this.strokeStyle = this.ctx.strokeStyle;\n      this.font = this.ctx.font;\n      this.lineCap = this.ctx.lineCap;\n      this.lineWidth = this.ctx.lineWidth;\n      this.lineJoin = this.ctx.lineJoin;\n      this.lineDash = this.ctx.lineDash;\n      this.lineDashOffset = this.ctx.lineDashOffset;\n    }\n  };\n\n  /**\n   * @name toDataURL\n   * @function\n   */\n  Context2D.prototype.toDataURL = function() {\n    throw new Error(\"toDataUrl not implemented.\");\n  };\n\n  //helper functions\n\n  /**\n   * Get the decimal values of r, g, b and a\n   *\n   * @name getRGBA\n   * @function\n   * @private\n   * @ignore\n   */\n  var getRGBA = function(style) {\n    var rxRgb = /rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)/;\n    var rxRgba = /rgba\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*([\\d.]+)\\s*\\)/;\n    var rxTransparent = /transparent|rgba\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*0+\\s*\\)/;\n\n    var r, g, b, a;","sourceCodeStart":1195,"sourceCodeEnd":1231,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/modules/context2d.js#L1195-L1231","documentation":"toDataURL is an unconditional stub in context2d: it always throws 'toDataUrl not implemented.' (note the lowercase 'rl' in the message vs. the correct 'URL' method name). jsPDF's context2d cannot rasterize the accumulated PDF drawing back into a PNG/JPEG data URL, so the Canvas2D method is intentionally absent.","triggerScenarios":"Any call to doc.context2d.toDataURL(); generic canvas-export utilities that probe for toDataURL; snapshot/thumbnail pipelines expecting a Canvas2D-compatible surface.","commonSituations":"Treating jsPDF's context2d as a drop-in Canvas2D replacement; libraries that auto-export a context via toDataURL for previews.","solutions":["Export the document via doc.output('datauristring') or doc.output('blob') for a PDF data URL instead.","If a PNG/JPEG thumbnail is required, render the same drawing to an offscreen HTML <canvas> and call its native toDataURL, then feed that image into jsPDF via addImage.","Remove toDataURL-based code paths when targeting jsPDF context2d."],"exampleFix":"// before\nconst png = ctx.toDataURL(); // throws [105]\n\n// after: export the PDF itself as a data URL\nconst pdfDataUri = doc.output('datauristring');","handlingStrategy":"fallback","validationCode":"// toDataURL is an unconditional stub; choose the right export surface.\nfunction exportFromContext(ctx, doc) {\n  // Prefer the PDF data URL over the stubbed context2d.toDataURL.\n  if (doc && typeof doc.output === 'function') return doc.output('datauristring');\n  throw new Error('No export available');\n}","typeGuard":"// No type narrowing helps — the method exists but throws.\n// Probe capability:\nfunction contextCanToDataURL(ctx) {\n  try { ctx.toDataURL(); return true; } catch (e) { return false; }\n}","tryCatchPattern":"try {\n  return ctx.toDataURL();\n} catch (e) {\n  if (/toDataUrl not implemented/.test(e.message)) {\n    return doc.output('datauristring'); // PDF data URL instead\n  }\n  throw e;\n}","preventionTips":["Use doc.output('datauristring' | 'blob' | 'arraybuffer') for PDF export, never context2d.toDataURL.","If a raster preview is required, render to a real <canvas> and use its native toDataURL, then addImage into jsPDF."],"tags":["canvas2d","context2d","unimplemented","stub","pdf","export"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}