所以我正在开发一个系统,我将html转换为pdf,我选择wkhtmltopdf有很多原因,所以当我使用xampp在windows上开发时,我有以下代码,它工作得非常完美
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require '../../vendor/autoload.php';
$aname=$_POST['aname'];
$name=$_POST['name'];
$email=$_POST['email'];
$str = json_decode($_POST['wah'], true);
function load_template_to_string($file_name) {
ob_start();
include $file_name;
return ob_get_contents();
}
//snappy
use Knp\Snappy\Pdf;
$snappy = new Pdf('C://"Program Files"/wkhtmltopdf/bin/wkhtmltopdf.exe');
header('Content-type', 'application/octect-stream');
header('Content-Disposition: attachment; filename="file.pdf"');
header('Content-Transfer-Encoding', 'binary');
// echo $snappy->getOutput('http://www.google.co.uk');
$snappy->setOption('no-outline', true);
$snappy->setOption('page-size','LETTER');
$snappy->setOption('encoding', 'UTF-8');
$abc = load_template_to_string('mailtemp.php');
$pdf_data = $snappy->getOutputFromHtml($abc);
其余代码是通过 swiftmailer 发送作为附件制作的 pdf 文件,这非常有效。
但是当我将相同的文件发送到我的服务器时,我的服务器是一台 ubuntu 机器
现在的代码是这样的
require '../../vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$aname=$_POST['aname'];
$name=$_POST['name'];
$email=$_POST['email'];
$str = json_decode($_POST['wah'], true);
function load_template_to_string($file_name) {
ob_start();
include $file_name;
return ob_get_contents();
}
//snappy
use Knp\Snappy\Pdf;
$snappy = new Pdf('/usr/bin/wkhtmltopdf');
header('Content-type', 'application/octect-stream');
header('Content-Disposition: attachment; filename="file.pdf"');
header('Content-Transfer-Encoding', 'binary');
// echo $snappy->getOutput('http://www.google.co.uk');
$snappy->setOption('no-outline', true);
$snappy->setOption('page-size','LETTER');
$snappy->setOption('encoding', 'UTF-8');
$abc = load_template_to_string('mailtemp.php');
$pdf_data = $snappy->getOutputFromHtml($abc);
但是当我使用它时会导致错误,它看起来像这样
<b>Fatal error</b>: Uncaught RuntimeException: The exit status code '134' says something went wrong:
stderr: "The switch --no-outline, is not support using unpatched qt, and will be ignored.QXcbConnection: Could not connect to display
Aborted (core dumped)
"
stdout: ""
command: /usr/bin/wkhtmltopdf --lowquality --page-size 'LETTER' --no-outline --encoding 'UTF-8' '/tmp/knp_snappy5a574d59b2e0f8.38598858.html' '/tmp/knp_snappy5a574d59b2e8a4.82962094.pdf'. in /var/www/html/flightsystem/vendor/knplabs/knp-snappy/src/Knp/Snappy/AbstractGenerator.php:378
Stack trace:
#0 /var/www/html/flightsystem/vendor/knplabs/knp-snappy/src/Knp/Snappy/AbstractGenerator.php(177): Knp\Snappy\AbstractGenerator->checkProcessStatus(134, '', 'The switch --no...', '/usr/bin/wkhtml...')
#1 /var/www/html/flightsystem/vendor/knplabs/knp-snappy/src/Knp/Snappy/Pdf.php(63): Knp\Snappy\AbstractGenerator->generate(Array, '/tmp/knp_snappy...', Array, false)
#2 /var/www/html/flightsystem/vendor/knplabs/knp-snappy/src/Knp/Snappy/AbstractGenerator.php(221): Knp\Snapp in <b>/var/www/html/flightsystem/vendor/knplabs/knp-snappy/src/Knp/Snappy/AbstractGenerator.php</b> on line <b>378</b><br />