XSL-FO:分配矩形的绝对定位而不是内容矩形

时间:2017-08-03 18:57:53

标签: xsl-fo apache-fop

我正在实施'仪表板的PDF导出'使用XSL-FO(使用Apache FOP作为渲染引擎)。这些仪表板被定义为在特定大小的区域内呈现的一组矩形元素,其位置由百分比上/左偏移和百分比宽度/高度定义。使用XSL-FO绝对定位渲染它似乎是一个显而易见的解决方案,而且大多数情况下运行良好。

以下XSL-FO:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-size="10pt" id="sequence-id">
<fo:layout-master-set>
<fo:simple-page-master master-name="page-layout" page-width=" 4in " page-height=" 4in ">
    <fo:region-body region-name="document-content" margin="0.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="page-layout" initial-page-number="1">
<fo:flow flow-name="document-content">
<fo:block-container border="1px solid green" height="3in">
<fo:block-container absolute-position="absolute" width="50%" left="0" top="0" height="40%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content. It is possible for me to occupy the entire width of my allocated area.</fo:block>
</fo:block-container>
<fo:block-container absolute-position="absolute" width="50%" left="50%" top="0" height="50%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
<fo:block-container absolute-position="absolute" width="50%" left="0" top="40%" height="60%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
<fo:block-container position="absolute" width="50%" left="50%" top="50%" height="50%" border="1px solid red">
    <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
</fo:block-container>
</fo:block-container>
</fo:flow>
</fo:page-sequence>
</fo:root>

...产生以下输出:

Example with no padding

绿色区域,代表仪表板元素的内容,彼此对接,这是丑陋的。所以,我想在内容周围添加一些填充。如果我向表示第一个仪表板元素的block-container添加填充:

<fo:block-container absolute-position="absolute" padding="5px" width="50%" left="0" top="0" height="40%" border="1px solid red">

...仪表板元素的(红色)边框向外扩展而不是向内添加填充:

Example with padding naively added

这是因为,与CSS相比,XSL-FO中的绝对定位偏移解决了内容矩形(即,填充内部)而不是包括填充和边界的矩形。每the spec

  

这些属性设置相关区域的内容矩形的位置。

我想通过绝对定位属性在我分配给仪表板元素的区域内添加填充/边框。我已经尝试了将内容放在另一个嵌套block-containerblock中的各种内容,但我尝试过的任何内容都无法正常工作。作为最后的手段,我可​​以计算偏移量/尺寸以有效地在元素之间留下一些空白,但这感觉就像应该由渲染引擎自动处理的东西。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

所有维度的最简单方法...在块容器中添加一个块,在其中添加所有内容。此块应具有您所需的保证金。所以:

    <fo:flow flow-name="document-content">
        <fo:block-container border="1px solid green" height="3in">
            <fo:block-container absolute-position="absolute" width="50%" left="0" top="0" height="40%" border="1px solid red">
                <fo:block margin="5px">
                    <fo:block border="1px solid blue" background-color="green">I am some content. It is possible for me to occupy the entire width of my allocated area.</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container absolute-position="absolute" width="50%" left="50%" top="0" height="50%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container absolute-position="absolute" width="50%" left="0" top="40%" height="60%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
            <fo:block-container position="absolute" width="50%" left="50%" top="50%" height="50%" border="1px solid red">
                <fo:block margin="5px">
                <fo:block border="1px solid blue" background-color="green">I am some content</fo:block>
                </fo:block>
            </fo:block-container>
        </fo:block-container>
    </fo:flow>

产生这个:

enter image description here