0

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 PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;

// Specify the PDF rendering library and its path
\PhpOffice\PhpWord\Settings::setPdfRenderer(\PhpOffice\PhpWord\Settings::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

0

1 Answer 1

0

Ensure you have the Dompdf library installed via Composer (composer require dompdf/dompdf). Additionally, make sure the paths to your Word template and output files are correct, and that the necessary placeholders exist in your Word template.

Also, ensure that you have the necessary permissions to write and delete files in the specified directories. If you encounter any errors, please let me know, and I'll assist you further

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I have installed dompdf/dompdf using composer using this command composer require dompdf/dompdf. Path means the full path is it? Because the word file is currently in my docs folder in my project
The issue now is my format when displaying is totally different. It converts to pdf and loosing the format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.