Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
moe12572 opened this issue Jul 4, 2024 · 1 comment
Open

Comments

@moe12572
Copy link

moe12572 commented Jul 4, 2024

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
Copy link
Member

Titou325 commented Jul 4, 2024

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants