Files
2026-04-28 21:15:09 +03:00

79 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use Mpdf\Mpdf;
// Excel dosyasını yükle
try {
$filePath = 'storage/documents/test/test.xlsx';
$spreadsheet = IOFactory::load($filePath);
// Değiştirilecek joker alanlar ve değerleri
$replacements = [
'{test1}' => 'Deger1',
'{test2}' => 'Deger2',
'{test3}' => 'Deger3',
// Diğer joker alanlar ve değerler buraya eklenebilir
];
// Aktif çalışma sayfasını al
$sheet = $spreadsheet->getActiveSheet();
// Tüm hücreleri döngüyle gezerek joker alanları değiştir
foreach ($sheet->getRowIterator() as $row) {
foreach ($row->getCellIterator() as $cell) {
$cellValue = $cell->getValue();
if (is_string($cellValue)) {
foreach ($replacements as $search => $replace) {
if (strpos($cellValue, $search) !== false) {
$cellValue = str_replace($search, $replace, $cellValue);
$cell->setValue($cellValue);
}
}
}
}
}
// Değişiklikleri kaydetmek için yeni bir dosya oluştur
$outputFilePath = 'storage/documents/test/output/output.xlsx';
$htmlFilePath = 'storage/documents/test/output/output.html';
$pdfFilePath = 'storage/documents/test/output/output.pdf';
// HTML yazıcı oluştur
$htmlWriter = new Html($spreadsheet);
$htmlWriter->save($htmlFilePath);
$htmlContent = file_get_contents($htmlFilePath);
html_to_pdf($htmlFilePath, $pdfFilePath);
// MPDF ile PDF oluştur
/*
$mpdf = new Mpdf();
$mpdf->WriteHTML($htmlContent);
$mpdf->Output($pdfFilePath, \Mpdf\Output\Destination::FILE);
// Geçici HTML dosyasını sil
unlink($htmlFilePath);
*/
echo "Dosya başarıyla PDF olarak kaydedildi: $pdfFilePath";
/*
$writer = new Xlsx($spreadsheet);
$writer->save($outputFilePath);
echo "Dosya başarıyla kaydedildi: $outputFilePath";
*/
} catch (\Throwable $th) {
echo 'Hata: ' . $th->getMessage();
}
?>