duandao2083 2015-04-09 22:28
浏览 88
已采纳

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

I have made the following file:

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');

?>

When I run from the command line (Ubuntu 12.x):

sudo php test.php

I get a file that shows up in my directory and works as intended. However now take the following file where I am trying to apply the above to a real world scenario - note: when I echo $htmlString I get a valid and expected string.

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');

?>

When I try to run the above by browsing to testPDF.php as a logged in user, I get nothing. No file is generated and no error/warning in the error logs. I've tried using $pdf->send() as well but no success.

My initial thought that this is a permissions issue, however I've tried setting testPDF.php to owner root and permission 755 but it still does not resolve the issue.

  • 写回答

1条回答 默认 最新

  • dougui1977 2015-04-10 00:14
    关注

    The issue is that when sending a HTML string to the pdf object, it looks for the tag to determine if it's html.

    The first example has a HTML tag, while the 2nd file does not have any html tag thus is fails, silently.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿