0

I've successfully convert Excel to PDF using this code:

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Tcpdf');
$writer->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
header("Content-Disposition: attachment; filename=test.pdf");
$writer->save('php://output');

My problem is, the Excels has too many columns so the generated PDF crop the data.

Is it possible to either:

  1. Fit the Excel into A4 Landscape (make the Excel scale smaller)
  2. Auto resize the PDF to fit the content (it will be bigger than A4 and not necessary standard US Paper Size)
Coisox
  • 1,002
  • 1
  • 10
  • 22
  • https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#pdf – Alex Howansky Feb 03 '22 at 17:15
  • @AlexHowansky, I did found the same documentation but I can't find the example to set autofit into paper size. – Coisox Feb 04 '22 at 15:37
  • @KJ, if I reduce the font size, it is not "auto". The data could be long or small. Setting the font too small will end up with empty space when the data is small – Coisox Feb 04 '22 at 15:40
  • @KJ, Do you mean $sheet->getColumnDimension('A1')->setAutoSize(true)? This is to autosize column width to fit content in Excel. I need the whole columns to autofit into papersize – Coisox Feb 04 '22 at 16:04

1 Answers1

0

I think there's no solution for this. So I just reduce the font size. When my data is small, the paper space will be wasted

Coisox
  • 1,002
  • 1
  • 10
  • 22