Convert an excel to pdf using phpSpreadsheet

12,003

Solution 1

$vendorDirPath = realpath(__DIR__ . '/vendor');
if (file_exists($vendorDirPath . '/autoload.php')) {
    require $vendorDirPath . '/autoload.php';
} else {
    throw new Exception(
        die("somthing went wrong");
    );
}

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile("190321_CNGD0N0242017_RatingShell.xls");
$phpWord = $reader->load("190321_CNGD0N0242017_RatingShell.xls");
use \PhpOffice\PhpSpreadsheet\Style\Border;

$phpWord ->getDefaultStyle()->applyFromArray(
            [
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => Border::BORDER_THIN,
                        'color' => ['rgb' => '000000'],
                    ],
                ]
            ]
        );

$xmlWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($phpWord,'Mpdf');

$xmlWriter->writeAllSheets();
$xmlWriter->setFooter("Sdfsdf");
$num = rand(00, 99);
//create folder named files
$xmlWriter->save("files/helloWorld$num.pdf");

Solution 2

If you are done with the missing Mpdf library you may encounter the error below:

Unable to `set PDF file protection, CSPRNG Functions are not available. Use paragonie/random_compat polyfill or upgrade to PHP 7.`

unless of course you are using PHP 7.

Share:
12,003
G. Cassini
Author by

G. Cassini

Updated on June 04, 2022

Comments

  • G. Cassini
    G. Cassini almost 2 years

    I'm using phpSpreadsheet to convert an Excel file already in memory to a pdf, but I'm obtaining this fatal error message.

    Here the message

    Fatal error: Uncaught exception 'PhpOffice\PhpSpreadsheet\Writer\Exception' with message 'Could not open file /pdf_finali/mario.pdf for writing.' in /membri/cassiodb/FEDEGARIPROVE/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Pdf.php:260 Stack trace: #0 /membri/cassiodb/FEDEGARIPROVE/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php(33): PhpOffice\PhpSpreadsheet\Writer\Pdf->prepareForSave('/pdf_finali/mar...') #1 /membri/cassiodb/FEDEGARIPROVE/excel_to_pdf.php(29): PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf->save('/pdf_finali/mar...') #2 {main} thrown in /membri/cassiodb/FEDEGARIPROVE/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Pdf.php on line 260
    

    Here the code

    <?php
    
    require 'vendor/autoload.php';
    
    
    use PhpOffice\PhpSpreadsheet\IOFactory;
    use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    
    //Inizio blocco solo per test standalone
    $new_excel_path = "prova_inserimento.xlsx" ;
    $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
    $spreadsheet = $reader->load("$new_excel_path"); 
    //Fine blocco solo per test standalone
    
    echo 'Fino alla creazione del foglio tutto bene <br> <br><br><br>';
    
    //Conversione della variabile spreadsheet in pdf
    
    
        //Creazione del writer
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
    
        //Salvataggio del pfd
        //$pdf_path = 'pdf_finali/'.$name.'.pdf';
        $pdf_path = 'pdf_finali/mario.pdf';
        echo '<br>';
        echo $pdf_path;
        $writer->save($pdf_path);?>
    

    EDIT:

    I found a solution to the previous problem (I have edited also the code above because I had written badly the path where to save the file), but now I'm obtaining this.

     Fatal error: Class 'Mpdf\Mpdf' not found in /membri/cassiodb/FEDEGARIPROVE/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php on line 20 
    
    • userfuser
      userfuser about 6 years
      How did you install the PhpSpreadsheet - with Composer or somehow manually? If you did it with composer, it should have had installed the missing Mpdf library. Did you follow these docs: phpspreadsheet.readthedocs.io/en/develop ?
    • Christopher Smit
      Christopher Smit over 5 years
      I am getting the same issue. I did not install using composer. How do I fix this?
  • Justo
    Justo over 4 years
    This fixs by installing github.com/paragonie/random_compat via composer