PDF из XLS/XLSX/ODS с помощью библиотеки PhpOffice\PhpSpreadsheet
<?php
require_once $this->dir.'/classes/PhpSpreadsheet/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader;
use PhpOffice\PhpSpreadsheet\Writer;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\IOFactory;
$tpl = $this->dir.'/tpl/shopzakaz.xls';
$reader = new Reader\Xls();
//$reader = new Reader\Xlsx();
$pExcel = $reader->load($tpl);
$pExcel->setActiveSheetIndex(0);
$aSheet = $pExcel->getActiveSheet();
// Лого-шапка
$pictloc = $this->dir.'/tpl/head.png';
if(file_exists($pictloc)){
$Drawing = new Drawing();
$Drawing->setPath($pictloc);
$Drawing->setCoordinates('A1');
$Drawing->setOffsetX(0);
$Drawing->setOffsetY(0);
$Drawing->setWorksheet($aSheet);
$Drawing->setResizeProportional(true);
$Drawing->setWidthAndHeight(2481, 324);
}
// Дата и номер заказа
$cell = $aSheet->getCell('A6');
$val = $cell->getValue();
$val = str_replace('{num}', $order->id, $val);
$val = str_replace('{date}', date('d.m.Y', strtotime($order->datecreate)), $val);
$cell->setValue($val);
// Строки
$lineTop = 21;
$root = rtrim(JUri::root(), '/');
foreach($basket as $b => $bpos){
$line = $lineTop + $b;
if($line > $lineTop) $aSheet->insertNewRowBefore($line);
$aSheet->getCell('A'.$line)->setValue($b+1);
$aSheet->getCell('D'.$line)->setValue($this->money($bpos->price));
$aSheet->getCell('E'.$line)->setValue($this->money($bpos->skidka));
$aSheet->getCell('F'.$line)->setValue($bpos->qnt);
$aSheet->getCell('G'.$line)->setValue($this->money($bpos->totalprice));
if(!isset($items[$bpos->itemid])) continue;
$item = &$items[$bpos->itemid];
$picturl = $root.'/images/shop/product/'.$item->image;
$pictloc = JPATH_SITE.'/images/shop/product/'.$item->image;
if(!file_exists($pictloc)){
$picturl = $root.'/images/shop/nophoto.png';
$pictloc = JPATH_SITE.'/images/shop/nophoto.png';
}
if(file_exists($pictloc)){
$Drawing = new Drawing();
$Drawing->setPath($pictloc);
$Drawing->setCoordinates('B'.$line);
$Drawing->setOffsetX(0);
$Drawing->setOffsetY(0);
$hyp = $aSheet->getHyperlink('B'.$line);
$hyp->setURL($picturl);
$Drawing->setHyperlink($hyp);
$Drawing->setWorksheet($aSheet);
$Drawing->setResizeProportional(true);
$Drawing->setWidthAndHeight(275, 115);
}
}
$filename = 'my.pdf';
//~ $aSheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_PORTRAIT);
$aSheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
$aSheet->getPageSetup()->setPaperSize(PageSetup::PAPERSIZE_A4);
IOFactory::registerWriter('Pdf', Tcpdf::class);
$writer = IOFactory::createWriter($pExcel, 'Pdf');
$writer->save(__DIR__ .'/'.$filename);
//~ header('Content-Type: application/vnd.ms-excel');
//~ header('Content-Disposition: attachment;filename="'.$filename.'"');
//~ header('Cache-Control: max-age=0');
//~ $writer->save('php://output');
exit;