OnedocLabs / dev-local

A small repository for local PDF development with Fileforge
https://www.fileforge.com
Apache License 2.0
10 stars 1 forks source link

Error during PDF conversion: ReferenceError: File is not defined #1

Open moe12572 opened 4 days ago

moe12572 commented 4 days ago

I keep getting an error when running npm run render:

import "dotenv/config";

import fs from "fs";
import { FileforgeClient } from '@fileforge/client';
import { compile } from "@fileforge/react-print";
import React from "react";

import { Document } from "./Document";

const ff = new FileforgeClient({
  apiKey:"process.env.FILEFORGE_API_KEY", // replace with your API key
});

(async () => {
  try {
    const HTML = await compile(<Document name="World" />)

    const pdf = await ff.pdf.generate(
      [new File([HTML], "index.html", {
        type: "text/html",
      }),
       new File([fs.readFileSync(__dirname + "/images/fileforge_cover.png")], "fileforge_cover.png", {
        type: "image/png",  
       })
    ],
      {
        options: {
          host: false,
          test: false,
        },
      }
    );

    pdf.pipe(fs.createWriteStream("output.pdf"));
  } catch (error) {
    console.error("Error during PDF conversion:", error);
  }
})();

Screenshot 2024-07-03 at 11 54 51 PM

Titou325 commented 4 days ago

Hey @moe12572, thanks for sharing! This is due to an older version of node that doesn't implement the File API. Node >= 20 will implement it, otherwise you can import { File } from "formdata-node". It is already shipped with Fileforge, but you may wan to add it yourself. See https://www.npmjs.com/package/formdata-node