Skip to content

render_pdf_page fails in Node.js: "Cannot read properties of undefined (reading 'createElement')" #43

Description

@augusto-afontes

Bug Report

Description

The render_pdf_page tool fails on Windows with the following error:
Cannot read properties of undefined (reading 'createElement')

This happens because the rendering code calls document.createElement('canvas'), which is a browser-only DOM API that does not exist in any version of Node.js. As a result, document is undefined at runtime and the call throws.

Environment

  • OS: Windows 11
  • Node.js: v24.16.0
  • Plugin: pdf-filler-simple (Claude Desktop Extension)

Steps to Reproduce

  1. Install the extension in Claude Desktop
  2. Ask Claude to use render_pdf_page on any PDF file
  3. The tool returns the error above

Root Cause

The code is using document.createElement('canvas') as if it were running in a browser. Since the plugin runs in a Node.js process, document is undefined.

The @napi-rs/canvas package (already listed as a dependency) provides createCanvas() specifically for Node.js environments and does not require the DOM. The fix is to replace:

// ❌ Browser-only — breaks in Node.js
const canvas = document.createElement('canvas');

with:

// ✅ Node.js compatible
const { createCanvas } = require('@napi-rs/canvas');
const canvas = createCanvas(width, height);

Additional Notes

All other tools in the plugin (merge_pdfs, read_pdf_content, split_pdf, etc.) work correctly. Only render_pdf_page is affected.

After running npm install from scratch (removing node_modules and package-lock.json), the original module-not-found error for @napi-rs/canvas-win32-x64-msvc was resolved. The createElement error only appeared after that fix, confirming the native binding now loads correctly — the issue is purely in the canvas initialization logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions