Categories > [old] TbsOoo & TinyDoc >

Convert html-styles to open office doc.

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Jeroen van Sluijs
Date: 2006-06-15
Time: 14:43

Convert html-styles to open office doc.

Hello,

I use a WYSIWYG editor, which stores the text in html-format.

Is it possible to convert html-styles like <b>bold text</b>, so the 'bold text' will actually be bold in the generated .odt file? I noticed only line-breaks are converted (\n converted to <text:line-break/>).

E.g. template.odt:
Hello, this is my [tag].

The tag is to be replaced by <b>bold text</b>, so the result will be an output_document.odt with the string 'bold text' as bold text.

I've been experimenting with this some time, but I can only manage to find a quite difficult/nasty solution. This would be looking up the bold style in the xml-document (after <office:automatic-styles>) and then inserting <b>bold text</b> in the content.xml as

<text:span text:style-name="T1">bold text</text:span>.

The ugly way would be adding a bold, italic and underlined text at the beginning of the template. In this way, you'll always be sure that bold is mapped to T1, italic->T2 and underlined->T3.

Thanks for any help in advance,
Jeroen

ps. I think I just posted a thread in the wrong forum. I'll try it here again :)
By: Olivier Loynet
Date: 2006-06-19
Time: 07:16

Re: Convert html-styles to open office doc.

Hi,

You're in the good forum...

I understand your question, and as now, you can't send some XML tags in the data, the < and > are converted to &lt; and &gt;
It will be possible next as with new parameters with tag like for HTML
http://www.tinybutstrong.com/manual.php#html_field_prm_htmlconv

You can make a workaround in this way in your template:
[var.content;if [var.style]=1;then [val]][var.content;if [var.style]=2;then [val]]

Then you select [var.content;if [var.style]=1;then [val]] and apply your first style, like bold

Second you select [var.content;if [var.style]=2;then [val]] and apply your first style, like italic

and in your PHP code you can do :

<?php
include_once('../tbs_class.php');
include_once('../tbsooo_class.php');

// datas
$content = 'a lazzy dog';
$style   = 2;

// instantiate a TBS OOo class
$OOo = new clsTinyButStrongOOo;

// setting the object
$OOo->SetZipBinary('zip');
$OOo->SetUnzipBinary('unzip');
$OOo->SetProcessDir('tmp/');

// create a new openoffice document from the template with an unique id
$OOo->NewDocFromTpl('change_style.odt');

// merge data with openoffice file named 'content.xml'
$OOo->LoadXmlFromDoc('content.xml');
$OOo->SaveXmlToDoc();

// display
header('Content-type: '.$OOo->GetMimetypeDoc());
header('Content-Length: '.filesize($OOo->GetPathnameDoc()));
$OOo->FlushDoc();
$OOo->RemoveDoc();
?>

I've made a test and it works.

Now, you have to make a converter from HTML by analyse your data content and call the good style

Hope that can help
Olivier


By: Jeroen van Sluijs
Date: 2006-06-20
Time: 11:22

Re: Convert html-styles to open office doc.

Hi Olivier

Thanks for the example. I managed to get it work on my PC as well. I've still got 2 questions:

1) I'm only wondering how these global varables $content and $style work. Do they have to be global, or can I put them somewhere else in my own application. In other words, where are $style and $content mapped on 'style' and 'content' in the .odt template?

2) I'm not quite sure if I can use this way of using styles. Let's say I've got this html text:
The <b>quick</b> brown <i>fox</i> <b>jumps</b>

My php-code would be:
$contentA=The
$styleA=1 // normal

$contentB=quick
$styleB=2 // bold

$contentC=brown
$styleC=1

$contentD=fox
$styleD=3 // italic

$contentE=jumps
$styleE=2 // bold

So I'll have to pre-define $contentA - $contentE and $styleA - $styleE in my .odt templte? Or am I misunderstanding something now?

Thanks,
Jeroen
By: Olivier Loynet
Date: 2006-06-20
Time: 15:28

Re: Convert html-styles to open office doc.

Hi,

I understand your problem to transform a HTML string like your example to the XML OOo equivalent with OOo style
The <b>quick</b> brown <i>fox</i> <b>jumps</b>

As now, you can't do it easy, and the example I send to you is just to swap beetween 2 styles or more, but for the entire string.

You can try in the class to comment the following lines
    $string_encode = str_replace('<'   ,'&lt;',  $string_encode);
    $string_encode = str_replace('>'   ,'&gt;',  $string_encode);
by
    // $string_encode = str_replace('<'   ,'&lt;',  $string_encode);
    // $string_encode = str_replace('>'   ,'&gt;',  $string_encode);

so you can merge your data with yours XML tags...

exemple of XML in the 'content.xml'

<text:p text:style-name="Standard">[var.foo]</text:p>

and merge your OOo file with the PHP var like is $foo

$foo = 'The <text:span text:style-name="T1">quick</text:span> brown <text:span text:style-name="T2">fox</text:span> <text:span text:style-name="T1">jumps</text:span>';

Be sure in yours data to convert the < and > if there are no XML tags because you'll have an error while opening your OOo file.

Olivier

By: Jeroen van Sluijs
Date: 2006-06-20
Time: 15:59

Re: Convert html-styles to open office doc.

Hi,

Only thing is, that you always have to define "T1" and "T2" at the beginning of the open office template, to be sure which tag is which style. Right?

I already noticed that "T1" becomes the tag for the first used style and "T2" for the 2nd one and so on.

Maybe it can be achieved by using:
[var.content;if [var.style]=1;then [val]][var.content;if [var.style]=2;then [val]]
And fill in an empty string for the content, so a user will not see it when his document is generated.

Greetings,
Jeroen

ps. I hope this will work. I'm looking for a solution to use 'bullets' as well.