save php spreadsheet template as PDF using Mpdf - php

i need some help with phpspreadsheet, i'm trying to load an excel spreadsheet, add something to it and then save it as a pdf, the problem is that when i load a template , saving as pdf doesn't work
ps : when i create a new spreadsheet it works fine.
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load('docs/MODEL_FACTURE_DOM_CRE.xlsx');
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('B13', 'test')
->setCellValue('F13', 'test');
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$rendererName = Settings::PDF_RENDERER_MPDF;
Settings::setPdfRendererName($rendererName);
$writer = IOFactory::createWriter($spreadsheet, 'Pdf');
$filename = 'FACTURE.pdf';
$writer->save($filename);
Any suggestions ?

Try the older version. On MPDF version v5.7.4a, your code is work.
In more recent versions there are incompatible changes.

Related

PhpSpreadsheet xlsx to PDF with mPDF -> I can't delete grid lines and show table borders

I am trying to generate a PDF invoice by XLSX template with phpspreadsheet.
Use php7.3 version, "mpdf/mpdf": "^8.0" and
"phpoffice/phpspreadsheet": "^1.10"
$path = 'ejemplo2.xlsx';
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($path);
$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($path);
$objPHPExcel->getActiveSheet()->getStyle('A:G')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setRGB('#ffffff');
$pdfPath = 'ejemplo2.pdf';
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Mpdf');
$writer->save($pdfPath);
The original xlsx and generated pdf look like: this
I tried to use $spreadsheet->getActiveSheet()->setShowGridLines(false); but so I can't show the table borders.
Does anybody know what I am doing wrong?
it probably has something to do with your version of php, seems like it is outdated, try to update it to the latest version. I had similar problem and updating my XAMPP fixed the problem.
I am triying with Dompdf and run OK.
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Dompdf');

Copy charts from (.xls or .xlsx) using PHP

I am working on a template excel file for some projects and my goal is to populate it with PHP. I create an excel document with two sheets, one is a table with all of the information that I am going to plot, and the second sheet is just a graph that is based on the table from the previous sheet.
Whenever I use (https://github.com/PHPOffice/PhpSpreadsheet) PHPSpreadsheet (since PHPExcel is deprecated) I am able to read/write/copy/etc the first sheet, but whenever I try to save the sheet without touching the graph sheet at all, nothing happens. I save the file successfully but when I try to open it I get an error stating that Excel found unreadable content in the file and to repair it. I repair it and the second sheet with the graph is empty.
https://github.com/PHPOffice/PhpSpreadsheet/issues/382
This is the closest thing I found to my issue, and I did add the setIncludeChart functions, but nothing seems to work.
I also feel that I have exhausted online searches and am now hoping that there is an alternative to PHPSpreadsheet that will allow me to use a chart in a template (supplied) excel file.
$xls = 'KPI_ReadinessTemplate.xls';
$xlsxTarget = 'NEWX_KPI_ReadinessTemplate.xlsx';
$xlsTarget = 'NEW_KPI_ReadinessTemplate.xls';
$inputFileType = 'Xlsx';
$inputFileName = $xlsx;
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load($inputFileName);
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('A1')->setValue('Help me');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$fileData = $writer->save($xlsxTarget);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setIncludeCharts(TRUE);
$workbook = $reader->load($xls);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($workbook);
$writer->setIncludeCharts(TRUE);
$writer->save($xlsTarget);
I am expecting the chart to be copied, in the code from above there is no modification of the file, it should be an exact one-to-one copy of the supplied file but it isn't working at all as it seems the graph gets corrupted and I have to repair the excel the next time I open it.
EDIT: I also installed the archived PHPExcel and tried it using Graph disapear read and write excel file using PHPExcel and I still had the same issue.

How to display an Excel with PhpSpreadsheet when loading the information with PHP

I am currently using the PHPSpreadsheet library to take values from PHP to Excel, what it does now is to take the values to an excel and store them in the address that I specify. but what I'm looking for is that when I upload information, show me the excel to open. When I used the PHPExcel if I could, and it was done with: $ objWriter-> save ('php: // output') then the excel was displayed to open. My code with PHPSpreadsheet is the following:
<?php
require_once("vendor/autoload.php");
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$a="we";
$a1="are";
$a2="the";
$a3="world";
$sheet->setCellValue('A2', $a);
$sheet->setCellValue('c2', $a1);
$sheet->setCellValue('E2', $a2);
$sheet->setCellValue('G2', $a3);
$writer = new Xlsx($spreadsheet);
$writer->save('C:\xampp\example.xlsx');
?>
Could it be done?
You can't show a Excel file without an application for open it, I think you could use another web application for display the file on web browser.

Load a Excel file with images using PHPExcel

I'm currently working on a project that needs to display excel files (xls, xlsx, csv) on the browser. So far, I have tried and used the PHPExcel library and was able to display the excel file (code below)
$opendoc = $userDoc;
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load($opendoc);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->writeAllSheets();
$objw->save('php://output');
The problem I'm encountering is that this code does not support the displaying images (charts , graph , etc) inside an excel file. Any ideas? Thanks in advance!
Errr..... yes it does. Have you read the documentation or looked at the examples? Images are supported directly, and (unless you tell PHPExcel to load data only) should always be loaded.
For charts and graphs, you specifically have to tell PHPExcel to load them when reading a file, and tell PHPExcel to save them when Writing. (Example)
$opendoc = $userDoc;
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($opendoc);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->setIncludeCharts(TRUE);
$objw->writeAllSheets();
$objw->save('php://output');

Excel 97-2003 file incompatible after PHPExcel reads and writes

I have an excel file that has been created and saved as an Excel 97-2003 file. I am using the following code to read and then write using PHPExcel.
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load(storage_path() . "/exports/file.xls");
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Test Message');
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter-> save(storage_path() . "/exports/file.xls");
I'm using SamApp ExcelSMS Android app that will then read the file. The only problem is that the file is now incompatible when overwritten with new content. Also, the file size decreases from 18KB to 5KB.
Any help on this is greatly appreciated.

Categories