pdfme / pdfme

A TypeScript based PDF generator library, made with React.
https://pdfme.com
MIT License
2.16k stars 192 forks source link

ReferenceError: generate is not defined #468

Open wonjongjang opened 1 month ago

wonjongjang commented 1 month ago

Describe the bug

If you run that code in React, an error will occur. Why?

To Reproduce

import type { Template } from '@pdfme/common';
import { BLANK_PDF } from '@pdfme/common';
import { generate } from '@pdfme/generator';

const template: Template = {
            basePdf: BLANK_PDF,
            schemas: [
                {
                    a: {
                        type: 'text',
                        position: { x: 0, y: 0 },
                        width: 10,
                        height: 10,
                    },
                    b: {
                        type: 'text',
                        position: { x: 10, y: 10 },
                        width: 10,
                        height: 10,
                    },
                    c: {
                        type: 'text',
                        position: { x: 20, y: 20 },
                        width: 10,
                        height: 10,
                    },
                },
            ],
        };

        const inputs = [{ a: 'a1', b: 'b1', c: 'c1' }];

        generate({ template, inputs }).then((pdf: Uint8Array) => {
            console.log('!', pdf);

            // Browser
            // const blob = new Blob([pdf.buffer], { type: 'application/pdf' });
            // window.open(URL.createObjectURL(blob));

            // Node.js
            // fs.writeFileSync(path.join(__dirname, `test.pdf`), pdf);
        });

Expected behavior

-

Your Environment

- @pdfme/common: "^3.4.3"
- @pdfme/generator: "^3.4.3"
- @pdfme/ui: "^3.4.3"

Your Error Log

ReferenceError: generate is not defined

Additional context

-

hand-dot commented 1 month ago

Hey @wonjongjang Can you give me a reproducable repo?

n-at-han-k commented 1 month ago

Mozilla defines a ReferenceError as:

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced.

Seems to me that your error lies with import { generate } from '@pdfme/generator'; The function generate isn't referencing anything.

Seems strange as I would expect you to get an error during the import, but as hand-dot says, it's hard to reproduce without an error as the code above should work.

Seems to me the error is not within the PDFme code but within your project, causing generate to reference nothing.