I need to fill the existing Excel sheet regularly and export it to PDF. But I can’t do it so that the content fits the page.
$spreadsheet = IOFactory::load(Storage::path('Time&Attendance Template.xltx'));
$sheet = $spreadsheet->getActiveSheet();
$pdfWriter = new Tcpdf($spreadsheet);
$sheet->getCell('A1')->setValue(Str::random());
$pdfWriter->setPaperSize(PageSetup::PAPERSIZE_A4);
$pdfWriter->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
$pdfWriter->save($file = Storage::path('test.pdf'));
Result:
The same file in Excel online without any scaling settings fits on one PDF page:
PS. I tried mpdf and dompdf generators. They get lines of fonts so wrong, not close enough to the original for me.
Also I don’t get why vertical align doesn’t work. TCPDF seems to be ignoring vertical align of the cell.
2
Answers
Manually adjust cell and font sizes:
$spreadsheet->getActiveSheet()->getColumnDimension(‘A’)->setWidth(10);
Wrap text in cells:
$spreadsheet->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
This should solve your problem
You need to work on three things to fit the content into a single page:
You need to include this line which allows you to export the spreadsheet to a PDF using the Mpdf engine
Here is the updated code: