Rob-Nasty
Rob-Nasty

Reputation: 11

Installing php wkhtmltopdf on a local server

Hey guys I am having difficulty installing wkhtmltopdf and php wkhtmltopdf. I need to put this on my local server first, but it will later have to be installed on a web server. I installed wkhtmltopdf first in my project folder from the website, I next installed php wkhtmltopdf1 via composer, which I have never used before. So I am not entirely sure that I installed it right but it seems straightforward and I don't think that is necessarily the problem. When I run a test php script I get this error message :

Warning: proc_open(): CreateProcess failed, error code - 2 in C:\xampp\htdocs\delete\vendor\mikehaertl\php-shellcommand\src\Command.php on line 313 Could not run command cd "wkhtmltopdf\bin" && wkhtmltopdf.exe "C:\Users\Robert\AppData\Local\Temp\tmpE437.tmp.html" "C:\Users\Robert\AppData\Local\Temp\tmpE438.tmp.pdf"

Here is my test code:

 /**
 * Register Composer Autloader
 */
define('VENDOR_DIR', __DIR__ . '\vendor' . DIRECTORY_SEPARATOR);

if (!is_file(VENDOR_DIR . 'autoload.php')) {
    throw new \RuntimeException(
        '[Error] Bootstrap: Could not find "vendor/autoload.php".' . PHP_EOL .
        'Did you forget to run "composer install --dev"?' . PHP_EOL
    );
}

require VENDOR_DIR . 'autoload.php';

/**
 * Use library
 */

use mikehaertl\wkhtmlto\Pdf;


$pdf = new Pdf(array(
    'binary' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe',
    'ignoreWarnings' => true,
    'commandOptions' => array(
        'procEnv' => array(
            // Check the output of 'locale' on your system to find supported languages
            'LANG' => 'en_US.utf-8',
        ),
        'escapeArgs' => false,
        'procOptions' => array(
            // This will bypass the cmd.exe which seems to be recommended on Windows
            'bypass_shell' => true,
            // Also worth a try if you get unexplainable errors
            'suppress_errors' => true,
        ),
    ),
));

$pdf->addPage('<html>
<head>
</head>
<body>

    <div id="print-area">
        <div id="header">
            This is an example header.
        </div>
        <div id="content">
            <h1>Demo</h1>
            <p>This is example content</p>
        </div>
        <div id="footer">
            This is an example footer.
        </div>
    </div>

</body>
</html>');

if (!$pdf->saveAs('page.pdf')) {
    echo $pdf->getError();
}

Does anyone know what the problem could be? I need to execute wkhtmltopdf from within the local server because it needs to work online. I found another helpful stack overflow topic that I will link2, I went through all the steps but was unable to execute it properly. Any help would be much appreciated! :)

php wkhtmltopdf stack overflow -installing php wkhtmltopdf

Upvotes: 1

Views: 2543

Answers (1)

Fadel
Fadel

Reputation: 711

Remove .exe from the command binary path

Add 'useExec' => true option

Check this code :

include __DIR__ . '/../vendor/autoload.php';

use mikehaertl\wkhtmlto\Pdf;

$pdf = new Pdf(array(
    'binary' => 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf',
    'ignoreWarnings' => true,
    'commandOptions' => array(
        'useExec' => true,
        'procEnv' => array(
            'LANG' => 'en_US.utf-8',
        ),
        'escapeArgs' => false,
        'procOptions' => array(
            'bypass_shell' => true,
            'suppress_errors' => true,
        ),
    ),
));

$pdf->addPage('<html><body><h1>test</h1></body></html>');

if (!$pdf->saveAs('page.pdf')) {
    echo $pdf->getError();
}

Upvotes: 2

Related Questions