Skip to content
Advertisement

how to convert .docx / .doc file to .pdf using php only

I want to convert ms-word file(.doc,.docx) to .pdf or .html file without losing style and image on word file.

                   $filenamearray = explode('.',$filename );
                        $doc = new Docx_reader();
                        $doc->setFile($item);

                        if(!$doc->get_errors()) {
                            $html = $doc->to_html();
                            $plain_text = $doc->to_plain_text();
                            $myfile = file_put_contents("$filenamearray[0].html", $html.PHP_EOL , FILE_APPEND | LOCK_EX);

                        } else {
                            echo implode(', ',$doc->get_errors());
                        }

Above solution i tried but giving me only html without style and image.i want .html or .pdf same as .doc .docx file.

Advertisement

Answer

This is not an easy task and your rate of success will largely depend on the complexity of your word document. If only basic style elements are used (bold, italic, underline, color) you could use the HTML add on to FPDF. Any more complex elements would require a translation function to FPDF.

I assume you need this for template purposes, may I suggest directly programming your page layout in FPDF. You can create custom tags for layout which can be used in a WYSIWYG editor.

You will lose some of the flexibility compaired to a Word document, but that loss does not outweight the ease of setting up standard FPDF. But again, your best way to go will depend on the functionality you need and your end-goal. I suggest taking a look at the possibilities with FPDF. It’s a great script and can make PDF’s lightning fast!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement