CakePDF not working in a shell


#1

I’m trying to write a shell script to generate a pdf file using CakePdf. When I run the shell script it runs fine the first time, but on subsequent runs it has an error. After waiting a minute or two, I can run it again without changing any code and it runs once again. But only once, and again I have to wait a minute or two before it will work. Any ideas as to what could be causing this?

Thanks

I’m using Cake 3.3.3.

The function I call in my shell.

public function makePdf() 
{
   Configure::write('CakePdf', [
        'engine' => [
            'className' => 'CakePdf.WkHtmlToPdf',
            'binary' => '/path/to/wkhtmltox/bin/wkhtmltopdf',
        ],
        'margin' => [
            'bottom' => 0,
            'left' => 0,
            'right' => 0,
            'top' => 0
        ],
        'pageSize' => 'A6',
        'orientation' => 'landscape'
    ]);

    $CakePdf = new \CakePdf\Pdf\CakePdf();
    $CakePdf->template('template_name', 'layout_name');

    $output_file_path = ROOT . DS  . 'files' . DS  . '123.pdf';
    $CakePdf->write($output_file_path);
}

The error I see if I try to run the function twice without waiting for a minute or two.

Error: [Cake\Core\Exception\Exception] System error <pre>Exit with code 1 due to network error: ContentNotFoundError
</pre>
Stack Trace:
#0 /app/vendor/friendsofcake/cakepdf/src/Pdf/CakePdf.php(298): CakePdf\Pdf\Engine\WkHtmlToPdfEngine->output()
#1 /app/vendor/friendsofcake/cakepdf/src/Pdf/CakePdf.php(337): CakePdf\Pdf\CakePdf->output('<!DOCTYPE html>...')
#2 /app/src/Shell/PdfShell.php(91): CakePdf\Pdf\CakePdf->write('/app/path...')
#3 [internal function]: App\Shell\PdfShell->makePdf()
#4 /app/vendor/cakephp/cakephp/src/Console/Shell.php(446): call_user_func_array(Array, Array)
#5 /app/vendor/cakephp/cakephp/src/Console/ShellDispatcher.php(227): Cake\Console\Shell->runCommand(Array, true, Array)
#6 /app/vendor/cakephp/cakephp/src/Console/ShellDispatcher.php(182): Cake\Console\ShellDispatcher->_dispatch(Array)
#7 /app/vendor/cakephp/cakephp/src/Console/ShellDispatcher.php(128): Cake\Console\ShellDispatcher->dispatch(Array)
#8 /app/bin/cake.php(34): Cake\Console\ShellDispatcher::run(Array)
#9 {main}

#2

Can you generate a PDF using wkhtmltopdf directly on the command line?

I’m sure I had a similar problems a year or so ago - and it was down to a buggy version of wkhtmltopdf I was using. I installed a different version, and my problem went away.

I’m successfully using wkhtmltopdf right now with cakePDF, my version is 0.12.3 (ubuntu)