Php xml to odt
Post by noscollections » Sat Jan 19, 2008 3:14 pm
I have a PHP soft and I would like have an export for openoffice (ODT)
I have search the web for do this and I found :
1. http://php.developpez.com/faq/?page=ooo
work find but only for text.
I woulf like page break, and image (link)
2. http://phpdocwriter.sourceforge.net/
but it create openoffice 1.1 file (.sxw) and openoofice 2.x open only file with text with repare. And don’t work with php5
3. tinybut strong, not tested but it look not easy.
Is anyone have do something like this ?
create odt, with text, image link, page break and why not more ?
Thanks for your experience help
french my english is not perfect.
TerryE Volunteer Posts: 1402 Joined: Sat Oct 06, 2007 10:13 pm Location: UK Contact:
Re: Create ODT with PHP
Post by TerryE » Sat Jan 19, 2008 3:58 pm
First, remember that ODF is primarily a ZIP compressed container containing a bunch of XML encoded files so any XLST transformation engine can be coopted to do most of this. For example: ODF-XSLT Document Generator and the engine within OpenXML/ODF Translator Add-ins for Office.
Also its pretty trivial rolling your own ODT/ODS if what you are really doing is generating proforma test reports as you can create a baseline with Writer or Calc then use php effectively as a macro generator create you specific contents.xml.
Re: Create ODT with PHP
Post by noscollections » Wed Jan 23, 2008 10:37 am
I have create an odt file with OOO.
The file contained only pages break text, page break, text.
My goal is to find xml code of texte and page break.
I have find the text code, but nothing particular between text code that could be page break code. I edit contents.xml file.
I have found the image link code. it’s a long code, I try to make the same but OOO show nothing.
The script here : http://php.developpez.com/faq/?page=ooo is good, because it create a real odt file. it zip the contents.xml and manifest.xml files and write odt on the server
I can’t find doc that explain how odt file is made.
TerryE Volunteer Posts: 1402 Joined: Sat Oct 06, 2007 10:13 pm Location: UK Contact:
Re: Create ODT with PHP
Post by TerryE » Wed Jan 23, 2008 3:18 pm
Of course for this to work you will need php5 together with the XML and ZIP modules installed on your server. I’ve got these on my test rig. At the moment our user.services.OpenOffice.org doesn’t have these modules installed for example. My hobby shared service is still on php4!
However I am currently writing a tutorial on Integrating OOo apps with Server application and I will cover this subject here.
Re: Create ODT with PHP
Post by noscollections » Fri Jan 25, 2008 3:18 pm
Yes, That work.
I have do like you, but I not see a new style was create.
I have create a style and Insert the xml code and, that work perfect.
For my other probleme (image link), I will look the SDK doc, by a search by google.
I will post message soon, to ask you, if I found, I will told you.
Re: Create ODT with PHP
Post by noscollections » Sat Jan 26, 2008 11:20 am
I have do a test, I have create a simple odt with image link.
here is the that I think, is for image link :
Part code that I have in my odt that I have in content.xml creat by php :
—
During I ‘m wrting this post I see just that style «standard» that is show here :
is not in my style.
It is in my OOO geneted file in the style.xml but not in my own ODT.
I will Add it for test. Except this I not see what is the probleme.
However I am currently writing a tutorial on Integrating OOo apps with Server application and I will cover this subject here.
This is a good news, I will be to read it when it will be ready. Don’t forget to inform us when it will be on the web.
Re: Create ODT with PHP
Post by noscollections » Sat Jan 26, 2008 11:43 am
image is not show, I don’t know why, I will search again. I must forget something but where . that is the question !
I post my content.xml code ( I have add line break in the code for make it read easier)
mis en forme mis en forme 2 Hello world! text must be after HERE "/> Hello world! text must be BEFORE HERE mis en forme 3
TerryE Volunteer Posts: 1402 Joined: Sat Oct 06, 2007 10:13 pm Location: UK Contact:
Re: Create ODT with PHP
Post by TerryE » Sat Jan 26, 2008 12:43 pm
- xmlns:dc http://purl.org/dc/elements/1.1/» >http://purl.org/dc/elements/1.1/»
xmlns:xlink http://www.w3.org/1999/xlink» >http://www.w3.org/1999/xlink»
xmlns:svg=»urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0″
xmlns:style=»urn:oasis:names:tc:opendocument:xmlns:style:1.0″
xmlns:table=»urn:oasis:names:tc:opendocument:xmlns:table:1.0″
xmlns:draw=»urn:oasis:names:tc:opendocument:xmlns:drawing:1.0″
xmlns:meta=»urn:oasis:names:tc:opendocument:xmlns:meta:1.0″
xmlns:number=»urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0″
I substituted the image with one of mine and this then loaded in Writer on my PC. As I said, the easiest way to create a document is to use writer in the first place and then retro-engineer your automated document from this.
Re: Create ODT with PHP
Post by noscollections » Sat Jan 26, 2008 4:18 pm
I found an other probleme : it’s «../CASERNE_POMPIERS.jpg» and not «CASERNE_POMPIERS.jpg» for external link in the same directory od the odt file
I will optimise the php code of my test file and poste the php code.
Re: [Solved] Create ODT with PHP
Post by noscollections » Wed Feb 13, 2008 10:14 am
With all the informations I have collected, I have done a php script, not really a class, to make an odt with php (5)
You need the file file odt_gen.php for the functions.
After I post an exemple of script that do an odt. the odt file is a real odt, it is zipped.
note : the image are link so when you open the odt file, break the link and OOO will includ them in the odt file
file odt_gen.php
createElement('text:p'); $XMLParagraph->setAttribute('text:style-name', 'P1'); $XMLOfficeText->appendChild($XMLParagraph); > // SAUT DE LIGNE // ----- function br()< global $content; global $XMLParagraph; global $XMLOfficeText; $XMLParagraph = $content->createElement('text:p'); $XMLOfficeText->appendChild($XMLParagraph); > // TEXTE SIMPLE // ----- function texte($text) < $Caracs = array("&" =>"&", "e" => "e"); $text=strtr($text, $Caracs); global $XMLText; global $content; global $XMLOfficeText; $XMLText = $content->createElement('text:p', utf8_encode($text)); $XMLOfficeText->appendChild($XMLText); > // TEXT et STYLE // ----- function textestyle($text,$style_name) < $Caracs = array("&" =>"&", "e" => "e"); $text=strtr($text, $Caracs); global $XMLText; global $content; global $XMLParagraph; $XMLText = $content->createElement('text:span', utf8_encode($text)); $XMLText->setAttribute('text:style-name', $style_name); $XMLParagraph->appendChild($XMLText); > // IMAGE // ----- function image($href,$style_name="fr1",$name="Image1", $anchor_type="paragraph" ,$x,$y , $width , $height )< global $XMLdraw; global $content; global $XMLParagraph; global $XMLimage; global $XMLOfficeText; // print $x."-".$y; $XMLdraw = $content->createElement('draw:frame'); $XMLdraw->setAttribute('draw:style-name', $style_name); $XMLdraw->setAttribute('draw:name', $name); $XMLdraw->setAttribute('text:anchor-type', $anchor_type); $XMLdraw->setAttribute('svg:x', $x.'cm'); $XMLdraw->setAttribute('svg:y', $y.'cm'); $XMLdraw->setAttribute('svg:width', $width.'cm'); $XMLdraw->setAttribute('svg:height', $height.'cm'); $XMLdraw->setAttribute('draw:z-index', '0'); $XMLParagraph->appendChild($XMLdraw); $XMLimage = $content->createElement('draw:image'); $XMLimage->setAttribute('xlink:href', $href); $XMLimage->setAttribute('xlink:type', 'simple'); $XMLimage->setAttribute('xlink:show', 'embed'); $XMLimage->setAttribute('xlink:actuate', 'onLoad'); $XMLimage->setAttribute('draw:filter-name', ''); $XMLdraw->appendChild($XMLimage); > //-------------------------------------- // --------DOC INIT -------------------- function odt_init()< global $manifest; global $XMLManifest; global $XMLFileEntry; $manifest = new DOMDocument('1.0', 'utf-8'); $XMLManifest = $manifest->createElement('manifest:manifest'); $XMLManifest->setAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0'); $manifest->appendChild($XMLManifest); $XMLFileEntry = $manifest->createElement('manifest:file-entry'); $XMLFileEntry->setAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.text'); $XMLFileEntry->setAttribute('manifest:full-path', '/'); $XMLManifest->appendChild($XMLFileEntry); $XMLFileEntry = $manifest->createElement('manifest:file-entry'); $XMLFileEntry->setAttribute('manifest:media-type', 'text/xml'); $XMLFileEntry->setAttribute('manifest:full-path', 'content.xml'); $XMLManifest->appendChild($XMLFileEntry); global $content; global $XMLOfficeContent; $content = new DOMDocument('1.0', 'utf-8'); $XMLOfficeContent = $content->createElement('office:document-content'); $XMLOfficeContent->setAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); $XMLOfficeContent->setAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'); $XMLOfficeContent->setAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'); $XMLOfficeContent->setAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); $XMLOfficeContent->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); $XMLOfficeContent->setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); $XMLOfficeContent->setAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'); $XMLOfficeContent->setAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'); $XMLOfficeContent->setAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0'); $XMLOfficeContent->setAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'); $XMLOfficeContent->setAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); $XMLOfficeContent->setAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0'); $content->appendChild($XMLOfficeContent); global $XMLAutomaticStyles; $XMLAutomaticStyles = $content->createElement('office:automatic-styles'); $XMLOfficeContent->appendChild($XMLAutomaticStyles); > function creat_style($name , $family='text' , $font_family, $font_weight , $font_style , $color , $background_color , $text_underline_type )< global $content; global $XMLStyle; global $XMLAutomaticStyles; global $XMLTextProperties; $XMLStyle = $content->createElement('style:style'); $XMLStyle->setAttribute('style:name', $name); $XMLStyle->setAttribute('style:family', $family); $XMLAutomaticStyles->appendChild($XMLStyle); $XMLTextProperties = $content->createElement('style:text-properties'); $XMLTextProperties->setAttribute('fo:font-family', $font_family); $XMLTextProperties->setAttribute('fo:font-weight', $font_weight); $XMLTextProperties->setAttribute('fo:font-style', $font_style); $XMLTextProperties->setAttribute('fo:color', $color); $XMLTextProperties->setAttribute('fo:background-color', $background_color); $XMLTextProperties->setAttribute('style:text-underline-type', $text_underline_type); $XMLStyle->appendChild($XMLTextProperties); > function creat_style_img($name )< global $content; global $XMLStyle; global $XMLAutomaticStyles; global $XMLTextProperties; $XMLStyle = $content->createElement('style:style'); $XMLStyle->setAttribute('style:name', $name); $XMLStyle->setAttribute('style:family', 'graphic'); $XMLStyle->setAttribute('style:parent-style-name', 'Graphics'); $XMLAutomaticStyles->appendChild($XMLStyle); $XMLTextProperties = $content->createElement('style:graphic-properties'); $XMLTextProperties->setAttribute('style:run-through', 'foreground'); $XMLTextProperties->setAttribute('style:wrap', 'dynamic'); $XMLTextProperties->setAttribute('style:number-wrapped-paragraphs', 'no-limit'); $XMLTextProperties->setAttribute('style:wrap-contour', 'false'); $XMLTextProperties->setAttribute('style:vertical-pos', 'from-top'); $XMLTextProperties->setAttribute('style:vertical-rel', 'paragraph'); $XMLTextProperties->setAttribute('style:horizontal-pos', 'from-left'); $XMLTextProperties->setAttribute('style:horizontal-rel', 'paragraph'); $XMLTextProperties->setAttribute('style:mirror', 'none'); $XMLTextProperties->setAttribute('fo:clip', 'rect(0cm 0cm 0cm 0cm)'); $XMLTextProperties->setAttribute('draw:luminance', '0%'); $XMLTextProperties->setAttribute('draw:contrast', '0%'); $XMLTextProperties->setAttribute('draw:red', '0%'); $XMLTextProperties->setAttribute('draw:green', '0%'); $XMLTextProperties->setAttribute('draw:blue', '0%'); $XMLTextProperties->setAttribute('draw:gamma', '100%'); $XMLTextProperties->setAttribute('draw:color-inversion', 'false'); $XMLTextProperties->setAttribute('draw:image-opacity', '100%'); $XMLTextProperties->setAttribute('draw:color-mode', 'standard'); $XMLStyle->appendChild($XMLTextProperties); > function doc_init()< global $content; global $XMLOfficeBody; global $XMLOfficeContent; global $XMLOfficeText; global $XMLParagraph; $XMLOfficeBody = $content->createElement('office:body'); $XMLOfficeContent->appendChild($XMLOfficeBody); $XMLOfficeText = $content->createElement('office:text'); $XMLOfficeBody->appendChild($XMLOfficeText); $XMLParagraph = $content->createElement('text:p'); $XMLOfficeText->appendChild($XMLParagraph); > ?>
open('hello-world2.odt', ZIPARCHIVE::OVERWRITE); $document->addFromString('META-INF/manifest.xml', $manifest->saveXML()); $document->addFromString('content.xml', $content->saveXML()); $document->close(); // juste for be sure the script is finish ! print "OK"; ?>
I hove that could help some of you
If you add funcionnality, juste complete the post