Skip to content

How to use MuPDF in a Cloudflare Worker for PDF transformations? #142

@yuriyward

Description

@yuriyward

Hi everyone,

I’m trying to create a Cloudflare Worker that uses MuPDF for PDF transformations. However, I’m running into some issues when trying to import and use the MuPDF library. Here’s what I’ve tried so far:

Using import:

import { Document } from "mupdf";

This results in the following error:

Uncaught Error: not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)

Using require:

const mupdf = require("mupdf");

This results in the following error:

✘ [ERROR] This require call is not allowed because the imported file "node_modules/mupdf/dist/mupdf.js" contains a top-level await

The top-level await in "node_modules/mupdf/dist/mupdf.js" is here:

node_modules/mupdf/dist/mupdf.js:24:17:
24 │ const libmupdf = await libmupdf_wasm();

Questions:

  1. Is it possible to use MuPDF in a Cloudflare Worker? If so, how can I properly import and use it?
  2. Are there any specific configurations or build steps I need to follow to make MuPDF work in this environment?
  3. Are there any alternative libraries or approaches you would recommend for PDF transformations in a Cloudflare Worker?

Additional Context:

  • I’m using the latest version of MuPDF.
  • My Cloudflare Worker is set up with Wrangler.
  • I’m relatively new to Cloudflare Workers and working with WASM in this context.

Any help or guidance would be greatly appreciated! Thank you in advance.


Steps to Reproduce:

  1. Set up a new Cloudflare Worker project using Wrangler.
  2. Install MuPDF using npm:
    npm install mupdf
  3. Attempt to import MuPDF in the worker script using either import or require.

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