phplaravellaravel-5wkhtmltopdflaravel-snappy

laravel-snappy: file was not created


At the moment, I'm trying to trace where this issue arose from given that nothing major was changed.

But at the moment I currently use laravel-snappy to generate pdfs, I haven't had an issue until now when I am all of a sudden receiving the following errors:

The file 'C:\Users\ADMINI~1\AppData\Local\Temp\knp_snappy5a7d3011c11883.41249127.pdf' was not created (command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf" --lowquality --images --enable-javascript --javascript-delay "10" "C:\Users\ADMINI~1\AppData\Local\Temp\knp_snappy5a7d3011b9a179.91650543.html" "C:\Users\ADMINI~1\AppData\Local\Temp\knp_snappy5a7d3011c11883.41249127.pdf").

Unfortunately, it doesn't tell me why it wasn't created. At this point in time, the error handler points to this specific line where it is returning this error:

if (!$this->fileExists($output)) {
    throw new \RuntimeException(sprintf(
        'The file \'%s\' was not created (command: %s).',
        $output, $command
    ));
}

This line comes from this file: vendor\knplabs\knp-snappy\src\Knp\Snappy\AbstractGenerator.php

My wkhtmltopdf binary is located in the correct place, and nothing has changed in response to the setup of these files. And yes, at the moment these files are hosted and served on a Windows Server platform.

My config for the snappy:

<?php

return array(

    'pdf' => array(
        'enabled' => true,
        'binary' => '"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf"',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),
    'image' => array(
        'enabled' => true,
        'binary'  => '"C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage"',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),

);

My files are being generated as such through my controller:

  public function downloadPDF(Shipment $shipment) {
      $shipment_details = $shipment->shipment_details;
      $shipment->print_date = Carbon::now();
      $shipment->save();

      $pdf = PDF::loadView('shipments.pdf', compact('shipment','shipment_details'))
                    ->setOption('images', true)
                    ->setOption('enable-javascript', true)
                    ->setOption('javascript-delay', 10);
      return $pdf->download('shipment'.$shipment->uuid.'.pdf');

      $shipment->print_date = Carbon::now();
      $shipment->save();
  }

Solution

  • The easiest way to get around this is to exec a raw command, wkhtmltopdf does not have the same command line parameter on Linux/Windows, this means that the snappy wrapper only works with amd64 and fail when it's used with a 64bit windows executable.

    exec("C:/path/to/wkhtmltopdf.exe path/to/my.html destination/for/my.pdf");
    

    since this solution is terrible and that wkhtmltopdf functionality are limited on windows, I strongly recommend you to deploy with docker, or to just develop under Linux. Or else you won't be able to use multiples functionality like pdf footer, pdf encoding utf-8 and much more...

    Here's a tutorial on how to use docker compose for laravel!