0

I'm trying to convert excel file directly to PDF with the help of PHPSpreadsheet. but got an error "Class 'Mpdf\Mpdf' not found", Here is my code:

<?php 
  require 'vendor/autoload.php';
  use PhpOffice\PhpSpreadsheet\IOFactory;
  use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;

  $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  $reader->setReadDataOnly(true);
  $spreadsheet = $reader->load("excel.xlsx");


  // when I use this MPDF class, I'm getting an error Class \Mpdf\Mpdf not found

  $writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
  $writer->save("excel.pdf");

Kindly Help with above code.

Thanks

Arun Kumar
  • 111
  • 2
  • 3
  • 14

1 Answers1

4

From the documentation of phpspreadsheet:

You must now install a PDF rendering library yourself; but PhpSpreadsheet will work with a number of different libraries.

The error comes from not installing the rendering library you want to use (in this case mPDF). Just add mpdf/mpdf to your composer dependencies.

BlobbyBob
  • 457
  • 3
  • 10
  • Thanks for reply @BlobbyBob. I was installed PHPSpreadsheet with composer and all there pdf library also install withing phpspreadsheet. – Arun Kumar Mar 20 '18 at 08:30
  • Apparently it isn't correctly installed because `\Mpdf\Mpdf` can't be found. Try to add it manually and run `composer update` – BlobbyBob Mar 20 '18 at 09:13
  • @ArunKumar You shouldn't put it manually in any directory. Just run `composer require mpdf/mpdf` and everything should work – BlobbyBob Mar 20 '18 at 09:51
  • @BlobbBob My excel file convert successfully to pdf but I'm Getting "Warning: unlink(C:\Windows\Temp\ib9CEB.tmp): Permission denied in C:\wamp\www\api\vendor\mpdf\mpdf\src\Cache.php on line 69" error. – Arun Kumar Mar 20 '18 at 11:22