首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mikehaertl/phpwkhtmltopdf包装器-在命令行运行PHP文件时工作

mikehaertl/phpwkhtmltopdf包装器-在命令行运行PHP文件时工作
EN

Stack Overflow用户
提问于 2015-04-09 22:28:12
回答 1查看 790关注 0票数 0

我制作了以下文件:

test.php

代码语言:javascript
复制
<?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');

?>

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

代码语言:javascript
复制
sudo php test.php

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

testPDF.php

代码语言:javascript
复制
<?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->send(),但没有成功。

我最初的想法是,这是一个权限问题,但是我尝试过将testPDF.php设置为所有者根和权限755,但它仍然无法解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-10 00:14:55

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

第一个示例有一个HTML标记,而第二个文件没有html标记,因此是失败的。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29550159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档