在本地服务器上安装php wkhtmltopdf

时间:2016-10-12 20:42:28

标签: php composer-php wkhtmltopdf phpwkhtmltopdf

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

  

警告:proc_open():第313行的C:\ xampp \ htdocs \ delete \ vendor \ mikehaertl \ php-shellcommand \ src \ Command.php中的CreateProcess失败,错误代码为2   无法运行命令cd" wkhtmltopdf \ bin" &安培;&安培; wkhtmltopdf.exe" C:\ Users \ Robert \ AppData \ Local \ Temp \ tmpE437.tmp.html" " C:\用户\罗伯特\应用程序数据\本地\ 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 stack overflow -installing php wkhtmltopdf

1 个答案:

答案 0 :(得分:0)

  

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

     

添加&#39; useExec&#39; =&GT;真实选项

检查此代码:

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();
}
相关问题