how to create docx file using php jquery

399 Views Asked by At

I m so much confused about how to create docx files while I submitting the data from my given form and also I need to save that data into database.

<html>
 <head>
  <title>Create Docx File</title> 
  <script>
   $('#myButton').click(function(){   var s = $('#myHTMLDiv').html($('#htmlstring').val(s);   );
  </script>
 </head>
 <body>
  <h3>User Details</h3>
  <form method="post" action="gendocx.php"> 
  Enter Name:<input type="text" name="htmlstring" id="htmlstring" value="">
     <input type="submit"id="MyButton" value="Generate Docx">
  </form>
 </body>
</html>

gendocx.php

<?php 
    require_once('vendor/autoload.php');
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection(); 
    \PhpOffice\PhpWord\Shared\Html::addHtml($section, $_POST['htmlstring']);
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment;filename="myfirstdocfile.docx"');
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('php://output'); 
?> 
0

There are 0 best solutions below