Convert HTML to Word /Excel/Powerpoint with PHP

Convert html to word /excel / powerPoint with PHP

Try the following PHP classes:

  • PhpSpreadsheet
  • PHPWord
  • PHPPresentation

I only used PHPExcel so far but it worked great and is easy to learn. Since all classes are from the same company I assume that they should fit your needs as well.

how to convert html file to word file in php

There is an API. Please go through it.

https://github.com/PHPOffice/PHPWord

You can see in above link's page, they have converted something like below:

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

Convert HTML to DOC in PHP

You cannot change a file format simply by changing the file extension. Do you think you can convert a PDF to a movie by changing the file name from .pdf to .mp4? I hope not, because it doesn't make any sense.

What's happening is that you're telling the browser to save data of an HTML file with a .doc extension. When you double click that file to open it, Word opens (because it's associated with .doc extensions). Word is stupid misleading forgiving enough to recognise that the file does not actually contain Word DOC data, but HTML, and it converts it for you on the fly without telling you.

When you then save this file, it creates an actual DOC/DOCX file for it; but apparently that doesn't happen cleanly and the container is breaking apart.

What you're seeing it a misbehaviour in Microsoft Word (on several levels).

What you should be doing to begin with is create an actual Word document, e.g. using https://github.com/PHPOffice/PHPWord.

Convert Word doc, docx and Excel xls, xlsx to PDF with PHP

I found a solution to my issue and after a request, will post it here to help others. Apologies if I missed any details, it's been a while since I worked on this solution.

The first thing that is required is to install Openoffice.org on the server. I requested my hosting provider to install the open office RPM on my VPS. This can be done through WHM directly.

Now that the server has the capability to handle MS Office files you are able to convert the files by executing command line instructions via PHP. To handle this, I found PyODConverter: https://github.com/mirkonasato/pyodconverter

I created a directory on the server and placed the PyODConverter python file within it. I also created a plain text file above the web root (I named it "adocpdf"), with the following command line instructions in it:

directory=$1
filename=$2
extension=$3
SERVICE='soffice'
if [ "`ps ax|grep -v grep|grep -c $SERVICE`" -lt 1 ]; then
unset DISPLAY
/usr/bin/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
sleep 5s
fi
python /home/website/python/DocumentConverter.py /home/website/$directory$filename$extension /home/website/$directory$filename.pdf

This checks that the openoffice.org libraries are running and then calls the PyODConverter script to process the file and output it as a PDF. The 3 variables on the first three lines are provided when the script is executed from with a PHP file. The delay ("sleep 5s") is used to ensure that openoffice.org has enough to time to initiate if required. I have used this for months now and the 5s gap seems to give enough breathing room.

The script will create a PDF version of the document in the same directory as the original.

Finally, initiating the conversion of a Word / Excel file from within PHP (I have it within a function that checks if the file we are dealing with is a word / excel document)...

//use openoffice.org
$output = array();
$return_var = 0;
exec("/opt/adocpdf {$directory} {$filename} {$extension}", $output, $return_var);

This PHP function is called once the Word / Excel file has been uploaded to the server. The 3 variables in the exec() call relate directly to the 3 at the start of the plain text script above. Note that the $directory variable requires no leading forward slash if the file for conversion is within the web root.

OK, that's it! Hopefully this will be useful to someone and save them the difficulties and learning curve I faced.



Related Topics



Leave a reply



Submit