toeverything/AFFiNE · error

canvas cannot be converted to image when printing pdf, this

Error message

canvas cannot be converted to image when printing pdf, this may be because of CORS policy

What it means

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.

Source

Thrown at blocksuite/affine/shared/src/utils/print-to-pdf.ts:170

      // convert all canvas to image
      const canvasImgObjectUrlMap = new Map<string, string>();
      const allCanvas = findAllCanvases(rootElement);
      let canvasKey = 1;
      const canvasToKeyMap = new Map<HTMLCanvasElement, string>();

      for (const canvas of allCanvas) {
        const key = canvasKey.toString();
        canvasToKeyMap.set(canvas, key);
        canvasKey++;
        const canvasImgObjectUrl = await new Promise<Blob | null>(resolve => {
          try {
            canvas.toBlob(resolve);
          } catch {
            resolve(null);
          }
        });
        if (!canvasImgObjectUrl) {
          console.warn(
            'canvas cannot be converted to image when printing pdf, this may be because of CORS policy'
          );
          continue;
        }
        canvasImgObjectUrlMap.set(key, URL.createObjectURL(canvasImgObjectUrl));
      }

      // Recursive deep clone that flattens Shadow DOM into Light DOM
      const deepCloneWithShadows = (node: Node): Node => {
        const clone = doc.importNode(node, false);

        if (
          clone instanceof HTMLCanvasElement &&
          node instanceof HTMLCanvasElement
        ) {
          const key = canvasToKeyMap.get(node);
          if (key) {
            clone.dataset['printToPdfCanvasKey'] = key;

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Serve canvas resources with proper CORS headers.
  2. Rasterize cross-origin content before printing.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: 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.

Common situations: 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.


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/3d64d23386b03bcc. Report an issue: GitHub.