spiritix / php-chrome-html2pdf

A PHP library for converting HTML to PDF using Google Chrome
MIT License
112 stars 29 forks source link

Binary Error -> import getStdin from 'get-stdin'; ^^^^^^^^ SyntaxError: Unexpected identifier at Module._compile #63

Closed bretvanhorn closed 2 months ago

bretvanhorn commented 9 months ago

Hi-

Our PDF generation is suddenly failing on our staging server after working great for the last several months. I suspect this happened after we upgraded from Ubuntu 18 to 20, but I may be mistaken.

We are running:

Here is the full error:

Spiritix \ Html2Pdf\ ConverterException Binary error: /home/forge/[...]/vendor/spiritix/php-chrome-html2pdf/index.js:11 import getStdin from 'get-stdin'; ^^^^^^^^ SyntaxError: Unexpected identifier at Module._compile (internal/modules/cjs/loader.js:723:23) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

And the code we use:

public function displayPdf($type, $content, $id) {
        $input = new StringInput();
        $input->setHtml($content);
        $converter = new Converter($input, new EmbedOutput());
        $converter->setOption('landscape', false);

        $converter->setOptions([
            'printBackground' => true,
            'displayHeaderFooter' => false,
            'mediaType' => 'screen',
        ]);

        $output = $converter->convert();
        $output->embed($type.'-'.$id.'.pdf');

    }

What I have tried:

Any help is appreciated. I can't seem to find anyone with this exact error here or on the web, so I'm at a loss. Thanks.

bretvanhorn commented 9 months ago

Just an update, zeroing in on the cause, I think. I did some debugging from the vendor files and was able to output the chrome command so I could run it manually. I was able to output the $input variable in ProcessUtil.php, save it to a text file, then run the below command:

node "/home/forge/[redacted]/vendor/spiritix/php-chrome-html2pdf/src/Spiritix/Html2Pdf/../../../index" -o '{"landscape":false,"printBackground":true,"displayHeaderFooter":false,"mediaType":"screen"}' < tmp.txt > tmp.pdf

This outputs a PDF with the data from our Laravel view.

Strange thing is, I can view the $input variable in executeShellCommand() and it shows the valid HTML from our Laravel view, but once it is run through proc_open, that seems to be where the error in my post above kicks in. If I output the $result['output'] value, it just comes back as "".

I'm going to keep digging, but welcome any guidance.

bretvanhorn commented 9 months ago

Update: completely uninstalling and reinstalling/downgrading to v1.6.1 fixes the issue.

spiritix commented 7 months ago

Might be fixed in the latest release, can you please verify?

thunder809 commented 5 months ago

Hello, i have the same issue.

We are using your package for pdf generation in a magento2-module. Node v18, same error, on the current version of the package (1.7.6).

I dont´t have more insights on the specific system an OS of the customer, as i only have very limited access.

Workaround with downgrade to version 1.6.4 worked for me.

spiritix commented 5 months ago

@thunder809 Did you try to use the latest version 1.7.6? This was a known issue on 1.7.5.