mikehaertl / phpwkhtmltopdf wrapper - 从命令行运行PHP文件时工作

时间:2015-04-09 22:28:12

标签: php wkhtmltopdf

我制作了以下文件:

test.php的

<?php

require 'vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;

$htmlString = <<<EOT
<!DOCTYPE html>
    test
</html>    
EOT;

$pdf = new Pdf($htmlString);
$pdf->setOptions(array(
    'orientation' => 'landscape'
));

$pdf->saveAs('new.pdf');

?>

当我从命令行(Ubuntu 12.x)运行时:

sudo php test.php

我收到一个显示在我的目录中并按预期工作的文件。但是现在采取以下文件,我试图将上述内容应用于现实世界的场景 - 注意:当我回显$ htmlString时,我得到一个有效且预期的字符串。

testPDF.php

<?php
    // Start session and check if user is logged in
    session_start();
    if(!isset($_SESSION['permission']))
    {
        die('Please login.');
    }
    // Dependencies
    require 'vendor/autoload.php';
    use mikehaertl\wkhtmlto\Pdf;
    require 'database.php';
    // Initialize
    $client = $db->real_escape_string($_GET['client']);
    $start_date = $db->real_escape_string($_GET['start_date']);
    $end_date = $db->real_escape_string($_GET['end_date']);
    $jobs = array();
    $htmlString = '';
    // Get list of jobs from DB & save to array
    $query = "SELECT somecolumns FROM mydatabase WHERE start_date > '{$start_date}' AND end_date < '{$end_date}' AND client = '{$client}'";
    if(!$result = $db->query($query))
    {
        die('There was an error running the query [' . $db->error . ']');
    }
    while($r = mysqli_fetch_assoc($result)) 
    {
        $jobs[] = $r;
    }
    // Loop through jobs array and formulate HTML string
    foreach($jobs as $value)
    {
        $id = $value['id'];
        $name = $value['name'];
        $tech = $value['tech'];
        $time_closed = $value['time_closed'];
        $time_total = $value['time_total'];
        $time_charged = $value['time_charged'];
        $description = $value['description'];

        $htmlString = $htmlString . <<<EOT
                <h4 style="text-align: center;">{$id} - {$name}</h5>
                    <table id="simple-table" class="table table-striped table-bordered table-hover">
                        <thead>
                            <tr>
                                <th style="width: 180px;">Date</th>
                                <th>Description</th>
                                <th style="width: 50px;">Duration</th>
                                <th style="width: 150px;">Time Charged</th>
                                <th style="width: 150px;">Technician</th>
                            </tr>
                        </thead>
                        <tbody id="jobs_start">
                            <tr>
                                <td>{$time_closed}</td>
                                <td>{$description}</td>
                                <td>{$time_total}</td>
                                <td>{$time_charged}</td>
                                <td>{$tech}</td>
                            </tr>
                        </tbody>
                    </table>
EOT;
    }

    $pdf = new Pdf($htmlString);
    $pdf->setOptions(array(
        'orientation' => 'landscape'
    ));

    $pdf->saveAs('new.pdf');

?>

当我尝试通过浏览testPDF.php作为登录用户来运行上述操作时,我什么都没得到。错误日志中没有生成文件且没有错误/警告。我尝试过使用$ pdf-&gt; send(),但没有成功。

我最初认为这是权限问题,但是我已经尝试将testPDF.php设置为所有者root和权限755,但它仍然无法解决问题。

1 个答案:

答案 0 :(得分:0)

问题在于,当向pdf对象发送HTML字符串时,它会查找标记以确定它是否为html。

第一个示例有一个HTML标记,而第二个文件没有任何html标记,因此无声地失败。

https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php