Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert html to doc in php [closed]

Tags:

html

php

doc

I need to convert a html file to doc. I am using html2pdf for pdf conversion.

Is there is any same kind of library for html2doc?

(PS must be free/open source)

EDIT

After Mark Eirich comment..

Here are two screenshots. Word document is not proper aligned. Check y-scroll in word document. WORD document, check y scroll..

html file, on browser..

Body tag is:--

<body style="margin-left:350px; margin-right:350px;">

I tried to adjust it but no effect..

EDIT 2

after Mark Eirich second comment i came to know word is taking things in pixel not in %age.. I am having last issue of back ground.. Any help.. please check the two screen shots. The difference is outer box. and thats y html generated doc is looking odd.

Original word file

html generated doc file

like image 897
Mohit Jain Avatar asked Feb 11 '11 23:02

Mohit Jain


People also ask

How can I convert HTML file to PHP file?

In your case, just change the extension of your filename. Example: yourfilename. html to yourfilename. php .

What is DOC file in HTML?

DOC is a word processing file created by Microsoft. This files format turns a plain-text format into a formatted document. It supports almost all the Operating Systems. It can contain large amount of text, data, charts, table, image etc. It can contain rich text format (RTF) and HTML texts also.


2 Answers

The answer IMO Would be no, For the following reasons:

Microsoft Office Documents are extremely complex in the way they are designed, there not just a formatted file with references to objects such as images, there is a type od file system within itself to manage the binary data of these objects.

Let me bring in a quote from our very own Joel:

If you started reading these documents with the hope of spending a weekend writing some spiffy code that imports Word documents into your blog system, or creates Excel-formatted spreadsheets with your personal finance data, the complexity and length of the spec probably cured you of that desire pretty darn quickly. A normal programmer would conclude that Office’s binary file formats:

  • are deliberately obfuscated
  • are the product of a demented Borg mind
  • were created by insanely bad programmers
  • and are impossible to read or create correctly.

You’d be wrong on all four counts....

Read further down for a possible solution:

If you really want to generate fancy formatted Word documents, your best bet is to create an RTF document. Everything that Word can do can be expressed in RTF, but it’s a text format, not binary, so you can change things in the RTF document and it’ll still work. You can create a nicely formatted document with placeholders in Word, save as RTF, and then using simple text substitution, replace the placeholders on the fly. Now you have an RTF document that every version of Word will open happily.

@source: http://www.joelonsoftware.com/items/2008/02/19.html

Some links that may interest you along your journey:

  • Resources:
    • XHTML2RTF: An HTML to RTF conversion tool based on XSL
    • Word (.doc) Binary File Format
    • HTML+CSS to RTF (in PHP)?
  • Solutions
    • Live DOCX In PHP
    • Zend_Service_LiveDocx

Although, Try opening a word file with winrar ;), Maybe creating an archive with certain headers and then changing the extenstion will suffice, Never Tried

like image 138
RobertPitt Avatar answered Oct 21 '22 10:10

RobertPitt


in order to convert to Microsoft Word you need an COM enabled server (running Windows and Office on it). If you have such a server

$word = new COM("word.application") or die ("couldnt create an instance of word"); 

should work!. Read http://php.net/manual/en/book.com.php for details.

Otherwise your best shot at html2doc is html2rtf which is achieved with a library such as http://paggard.com/projects/rtf.generator/ or http://sourceforge.net/projects/phprtf/.

Then after you create the RTF you serve it to the browser with a doc header

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

If the user has word then it will be open to handle the file.

Also saving an rtf as doc is ok and word will open in layout view without any complaints. You can also serve HTML with the above header but the problem is that Word will open in web view and that is bad :)

like image 21
Andrei Draganescu Avatar answered Oct 21 '22 09:10

Andrei Draganescu