I am having an error in my PHP code and I need help. If anyone knows the solution to this issue, please help me. The error message I am seeing is
[Fatal error: Uncaught BadMethodCallException: Method createtemplate is not defined. in C:xampphtdocsphpvendorphpofficephpwordsrcPhpWordPhpWord.php:148 Stack trace: #0 C:xampphtdocsphpindex1.php(10): PhpOfficePhpWordPhpWord->__call(‘createtemplate’, Array) #1 {main} thrown in C:xampphtdocsphpvendorphpofficephpwordsrcPhpWordPhpWord.php on line 148].
This is my code
<!-- HTML form for selecting the Word document to convert --> <form method="post" enctype="multipart/form-data"> <label for="word-file">Select Word document:</label> <input type="file" name="word-file" id="word-file"> <input type="submit" value="Convert"> </form> <?php // Check if the form has been submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Get the uploaded Word document $wordFile = $_FILES['word-file']['tmp_name']; // Include the PHPWord library require_once 'vendor/autoload.php'; // Create a new PHPWord object $PHPWord = new PhpOfficePhpWordPhpWord(); // Load the Word document $document = $PHPWord->loadTemplate($wordFile); // Save the document as a PDF $document->saveAs('converted.pdf'); // Show a message to the user echo '<p>The Word document has been converted to PDF.</p>'; } ?>
have tried many solutions but nothing works….
Advertisement
Answer
I think you can use save method not to loadTemplate… please replace your code…
From:
$document = $PHPWord->loadTemplate($wordFile); $document->saveAs('converted.pdf');
To:
$document = $PHPWord->save($wordFile);