01_Simple_download_pdf.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Helper\Sample;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. require_once __DIR__ . '/../../src/Bootstrap.php';
  6. $helper = new Sample();
  7. if ($helper->isCli()) {
  8. $helper->log('This example should only be run from a Web Browser' . PHP_EOL);
  9. return;
  10. }
  11. // Create new Spreadsheet object
  12. $spreadsheet = new Spreadsheet();
  13. // Set document properties
  14. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  15. ->setLastModifiedBy('Maarten Balliauw')
  16. ->setTitle('PDF Test Document')
  17. ->setSubject('PDF Test Document')
  18. ->setDescription('Test document for PDF, generated using PHP classes.')
  19. ->setKeywords('pdf php')
  20. ->setCategory('Test result file');
  21. // Add some data
  22. $spreadsheet->setActiveSheetIndex(0)
  23. ->setCellValue('A1', 'Hello')
  24. ->setCellValue('B2', 'world!')
  25. ->setCellValue('C1', 'Hello')
  26. ->setCellValue('D2', 'world!');
  27. // Miscellaneous glyphs, UTF-8
  28. $spreadsheet->setActiveSheetIndex(0)
  29. ->setCellValue('A4', 'Miscellaneous glyphs')
  30. ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
  31. // Rename worksheet
  32. $spreadsheet->getActiveSheet()->setTitle('Simple');
  33. $spreadsheet->getActiveSheet()->setShowGridLines(false);
  34. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  35. $spreadsheet->setActiveSheetIndex(0);
  36. IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
  37. // Redirect output to a client’s web browser (PDF)
  38. header('Content-Type: application/pdf');
  39. header('Content-Disposition: attachment;filename="01simple.pdf"');
  40. header('Cache-Control: max-age=0');
  41. $writer = IOFactory::createWriter($spreadsheet, 'Pdf');
  42. $writer->save('php://output');
  43. exit;