PhpSpreadsheet xlsx to pdf with mPdf and select cells - php

I want to convert my xlsx in PDF. It works, but I can't zoom my file. I would like select print area . Is it possible ?
Today, I do :
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$path = 'test/FACHAP202212-00001.xlsx';
$spreadsheet = $reader->load($path);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setShowGridlines(false);
$spreadsheet->getActiveSheet()->getPageSetup()
->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT);
$spreadsheet->getActiveSheet()->getPageSetup()
->setPaperSize(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::PAPERSIZE_A4);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
$pdf_path = 'monpdf.pdf';
$writer->save($pdf_path);
I would like to select the print area to have my full-page document in the PDF
Is there a solution?

Related

Out of memory while writing a big file with MemoryDrawing images (PhpSpreasheet)

I'm currently trying to export a list of products (from an API, around 6000 products) to an .xlsx file where I list the product name and its thumbnail.
For text values everything is fine, but I'm trying to render the image directly in the spreadsheet, using PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing.
My issue is after a certain amount of generated image, my process runs out of memory.
Here's my code:
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
foreach ($products as $k => $product) {
$sheet->setCellValueByColumnAndRow(1, $k+1, $product['ref']);
$sheet->getCellByColumnAndRow(1, $k+1)->setDataType(DataType::TYPE_STRING);
$sheet->setCellValueByColumnAndRow(2, $k+1, $product['title']);
if (empty($product['image'])) {
continue;
}
$drawing = new MemoryDrawing();
$sheet->getRowDimension($k+1)->setRowHeight(80);
$gdImage = substr_count($imagePath, '.png') ? imagecreatefrompng($imagePath) : imagecreatefromjpeg($imagePath);
$drawing->setName('Thumbnail');
$drawing->setDescription('Thumbnail');
$drawing->setResizeProportional(true);
$drawing->setImageResource($gdImage);
$drawing->setRenderingFunction(substr_count($imagePath, '.png') ? MemoryDrawing::RENDERING_PNG : MemoryDrawing::RENDERING_JPEG);
$drawing->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT);
$drawing->setHeight(80);
$drawing->setOffsetX(0);
$drawing->setOffsetY(0);
$drawing->setCoordinates('C'.($k+1));
$drawing->setWorksheet($sheet);
unset($drawing);
}
$writer = new Xlsx($spreadsheet);
$writer->save($filePath);
I've also tried to write my file, unset the spreadsheet and reload it every 100 or 200 lines, but I'm losing all the memory images written before.
if ($k % 100 === 0) {
$writer = new Xlsx($spreadsheet);
$writer->save($filePath);
$spreadsheet->disconnectWorksheets();
$spreadsheet->garbageCollect();
unset($spreadsheet);
unset($sheet);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($filePath);
$sheet = $spreadsheet->getActiveSheet();
}
Is there any way to make it work?
try this (persist result in the file system and not in memory)
$drawing->setWorksheet($spreadsheet->getActiveSheet());
$writer = new Xlsx($spreadsheet);
$writer->save('my_file.xlsx');

PhpSpreadSheet: How to save Workbook sheets in individual CSV files

I'm using PhpSpreadSheet and I need to save the sheets contained in a workbook as individual CSV files.
I've tried to use $reader->setLoadAllSheets(); but at the end I always have a single CSV file containing the first sheet of the workbook.
Here's an example of my code:
$excel = 'excelfile.xlsx';
$name = 'newCsvName';
//Read the file
$reader = new Xlsx();
$reader->setReadDataOnly(true);
$reader->setLoadAllSheets();
$spreadsheet = $reader->load($excel);
//Write the CSV file
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->setDelimiter(";");
$csvPath = 'csv_files/' . $dir . '/' . $name .'.csv';
$writer->save($csvPath);
This is how I solved it.
I first load every sheet contained into the excel file and then save a new CSV file with the info. I'm sure there should be a better way, but it works fine.
$excel = 'excelfile.xlsx';
$name = 'newCsvName';
$reader = new Xlsx();
$reader->setReadDataOnly(true);
//Get all sheets in file
$sheets = $reader->listWorksheetNames($excel);
//Loop for each sheet and save an individual file
foreach($sheets as $sheet){
//Load the file
$spreadsheet = $reader->load($excel);
//Write the CSV file
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->setDelimiter(";");
$csvPath = 'csv_files/' . $dir . '/' . $name.'_'.$sheet.'.csv';
$writer->save($csvPath);
}
The answer above is missing a vital line:
$reader->setLoadSheetsOnly([$sheet]);
This makes the reader load the specific sheet which then allows the loop to open the specific sheet and write it to the CSV, otherwise you will find this code creates each sheet name but with the contents from the first sheet of the document.
$excel = 'excelfile.xlsx';
$name = 'newCsvName';
$reader = new Xlsx();
$reader->setReadDataOnly(true);
//Get all sheets in file
$sheets = $reader->listWorksheetNames($excel);
//Loop for each sheet and save an individual file
foreach($sheets as $sheet){
//Load the file
$reader->setLoadSheetsOnly([$sheet]);
$spreadsheet = $reader->load($excel);
//Write the CSV file
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
$writer->setDelimiter(";");
$csvPath = 'csv_files/' . $dir . '/' . $name.'_'.$sheet.'.csv';
$writer->save($csvPath);
}

Automatic generate excel file with phpexcel

I want to automatically generate new excel file if i run this code:
//file.php
$arrtweet[] = array(....);
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("file/ABCDay-1.xlsx");
$objSheet = $objPHPExcel->getActiveSheet();
$objSheet->setTitle('Data ABC Day-1');
$objSheet->getCell('A1')->setValue('User_ID');
$objSheet->getCell('C1')->setValue('Content');
$objSheet->getCell('D1')->setValue('Timestamp');
$objPHPExcel->getActiveSheet()->fromArray($arrtweet, NULL, 'A2');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,"Excel2007");
$objWriter->save("file/ABCDay-1.xlsx");
What I want is:
PHP will automatically generate new excel file if I run that codes for day-2,day-3...,day-N
. Is that possible? Can you help me? thank you so much :')

PHP code can insert image to excel file and open it correctly in MS Excel?

I already have a document of excel and I want to use php to insert an image to that excel.
Is it possible to do that? How to implement it (code)?
Thanks,
$fileType = 'Excel2007';
$fileName = 'test.xlsx';
// Load the workbook
$objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objPHPExcelReader->load($fileName);
// Add an image to the worksheet
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('My Image');
$objDrawing->setDescription('The Image that I am inserting');
$objDrawing->setPath('./images/myImage.png');
$objDrawing->setCoordinates('B2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
// Save the workbook
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$fileType);
$objPHPExcelWriter->save($fileName);

How to split the Excel file that contains multiple sheets?

I have the Excel file that contains a few sheets. How to split this file to get get each sheet as a separate file?
From PHP, you'llneed to have something like phpExcel to open the spreadsheet, and rewrite each tab as a new file.
Using the PHPExcel library:
include 'PHPExcel.php';
$fileType = 'Excel2007';
$inputFileName = 'testExcel.xlsx';
$objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$sheetIndex = 0;
$sheetCount = $objPHPExcel->getSheetCount();
while ($sheetIndex < $sheetCount) {
++$sheetIndex;
$workSheet = $objPHPExcel->getSheet(0);
$newObjPHPExcel = new PHPExcel();
$newObjPHPExcel->removeSheetByIndex(0);
$newObjPHPExcel->addExternalSheet($workSheet);
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($newObjPHPExcel,$fileType);
$outputFileTemp = explode('.',$inputFileName);
$outputFileName = $outputFileTemp[0].$sheetIndex.'.'.$outputFileTemp[1];
$objPHPExcelWriter->save($outputFileName);
}

Categories