php-chrome-html2pdf
php-chrome-html2pdf copied to clipboard
Binary Error -> import getStdin from 'get-stdin'; ^^^^^^^^ SyntaxError: Unexpected identifier at Module._compile
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:
- Laravel 10.44
- PHP 8.2
- Node 18.19.1
- NPM 10.2.4
- Ubuntu 20.04.6 LTS
- "spiritix/php-chrome-html2pdf": "^1.7.5"
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:
- Installed and verified working Chrome on the command line via dpkg
- Updated the server software
- Updated packages
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.
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.
Update: completely uninstalling and reinstalling/downgrading to v1.6.1 fixes the issue.
Might be fixed in the latest release, can you please verify?
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.
@thunder809 Did you try to use the latest version 1.7.6? This was a known issue on 1.7.5.