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: node:internal/fs/utils:348 throw err; ^ Error: EPERM: operation not permitted #39

Closed vincemammoliti closed 2 years ago

vincemammoliti commented 2 years ago

I am trying to get this package up and running with a simple example but am unable to get past an error. My development environment: Windows 11, Node v18.7.0, npm v8.15.0, Laravel 9.

The error is from $converter->convert():

Spiritix \ Html2Pdf \ ConverterException
Binary error: node:internal/fs/utils:348 throw err; ^ Error: EPERM: operation not permitted, lstat 'C:\Users\me' at Object.realpathSync (node:fs:2552:7) at toRealPath (node:internal/modules/cjs/loader:405:13) at tryFile (node:internal/modules/cjs/loader:401:10) at tryExtensions (node:internal/modules/cjs/loader:413:22) at Module._findPath (node:internal/modules/cjs/loader:569:20) at resolveMainPath (node:internal/modules/run_main:19:25) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:75:24) at node:internal/main/run_main_module:17:47 { errno: -4048, syscall: 'lstat', code: 'EPERM', path: 'C:\\Users\\me' } Node.js v18.7.0

This is the code I'm using in a Laravel controller, similar to the example provided in the readme:

<?php

namespace App\Http\Controllers;

use Spiritix\Html2Pdf\Converter;
use Spiritix\Html2Pdf\Input\UrlInput;
use Spiritix\Html2Pdf\Output\DownloadOutput;

class DownloadController extends Controller
{
    public function __invoke()
    {
        $input = new UrlInput();
        $input->setUrl('http://www.example.com');

        $converter = new Converter($input, new DownloadOutput());

        $converter->setNodePath('C:\nodejs\node.exe');

        $converter->setOption('landscape', true);

        $converter->setOptions([
            'printBackground' => true,
            'displayHeaderFooter' => true,
            'headerTemplate' => '<p>I am a header</p>',
        ]);

        $output = $converter->convert();
        return $output->download('example.pdf');
    }
}
spiritix commented 2 years ago

This looks like a permission issue. Generally, the library works on Windows. You can try to re-install the dependency manually:

npm cache clean --force
npm install puppeteer --force

Are other node modules working on your system? Especially such that require file system access? If not, you might have to setup permissions properly first.

vincemammoliti commented 2 years ago

Thanks for your response. I've just resolved it now and the issue was related to Node and IIS config/directory permissions.

Despite setting up the correct permissions to run my Laravel application from C:\Users{username}{subfolders}\my-app\ , when running node.js as the user identity associated with the IIS application pool it caused a permissions error on the C:\Users{username}\ directory.

After moving my application code from my Documents directory to C:\inetpub\apps\my-app, and setting up the same IIS user permissions, it is now working.

For reference, this answer on a different problem helped me get to that solution: https://stackoverflow.com/a/68937635