phperror-handlingphpword

I'm having a php error can someone provide some solutions


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:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php:148 Stack trace: #0 C:\xampp\htdocs\php\index1.php(10): PhpOffice\PhpWord\PhpWord->__call('createtemplate', Array) #1 {main} thrown in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.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 \PhpOffice\PhpWord\PhpWord();

  // 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....


Solution

  • 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);