This code fetches a service request from the database, retrieves the request data and service information. It uses an HTML template and replaces placeholders with actual data. Then, it generates a Word document using PHPWord library and downloads it to the user's device.
$serviceRequest = ServiceRequest::findOrFail($id);
$data = $serviceRequest->request_data;
$service = $serviceRequest->service;
$templateContents = $service->request_form_template;
$html = $templateContents;
// $dom = new \DOMDocument();
// $dom->loadHTML($html);
foreach ($data as $field => $value) {
$html = str_replace('{{' . $field . '}}', $value, $html);
}
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
$tempFilePath = tempnam(sys_get_temp_dir(), 'phpword_');
$phpWord->save($tempFilePath );
return response()->download($tempFilePath, 'ServiceRequest.docx')->deleteFileAfterSend(true);
When a user uploads a template using a WYSIWYG editor, there may be cases where the saved template is invalid and throws an error. To ensure a smooth user experience and avoid any problems related to missing tags, i need filter out these missing tags and proceed with the download. In simpler terms, i need to identify any missing tags in the template and fix them automatically before allowing the user to download the template. what i have tried is using
$dom = new \DOMDocument();
$dom->loadHTML($html);
But it did not work.