wkhtmltopdf with snappy error

2.3k Views Asked by At

so i was developing a system where i was converting html to pdf and i choose wkhtmltopdf for many reasons , so when i was developing on windows using xampp i had following code and it works absolutely perfectly

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

rest of code is to send the pdf made as an attachment via swiftmailer and this works absolutely fine .

but when i send the same documents to my server which is a ubuntu machine

and code is now this

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

but when i use this there is an error caused and it looks like this

<b>Fatal error</b>:  Uncaught RuntimeException: The exit status code '134' says something went wrong:
stderr: &quot;The switch --no-outline, is not support using unpatched qt, and will be ignored.QXcbConnection: Could not connect to display 
Aborted (core dumped)
&quot;
stdout: &quot;&quot;
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-&gt;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-&gt;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 />
1

There are 1 best solutions below

0
On

The error message says

The switch --no-outline, is not supported

on your setup.

So I suggest you do not use it (unless you have a reason to).

Just remove the following line:

$snappy->setOption('no-outline', true);