{"record":{"id":"3d64d23386b03bcc","repo":"toeverything/AFFiNE","slug":"canvas-cannot-be-converted-to-image-when-printing","errorCode":null,"errorMessage":"canvas cannot be converted to image when printing pdf, this may be because of CORS policy","messagePattern":"canvas cannot be converted to image when printing pdf, this may be because of CORS policy","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"blocksuite/affine/shared/src/utils/print-to-pdf.ts","lineNumber":170,"sourceCode":"      // convert all canvas to image\n      const canvasImgObjectUrlMap = new Map<string, string>();\n      const allCanvas = findAllCanvases(rootElement);\n      let canvasKey = 1;\n      const canvasToKeyMap = new Map<HTMLCanvasElement, string>();\n\n      for (const canvas of allCanvas) {\n        const key = canvasKey.toString();\n        canvasToKeyMap.set(canvas, key);\n        canvasKey++;\n        const canvasImgObjectUrl = await new Promise<Blob | null>(resolve => {\n          try {\n            canvas.toBlob(resolve);\n          } catch {\n            resolve(null);\n          }\n        });\n        if (!canvasImgObjectUrl) {\n          console.warn(\n            'canvas cannot be converted to image when printing pdf, this may be because of CORS policy'\n          );\n          continue;\n        }\n        canvasImgObjectUrlMap.set(key, URL.createObjectURL(canvasImgObjectUrl));\n      }\n\n      // Recursive deep clone that flattens Shadow DOM into Light DOM\n      const deepCloneWithShadows = (node: Node): Node => {\n        const clone = doc.importNode(node, false);\n\n        if (\n          clone instanceof HTMLCanvasElement &&\n          node instanceof HTMLCanvasElement\n        ) {\n          const key = canvasToKeyMap.get(node);\n          if (key) {\n            clone.dataset['printToPdfCanvasKey'] = key;","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/b4c8548c09da21b2898443559a5b846f0ccf5dd8/blocksuite/affine/shared/src/utils/print-to-pdf.ts#L152-L188","documentation":"Warning logged in `printToPdf` when a canvas's toBlob throws while converting canvases to images for the print iframe — normally a SecurityError because the canvas is tainted by cross-origin (CORS-restricted) content. That canvas is omitted from the printed output; the remaining canvases are still converted and printing proceeds.","triggerScenarios":"Triggered during print-to-PDF when canvas.toBlob fails or returns null because the canvas is tainted by cross-origin content, so the canvas is skipped in the print output.","commonSituations":"Occurs when printing pages containing canvases that drew cross-origin images without CORS. Those canvases appear blank in the PDF; load images with crossOrigin set.","solutions":["Serve canvas resources with proper CORS headers.","Rasterize cross-origin content before printing."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"b4c8548c09da21b2898443559a5b846f0ccf5dd8","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}