4

我一直在尝试使链接在 openTBS 生成的文档中起作用。到目前为止没有运气:(

  • 能做什么:通过一些技巧,我可以创建一个带有链接的模板,并通过我的变量更改链接标题和 href。
  • 不能做的:创建一个带有链接的块,用 MergeBlock 填充它并使其与我的 php 对象数组一起使用。

我完全迷路了,花了几天时间试图弄清楚如何做到这一点。这让我很烦恼,因为这似乎是 openTBS 可以自己处理的事情,没有问题。

我这是我的 php 代码:

<?    
include_once('tbs/tbs_class.php');
include_once('tbs/plugins/tbs_plugin_opentbs_1.8.1/tbs_plugin_opentbs.php');
$TBS = new clsTinyButStrong('!-,-!');  
//the special pattern is needed because 
//word replaces [] brackets when put in link's href.

$TBS -> Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
//some variables for mergeing with the template
$tmpl_headname='Sarah';
$tmpl_headlink='http://example.com/?user=sarah';
$tmpl_items = array(
  array('title'=>'My title', 'url'=>'http://myurl.com/firstarticle'),    
  array('title'=>'My second title', 'url'=>'http://myurl.com/secondarticle'),
  array('title'=>'My third title', 'url'=>'http://myurl.com/thirdarticle')
);

$TBS->LoadTemplate('sampledoc.docx');
$TBS->MergeBlock('item',$tmpl_items);
$TBS->Show(OPENTBS_DOWNLOAD, 'sample_filename_doc');
?>

我的Word模板:

This is your unique link, !-onload.tmpl_headname-!  (points to: !- onload.tmpl_headlink-!)
!-item;block=begin;tbs:page-!

  !-item.title-!
  Link to the website  (points to: !-item.url-!)
  ***
!-item;block=end;tbs:page-!

在我的 Word 模板中,链接指向!-item.url-!,并且在其上运行 openTBS 后保持不变。问题是,在 Docx zip 存档中word/_rels/document.xml.rels˙,它们看起来没有变化:

<Relationship TargetMode="External" Target="!-item.url-!" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Id="rId8"/>

非常感谢任何帮助!:)


此外,对于那些想用 openTBS 更改链接 url(但不是在合并块模式下!)的人,我找到了一种解决方法:打开 document.xml.rels 作为模板,并在其上运行 openTBS:

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

这个 hack 不适用于 mergeblock,因为 !-item.url-! 成为每个资源的目标,你无法分辨哪个块迭代是哪一个:(


编辑:

OpenTBS 生成带有rId前缀:rId1rId2等的 id。资源文件中的每个其他项目都与rId[x]模式链接。运行 openTBS 后,我在 document.xml 中得到了这个 xml 代码,代表以下 Word 部分:

***
My second title
Link to the website 

在“网站链接”位上有一个链接。...

<w:p w:rsidRDefault="00886D12" w:rsidP="00886D12">
   <w:r>
    <w:t xml:space="preserve">
     ***
    </w:t>
   </w:r>
   <w:r>
    <w:br/>
   </w:r>
   <w:r>
    <w:t>
     My second title
    </w:t>
   </w:r>
   <w:r>
    <w:br/>
   </w:r>
   <w:hyperlink r:id="rId7" w:history="1">
    <w:r>
     <w:rPr>
      <w:rStyle w:val="Hyperlink"/>
     </w:rPr>
     <w:t xml:space="preserve">
      Link to the website
     </w:t>
    </w:r>
   </w:hyperlink>
  </w:p>
  ...

document.xml.rels 文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
  <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="!-item.url-!" TargetMode="External"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml"/>
  <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml"/>
  <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml"/>
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
  <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
</Relationships>

我也许可以使用放在 Target 属性中的特殊 openTBS 代码来复制超链接资源项,但是我也必须在 document.xml 上使用新的 rID。

谢谢你的帮助!

4

2 回答 2

0

这很难在评论中显示。

您可能需要使用att参数:http ://www.tinybutstrong.com/manual.php#html_field_prm

类似(未经测试):

<Relationship TargetMode="External" Target="" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Id="rId8">
    !-block=item;att=Target;item.url-!
</Relationship>

你可以玩弄att=Relationship#Target而不是像我一样打开关系标签。像这样的东西(真的未经测试):

<Relationship TargetMode="External" Target="" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Id="rId8"/>
!-block=item;att=Relationship#Target;item.url-!

这是否允许块正确重复,因此您不会将块全部分组到单个目标属性中?

显然,您还需要为每个rId属性提供一个唯一的 ID。您可能希望在此处遵循 OpenTBS 的示例并使用与 Word 不同的结构来避免冲突。类似rId=mytbs1而不仅仅是rId=1(OpenTBSopentbs1用于图片,但您也不想在那里发生冲突)。

我确信 Skrol29 会阅读这篇文章,并会考虑是否有可能/合理更新 OpenTBS 来处理这个问题。


编辑:

在看到 XML 输出后,我最好的猜测是添加一个您想要使用的唯一 rId 到填充您的块的数组中。就像是:

$items = array(
    array('title' => 'My Title1', 'url' => 'http://my.url', 'rId' => 'myTBS1'),
    array('title' => 'My Title2', 'url' => 'http://my.url', 'rId' => 'myTBS2'),
);

然后,在您的模板中:

!-item;block=begin;tbs:page-!
    !-item.title-!
    Link to the website
    ***
!-item;block=end;tbs:page-!

其中“网站链接”有一个超链接,其内容是(类似于):

!-block=item;item.url-!!-item.rId;att=rId-!

然后在您的rels模板中:

<Relationship TargetMode="External" Target="" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Id="rId8">
    !-block=item;att=Target;item.url-!
    !-item.rId;att=rId-!
</Relationship>

只是一个保持 rIds 链接的想​​法。我不确定我的语法是否正确,以及单个标签上的多个属性更改的规则是什么……希望 Skrol29 能够提供更好的解决方案,如果我们在这里吠叫错误的树。

于 2013-10-28T15:57:29.503 回答
0

是的,没看错!因此,根据 Sarah 的建议,我能够在 Word 文档的 rels 文件和 document.xml 中伪造链接 rId。

我就是这样做的:

  1. 在 word 文件中,block 中的每个链接都指向一个示例 urlhttp://mypage.com选择哪个 url 无关紧要,原因如下:在 block 多次乘以后,这些 block 中的所有链接将保持链接到同一个<Realtionship>tag rels 文件,我们即将修改此连接,因此没有链接将指向原始mypage.com <Relationship>项目。

  2. 为此,我在链接中的实际文本之后插入了以下文本所以 !-item.rId;att=w:hyperlink#r:id;-!我的链接现在看起来像 : My website!-item.rId;att=w:hyperlink#r:id;-!。此标记的作用是更改链接的默认 r:id 属性(这是 Word 在document.xml和 rels 文件之间链接项目的属性_rels/document.xml.rels)。r:id 将更改为自定义 id 名称,您必须在合并的 php 数组中提供该名称!这是我的 rId 的 php 代码:

    $tmpl_items = array(array('title'=>'我的标题', 'url'=>' http://myurl.com/firstarticle ','rId' => 'linkRid1'),
    array('title'= >'我的第二个标题', 'url'=>' http://myurl.com/secondarticle ','rId' => 'linkRid2'), array('title'=>'我的第三个标题', 'url' =>' http://myurl.com/thirdarticle ','rId' => 'linkRid3') );

    请注意,rId 是我们将替换默认 r:ids 的值。 当然 openTBS 标记会从链接文本中删除。

  3. 破解 rels 文件。 !-item.block;block=begin;-! <Relationship Id="!-item.rId-!" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="!-item.url-!" TargetMode="External"/> !-item.block;block=end;-!
  4. 也在 rels 文件上运行 merge_block。这样,<Relationship>openTBS 也会生成项目。此外,这意味着,到目前为止,您的 Word 文件已损坏,因为您破坏了 rels 文件中的有效 XML 语法,并且无法再在 Word 中进行编辑。

    $TBS->LoadTemplate('tbs/sampledoc_stackof2.docx'); $TBS->MergeBlock('item',$tmpl_items); $TBS->LoadTemplate('#word/_rels/document.xml.rels'); $TBS->MergeBlock('item',$tmpl_items);

在所有这些麻烦之后,一切看起来都很好。我在 rels 文件中有这些:

 <Relationship Id="linkRid1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://myurl.com/firstarticle" TargetMode="External"/>
 <Relationship Id="linkRid2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://myurl.com/secondarticle" TargetMode="External"/>
 <Relationship Id="linkRid3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://myurl.com/thirdarticle" TargetMode="External"/>

我在 document.xml 中也得到了 rId:

<w:p w:rsidRDefault="004842D8" w:rsidP="008A11F0">
   <w:hyperlink r:id="linkRid1" w:history="1">
    <w:r>
     <w:rPr>
      <w:rStyle w:val="Hyperlink"/>
     </w:rPr>
     <w:t>
      My website
     </w:t>
    </w:r>
   </w:hyperlink>
  </w:p>

现在在文档上运行 openTBS 将生成包含合并链接的 docx 文件。唯一的缺点是,随着 rels 文件被黑,我失去了 xml 的有效性,以及以后在 Word 中编辑文档的机会。任何建议,如何避免这种情况都会受到重视,尽管我们需要一个解决方案,让 Word 在保存模板文档后保持 rels 文件的被黑部分完好无损。

感谢莎拉提供的大量帮助!

我现在正在打开一瓶酒。

于 2013-11-04T12:50:58.337 回答