0

嘿伙计们,我在安装 wkhtmltopdf 和 php wkhtmltopdf 时遇到了困难。我需要先将它放在我的本地服务器上,但稍后必须将它安装在 Web 服务器上。我首先在网站上的项目文件夹中安装了 wkhtmltopdf,然后我通过 composer 安装了 php wkhtmltopdf 1,这是我以前从未使用过的。所以我不完全确定我是否正确安装了它,但它看起来很简单,我认为这不一定是问题所在。当我运行测试 php 脚本时,我收到以下错误消息:

警告:proc_open(): CreateProcess failed, error code - 2 in C:\xampp\htdocs\delete\vendor\mikehaertl\php-shellcommand\src\Command.php 在第 313 行无法运行命令 cd "wkhtmltopdf\bin" && wkhtmltopdf.exe "C:\Users\Robert\AppData\Local\Temp\tmpE437.tmp.html" "C:\Users\Robert\AppData\Local\Temp\tmpE438.tmp.pdf"

这是我的测试代码:

 /**
 * 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();
}

有谁知道是什么问题?我需要从本地服务器中执行 wkhtmltopdf,因为它需要在线工作。我发现了另一个有用的堆栈溢出主题,我将链接2,我完成了所有步骤,但无法正确执行它。任何帮助将非常感激!:)

php wkhtmltopdf 堆栈溢出 - 安装 php wkhtmltopdf

4

1 回答 1

1

从命令二进制路径中删除 .exe

添加'useExec' => true选项

检查此代码:

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();
}
于 2017-03-05T00:09:28.843 回答