mikehaertl/phpwkhtmltopdf 包装器 - 当来自命令行的 运行 PHP 文件时工作
mikehaertl/phpwkhtmltopdf wrapper - Works when running PHP file from command line
我制作了以下文件:
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 来尝试 运行 上述操作时,我什么也没得到。没有生成文件,错误日志中也没有 error/warning。我也尝试过使用 $pdf->send() 但没有成功。
我最初认为这是一个权限问题,但是我已经尝试将 testPDF.php 设置为所有者 root 和权限 755 但它仍然没有解决问题。
问题是,当向 pdf 对象发送 HTML 字符串时,它会查找标签以确定它是否是 html。
第一个示例有一个 HTML 标签,而第二个文件没有任何 html 标签,因此失败,没有提示。
https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php
我制作了以下文件:
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 来尝试 运行 上述操作时,我什么也没得到。没有生成文件,错误日志中也没有 error/warning。我也尝试过使用 $pdf->send() 但没有成功。
我最初认为这是一个权限问题,但是我已经尝试将 testPDF.php 设置为所有者 root 和权限 755 但它仍然没有解决问题。
问题是,当向 pdf 对象发送 HTML 字符串时,它会查找标签以确定它是否是 html。
第一个示例有一个 HTML 标签,而第二个文件没有任何 html 标签,因此失败,没有提示。
https://github.com/mikehaertl/phpwkhtmltopdf/blob/master/src/Pdf.php