Categories > OpenTBS with DOCX >

Create a hyperlink to a file

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Marco
Date: 2013-03-25
Time: 10:32

Create a hyperlink to a file

Hello,

is there any way to create a hyperlink to a file while merging a MS Word template?

Merging the address with [a.name;block=w:tr] does not work because this leads to %5ba.name;block=w:tr%5d

Thanks

Marco
By: Sarah
Date: 2013-03-25
Time: 17:16

Re: Create a hyperlink to a file

I wouldn't think that creating a hyperlink in a Word template should cause problems. Can you give more context? Where is this tag located - in the main template or a subtemplate? Is the tag located inside of another tag?
By: Marco
Date: 2013-03-26
Time: 07:55

Re: Create a hyperlink to a file

Hi Sarah,
The hyperlink will be inside a table in future but first I tried to change one in an empty document.
The text changes but the link doesn't because Word replaces [ with  %5b after closing the window for creating a hyperlink.
Thank you
Marco
By: Sarah
Date: 2013-03-26
Time: 16:16

Re: Create a hyperlink to a file

Ah, I see your problem now. I forget that mostly people build these templates in Word itself - I have been in the XML too long.

I cannot find any way to get Word to not convert certain characters (including [ ] and { }) in the hyperlink input (at least in Word 2007). You can, however, choose different tags for TBS to recognize. For instance, if you use != and =!, Word will not convert them and TBS should be able to use these as long as you have a version >=2.02 and you use:

$TBS = new clsTinyButStrong('!=,=!');

You would have to change all of the tags in your template for them to work though. You can read about changing markers here: http://www.tinybutstrong.com/support.php#faq_markers

FYI, I have not used this functionality.
By: Marco
Date: 2013-03-27
Time: 08:19

Re: Create a hyperlink to a file

Thank you very much!
Now the %5b is gone and standard tags work as normal. But TBS does not replace the placeholder of the address.

How do I make TBS to change the target of the hyperlink?
The next problem will be the creation of new relationships, I think.

<Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="!=a.name;block=w:tr=!" TargetMode="External" />

My php-code:

<?php
include_once('tbs_class_php5.php');
include_once('../tbs_plugin_opentbs.php');
$TBS = new clsTinyButStrong('!=,=!');

$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load OpenTBS plugin

// Retrieve the template to open
$template = 'Test.docx';
$template = basename($template);

if (!file_exists($template)) exit("File does not exist.");

// Prepare some data for the demo
$a = array();
$a[] = array('name'=>'test.PDF', 'desc'=>'desc01');
$a[] = array('name'=>'name02'  , 'desc'=>'desc02');
$a[] = array('name'=>'name03', 'desc'=>'desc03');

$b = array();
$b[] = array('name'=>'name11' , 'desc'=>'desc11');
$b[] = array('name'=>'name12'  , 'desc'=>'desc12');
$b[] = array('name'=>'name13', 'desc'=>'desc13');

// Load the template
$TBS->LoadTemplate($template);

// Merge data
$TBS->MergeBlock('a', $a);
$TBS->MergeBlock('b', $b);

// save as file
$file_name = "Testedited.docx";
$TBS->Show(OPENTBS_FILE+TBS_EXIT, "Testedited.docx");





Thank you!
Marco


By: Sarah
Date: 2013-03-27
Time: 16:48

Re: Create a hyperlink to a file

When you say "standard tags work as normal" do you mean you are still able to use [ and ] style tags when you have $TBS = new clsTinyButStrong('!=,=!'); ?

In my tests, this was not true. Either != =! tags worked, or [ ] tags - not both at once. I am using TBS 3.8.0.

If [ and ] tags are still working, there must be something wrong with the new tag selection. What TBS version are you using?
By: Marco
Date: 2013-03-28
Time: 10:25

Re: Create a hyperlink to a file

No, that's not the problem. I've changed every [ ] to != =!.

The point is that the target of the hyperlink
Target="!=a.name;block=w:tr=!"
has not been replaced by TBS. Everything else works with != =!.

I am using the newest TBS version.

Thank you.
Marco
By: Sarah
Date: 2013-03-28
Time: 16:30

Re: Create a hyperlink to a file

I see now. I'm afraid I have not worked enough with links to help much more, I have not attempted to edit the relationship declarations directly.

It may be necessary to load the word/_rels/document.xml.rels template in addition to your main template to give TBS access to the tag in the Target attribute (I believe that is where relationship information is stored). You can read more about this file here: http://www.phpclasses.org/browse/file/29261.html

If you open up your template archive and look at this file and see your tag that is not merging correctly there, I would certainly give load this file as a second template a shot.

// Load the template
$TBS->LoadTemplate($template);
//Load the relationships template
$TBS->LoadTemplate('#_rels/document.xml.rels');

(I haven't tried this, it works with xml files in the archive but I don't know about .rels files...)
By: Marco
Date: 2013-04-02
Time: 08:57

Re: Create a hyperlink to a file

Hi Sarah,
your idea works :)
But the block=w:tr-part doesn't because the relationship is not a table. If I leave block=w:tr away, the address is always the same: test.PDF.
How can I make it varying?

Thank you very much
Marco


<?php
include_once('tbs_class_php5.php');
include_once('../tbs_plugin_opentbs.php');
$TBS = new clsTinyButStrong('!=,=!');

$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load OpenTBS plugin

// Retrieve the template to open
$template = 'test.docx';
$template = basename($template);

if (!file_exists($template)) exit("File does not exist.");

// Prepare some data for the demo
$a = array();
$a[] = array('name'=>'test.PDF', 'desc'=>'desc01');
$a[] = array('name'=>'name02'  , 'desc'=>'desc02');
$a[] = array('name'=>'name03', 'desc'=>'desc03');

$b = array();
$b[] = array('name'=>'name11' , 'desc'=>'desc11');
$b[] = array('name'=>'name12'  , 'desc'=>'desc12');
$b[] = array('name'=>'name13', 'desc'=>'desc13');

// Load the template
$TBS->LoadTemplate($template);
$TBS->MergeBlock('a', $a);
$TBS->MergeBlock('b', $b);

$TBS->LoadTemplate('#word/_rels/document.xml.rels');

// Merge data
$TBS->MergeBlock('a', $a);
$TBS->MergeBlock('b', $b);

// save as file
$file_name = "Testedited.docx";
$TBS->Show(OPENTBS_FILE+TBS_EXIT, "Testedited.docx");
By: Sarah
Date: 2013-04-02
Time: 17:09

Re: Create a hyperlink to a file

I'm glad we're making progress!

I would try block=relationship, and if that doesn't work, you can always try making your own block boundaries inside the attribute:

<Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="!=a;block=begin;=!!=a.name;=!!=a;block=end=!" TargetMode="External" />

I'm sort of regretting my choice of the != =! tags as they are quite confusing to read... but hopefully you see what I am getting at here. You can see an example of this kind of block declaration in the manual ( http://www.tinybutstrong.com/manual.php#html_block ) under 'Explicit Syntax'.
By: Marco
Date: 2013-04-03
Time: 13:32

Re: Create a hyperlink to a file

Hi!
block=relationship does not work. It currupts the document.

Target="!=a;block=begin;=!!=a.name;=!!=a;block=end=!" leads to 3 times test.PDFname02name03.

Thank you
Marco
By: Sarah
Date: 2013-04-03
Time: 17:34

Re: Create a hyperlink to a file

I haven't worked with links but I think they are similar to pictures in that there are two distinct locations that need to be updated and kept in sync and I don't know the best way to access them both within the bounds that Word allows. This seems like something OpenTBS would do for you, but I couldn't find anything in the documentation about hyperlinks. You could try adding something like this in the hyperlink target:
//in the PHP
$a = array();
$a[] = array('rId'=>'myUniquePrefix01', 'name'=>'test.PDF', 'desc'=>'desc01');
$a[] = array('rId'=>'myUniquePrefix02', 'name'=>'name02'  , 'desc'=>'desc02');
$a[] = array('rId'=>'myUniquePrefix03', 'name'=>'name03', 'desc'=>'desc03');

// in the relationship file (the 'Target=' segment would go into them target attribute of hyperlink in word)
<Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="!=a;a.name;block=Relationship;att=Relationship#Target;=!!=a.rId;block=Relationship;att=Relationship#Id;=!" TargetMode="External" />

And then make sure you are also using a block to propagate the anchor text and you change the id for that too - you haven't shown the XML for the anchor text so I don't have an example, but you would need something just like the second tags in the target shown above:
!=a.rId;block=whateverAnchorTextIsContainedBy;att=whateverAnchorTextIsContainedBy#Id;=!!=a.desc;block=whateverAnchorTextIsContainedBy;=!

Haven't tested this - it's just a starting point.

If you end up with a corrupted document, use OpenTBS's $TBS->Show(OPENTBS_DEBUG_XML); to see where the problem is, or take a note of the line and character number Word gives you and extract the .docx manually and look at the xml at that location to see what your problem is.