Create Word Document from PHP Documentation - php

To document my code I thought it would be best practice to use phpDoc syntax, because there are several parsers out there and some IDEs create IntelliSense out of it.
Now I need to put the documentation (API) into a word file, but I don't know which parser is able to output .doc or similar.
I tried DoxyGen, which outputs .rtf and phpDocumentor2, which can only export to .html and .xml (?).
Is there a way to generate a .doc(x) file from phpDoc? Or a simple way to get a document which can be imported to word?
I would appreciate if I don't have to change the phpDoc syntax, because my documentation is very long.
Edit: The prefered parser would be phpDocumentor2, because it supports PHP 5.3 functionalities and it's faster than DoxyGen, but phpDocumentor2 has less features than phpDocumentor, which is no longer maintained, related to output formats.
Edit: I tried to copy content from the .rtf file into the .docx file, but when I select 'Use Destination Styles', both Word instances suspend and do not respond.

Presumably you want one large Word doc that contains all the info for your project in the one doc/file... therefore just opening the phpDoc2 HTML output into Word in order to convert it to docx will not meet your need, since that would be one docx per phpdoc2 HTML page.
You might try altering your searches to be for a tool that can spider a given HTML page, recursively pick up all its target page hierarchy, and convert it all into a single docx. You might have more luck finding a tool that does this but produces a PDF... then you could just use Word to convert the PDF into docx.

Related

Merge two RTF documents in PHP

I've searched online in multiple places, but I can't seem to find a proper solution. I have two RTF documents that I want to merge together as follows. I have a document and a cover letter, so I want the document followed by copies of it with a cover letter before each copy, as follows:
Document
Cover Letter
Document
Cover Letter
Document
With RTF files they have markup at the beginning and end, so I can write a regex to remove the markup, which technically works fine, but the formatting can be changed in the cover letter. Also, the other problem is that the document can change - there is a list of about 10 or 15 documents that I can choose from, each of which has a different markup at the beginning of the document, so the solution that I found at Concatenate RTF files in PHP (REGEX) can't help in all situations.
Sadly, since I am on PHP 5.3 I can't use the solution mentioned at Merge multiple doc or rtf files into a single doc or rtf file by using php script because it relies on PHP 5.4 or PHP 7

Parsing/edition docx file with PHP

I've been asked to write a php script that should read/parse a docx file and do some operations such as duplicate a specific paragraph/table and fill-in some variables (#myvar or $myvar) with values.
What do you guys recommand, use the word/document.xml file directly or convert the whole document to an HTML file and then parse it using DOM(I don't like this solution :( )?
the structure of the docx to parse is not defined yet, it's my job to do that ! And it has to be as general as possible.
To have a clear idea about what I'm doing, the docx file is a CV model that I have to fill-in with data from DB.
P.S: I don't know how to efficiently parse/modify the XML file using Xquery since the only solution I have is to use variables (plain text with $ or #..) inside that docx
thanks for your help :)
There are 2 major PHP libraries able to create Word documents. Here's a description of features from both that might help you solve your problem:
PHPWord (opensource) - allows to load template documents and replace values... take a look at this example in library's source code, maybe you can define a CV template and use this to work a solution out;
PHPDocX (free with basic features, paid for more advanced features) - allows templates and search and replace of content in documents (probably only in paid versions though).
This is a old question, but I thought I give some pointers as I have been struggling with this for some time and have ended up writing my own package at github: wrklst/docxmustache.
Here are some solutions I know of:
Free solutions:
https://github.com/PHPOffice/PHPWord (as mentioned above, cumbersome and not very capable)
http://www.tinybutstrong.com/opentbs.php (works but is highly cumbersome, also introduces a lot of security issues if you plan to allow user supplied templates)
Partially Free and Paid:
https://www.phpdocx.com
http://www.docxpresso.com (looks like one of the more complete solutions to me, at 199 eur for a server license its not too expensive either)
https://modules.docxtemplater.com
I worked with opentbs quite a bit but I am not happy with it and I am currently trying to evaluate to write my own solution that is more geared to my specific needs. Generally you need:
- A zip calss to unzip/rezip the docx file
- A template engine to replace values, I am using mustache (https://github.com/bobthecow/mustache.php)
- If you are planning to replace images as well you need to more advanced file, reference and xml handling. Php's SimpleXMLElement should be sufficient to handle all the xml manipulation.
Off course you can always convert the docx into a more accessible format, but that will greatly mess with any styling. If thats not an issue I recommend to use libreoffice to convert your docx into any format that libreoffice supports. on linux based servers you can easily access it via command line, here an example with symfony for command execution:
$command = "soffice --headless --convert-to html ".$inputfile.' --outdir '.$outputfile.'/');
$process = new \Symfony\Component\Process\Process($command);
$process->start();
while ($process->isRunning()) {}
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new \Symfony\Component\Process\Exception\ProcessFailedException($process);
}
Check out my package wrklst/docxmustache if you want to see this in context.
good luck!

Manipulating Microsoft Word Office 2007 .docx document from PHP

I need an option from within PHP to Manipulate .docx (Microsoft Office 2007) document.
I need to:
Read the internal text
Convert to .html
To view them inside a browser.
To replace text.
I know I can use Word Automation, creating a COM object of Microsoft Word, but it's too slow, unstable and I have to have it installed on the server.
Is there any library or code that can do it from PHP?
There is PHPWord for that by the authors of PHPExcel.
Docx is just a ZIP file containing multiple XML files and embedded media files like images. Because of this, you can read and edit the document with ease. Just unzip it, open word/document.xml, do reading & writing, and repack the files.
Convet to HTML may be difficult. But you'll find a thumbnail of the first page in docProps/thumbnail.jpeg.
Note that you'll have to familiarize yourself with the XML structure to do any complex edits. There's a summary XML docProps/app.xml which has some metadata for the file so don't forget to update it. Read more from Wikipedia: http://en.wikipedia.org/wiki/Office_Open_XML
You may have a look at PHPDocX I believe it does all you are asking for.
You may replace variables in a template or just plain text from a prexisting Word document.
It offers quite a few conversion options.
You can also extract the text.
You can work with the internal format directly.
DOCX is just a zip file, and inside that there's word/document.xml containing the actual document.
It's quite trivial to unzip the file, read document.xml, str_replace() what you're looking for, save it and re-zip the directory, and it makes for a lightweight, quick and easy mail merge capability for word documents. This also works for other office formats.
Here's the official docs on the internal structure for more information.
There is also a PHP class for merging new content into an existing .docx file. It is available here: http://www.tinybutstrong.com/ . The documentation is pretty good as well as having many examples and it is all free and open source. It does require familiarity with the .docx concepts, though.

How can i convert a php page into .doc file with php

Recently i worked in a project. On this project I need convert page into a Microsoft word document (.doc file) and offer the document for download, all using PHP. But I can't solve this problem.
Please help me. Thank You very much, Arif
This is not easy to solve.
First off, if you want to write real word documents, you will have to do on Windows. You can use COM to talk to Word and this is how you manage to get good results. I've tried all the unix/linux based solutions and the results were not so great.
Otherwise, I'd suggest you write RTF -- which is just as good. And in the end, you can call the .rtf-file, .doc and no one will notice it. RTF has a couple limitations (formatting), but on the flipside -- it's all ASCII and the RTF standard is pretty comprehensive and well documented.
There's a class which does it pretty nicely -- phpLiveDocx (this is a great introduction). And this class also claims to write PDF and DOC -- but I haven't tried those yet. I use another solution for PDF.
I would recommend using the RTF format instead of the .doc - it's much simpler to write to, and all text editors understand it. Similar recommendation for .csv when you want to output an Excel file.
Perhaps not the answer you seek, but still interesting to note, there is a open source word processor out there called abiword that has a CLI (Command Line Interface). You can use it to easily convert between document formats. I know that at least one website uses it to convert text files into various formats.
It is actively getting developed and could easily be used as a 3de party black box solution to converting documents server side.
Here is a blog from one of the developers on how to integrate it with PHP
Server-Side AbiWord
abiword home page

Extract hyperlinks from .doc

Is there any way to extract hyperlinks from .doc. I got bunch of hyperlinks in doc that I need to import in my database.
I have tried converting doc to HTML, but hyperlinks are not transferred.
Regardz,
Mladen
We had a similar issue and ended up using a third party component called Aspose.Words.
You can find it here: http://www.aspose.com
It's available for .NET and Java.
You could try importing the file into OpenOffice and see whether hyperlinks are transferred. OpenDocument is just a ZIP file with XML inside, very easy to parse once you've got the hang of it.
I have done the following thing. I have opened the .doc file with officeXP, then published it as a blog and after that I have saved that blog in the form of filtered web page. That gives you nice HTML which you can parse with ease.
I realise this is some months after your initial question, however, You can also extract hyperlinks in a .doc file through through Word Automation. There are hyperlink objects in the API that you can easily extract.

Categories