Search code examples
node.jscommandnode-html-pdf

Node js html-pdf command not working from PHP


I am trying to create a PDF file from a HTML for which I am using npm html-pdf package. When I am trying to run this command from terminal

html-pdf ticket-2.html ticketpdf.pdf

its working fine and PDF is creating successfully.But when I am trying to run this command from a PHP file then its showing this error sh: html-pdf: command not found.

$ret = exec("html-pdf ticket-2.html ticketpdf.pdf", $out, $err);
print_r($out);

Output: Array ( [0] => sh: html-pdf: command not found )

Can anyone help me on this where this issue is exactly?

Thanks in advance


Solution

  • It seems your php script is not able to access the path where html-pdf is installed. For Windows, It will be installed in C:\Users\<username>\AppData\Roaming\npm assuming you would have installed it globally. Set this environment path in the scope of php script. It should work.

    Edited: Try adding $_ENV["PATH"]="/home/designed/bin"; before $ret = exec("html-pdf ticket-2.html ticketpdf.pdf", $out, $err);