From fb472f29c57508e76061058c7a925e40f3ba29dc Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Fri, 10 Jan 2025 12:15:16 +0600 Subject: [PATCH] phpdocx --- .../Controllers/Staff/ReportController.php | 5 ++- app/Services/DocumentGenerator/Word.php | 38 +++++++------------ composer.json | 4 +- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/Staff/ReportController.php b/app/Http/Controllers/Staff/ReportController.php index ed68d93..db7f8b0 100644 --- a/app/Http/Controllers/Staff/ReportController.php +++ b/app/Http/Controllers/Staff/ReportController.php @@ -9,6 +9,7 @@ use App\Models\Study; use App\Models\StudyReport; use App\Services\DocumentGenerator\Word; +use App\Services\ReportStorage; class ReportController extends HashidControllerBase { @@ -75,6 +76,8 @@ public function download(string $uuid) // $study = Study::with(['reports.radiologist', 'readingPhysician'])->where('accession_number', $uuid)->firstOrFail(); $report = StudyReport::where('accession_number', $uuid)->firstOrFail(); - return Word::make($report->study, $report); + $path = Word::make($report->study, $report); + + return response()->download(ReportStorage::abspath($path)); } } diff --git a/app/Services/DocumentGenerator/Word.php b/app/Services/DocumentGenerator/Word.php index ac0936a..1b14dec 100644 --- a/app/Services/DocumentGenerator/Word.php +++ b/app/Services/DocumentGenerator/Word.php @@ -4,36 +4,24 @@ use App\Models\Study; use App\Models\StudyReport; -use PhpOffice\PhpWord\IOFactory; -use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Shared\Html; +use App\Services\ReportStorage; +use CreateDocx; -class Word +final readonly class Word { - public static function make(Study $study, StudyReport $report) + public static function make(Study $study, StudyReport $report): string { + $filename = $report->accession_number . '.docx'; + $filepath = ReportStorage::customFilepath($study, $filename); + if (ReportStorage::exists($study, $filename)) { + return $filepath; + } + $html = $report->getContent(); - - $phpWord = new PhpWord; - $section = $phpWord->addSection(); - - // Add HTML content to the section - Html::addHtml($section, $html, false, false); - - // Save the document as a DOCX file - // $filename = 'report.docx'; - $writer = IOFactory::createWriter($phpWord, 'Word2007'); - ob_start(); - $writer->save('php://output'); - $content = ob_get_clean(); - - return $content; - - $content = view('staff.reports.word', compact('study', 'report'))->render(); - $filepath = ReportStorage::reportFilepath($study, '.docx'); - Storage::put($filepath, $content); + $docx = new CreateDocx; + $docx->embedHTML($html); + $docx->createDocx(ReportStorage::abspath($filepath)); return $filepath; - } } diff --git a/composer.json b/composer.json index 1e42b0f..ad9bf81 100644 --- a/composer.json +++ b/composer.json @@ -55,7 +55,8 @@ "Tests\\": "tests/" }, "files": [ - "app/helpers.php" + "app/helpers.php", + "src/phpdocx/src/CreateDocx.php" ] }, "scripts": { @@ -90,6 +91,7 @@ "composer clear-all", "composer cache-all" ], + "clr": "composer clear-all", "fix": "php-cs-fixer fix", "dev": [ "Composer\\Config::disableProcessTimeout",