I attempted to convert my Word document to PDF, but the formatting was lost and did not match the original. Specifically, I was converting a certificate from a Word file to PDF, resulting in incorrect formatting. Despite searching for a solution, I couldn’t find one, and I’m not very familiar with PHP libraries like PHPWord. Any assistance would be greatly appreciated. I have included my code as well
<?php
require_once 'vendor/autoload.php'; // Include PHPWord autoload.php
use PhpOfficePhpWordTemplateProcessor;
use PhpOfficePhpWordSettings;
use PhpOfficePhpWordIOFactory;
// Specify the PDF rendering library and its path
PhpOfficePhpWordSettings::setPdfRenderer(PhpOfficePhpWordSettings::PDF_RENDERER_DOMPDF, 'vendor/dompdf/dompdf');
// Load the template document
$templateFile = 'docs/certification-of-appreciation.docx'; // Path to your Word template
$templateProcessor = new TemplateProcessor($templateFile);
// Replace placeholders with dynamic content
$templateProcessor->setValue('USERNAME', $_SESSION['worker_name']); // Replace 'USERNAME' with your placeholder for the user's name
// You might need to replace 'SUPERVISOR_SIGNATURE' with the appropriate placeholder for the supervisor's signature
// Save the processed document
$processedFilePath = 'docs/certification-of-appreciation_processed.docx'; // Path to save the processed Word document
$templateProcessor->saveAs($processedFilePath);
// Load the processed document
$processedPhpWord = IOFactory::load($processedFilePath);
// Convert Word to PDF
$pdfWriter = IOFactory::createWriter($processedPhpWord, 'PDF');
$pdfFilePath = 'docs/Certificate.pdf'; // Path to save the PDF file
$pdfWriter->save($pdfFilePath);
// Send the PDF to the browser
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="Certificate.pdf"');
readfile($pdfFilePath);
// Clean up: delete temporary files if needed
unlink($processedFilePath);
unlink($pdfFilePath);
?>
I want a output where it successfully converts my word file to pdf without loosing it format