如何在使用Apache-FOP生成的PDF中将图像组织成网格?

huangapple go评论50阅读模式
英文:

How to organise images in a grid in a PDF generated with Apache-FOP?

问题

我正在使用Apache FOP生成PDF文件。我有以下模板片段:

<xsl:template match="images">
    <xsl:apply-templates select="image"/>
</xsl:template>

<xsl:template match="image">
    <fo:block space-after="14px">
        <fo:external-graphic content-height="200pt" height="auto" width="auto"
                             src="url('data:{type};base64,{data}')"/>
    </fo:block>
</xsl:template>

还有XML数据:

<images>
    <image>
        <type>image/png</type>
        <data>iVBORw0KGgoAAAANS...</data>
    </image>
    <image>
        <type>image/png</type>
        <data>iVBORw0KGgoAAAANS...</data>
    </image>
    // ... 更多图片
</images>

如何修改这些代码以使图像呈现为每页3x2的两列网格?(必须相应地分配图像的大小)

英文:

I am generating PDF-s with Apache-fop. I have the following template snippet:

&lt;xsl:template match=&quot;images&quot;&gt;
    &lt;xsl:apply-templates select=&quot;image&quot;/&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;image&quot;&gt;
    &lt;fo:block space-after=&quot;14px&quot;&gt;
        &lt;fo:external-graphic content-height=&quot;200pt&quot; height=&quot;auto&quot; width=&quot;auto&quot;
                             src=&quot;url(&#39;data:{type};base64,{data}&#39;)&quot;/&gt;
    &lt;/fo:block&gt;
&lt;/xsl:template&gt;

And the XML data:

&lt;images&gt;
    &lt;image&gt;
        &lt;type&gt;image/png&lt;/type&gt;
        &lt;data&gt;iVBORw0KGgoAAAANS...&lt;/data&gt;
    &lt;/image&gt;
    &lt;image&gt;
        &lt;type&gt;image/png&lt;/type&gt;
        &lt;data&gt;iVBORw0KGgoAAAANS...&lt;/data&gt;
    &lt;/image&gt;
    /// ... more images
&lt;/images&gt;

How can I change this so the images get in a two column grid, 3x2 image each page? (Size of the images have to be assigned accordingly)

答案1

得分: 0

我会在每两张图片之后输出一个换行,或者在每六张图片之后使用模运算符 `mod` 输出一个分页符,如下所示:

<xsl:template match="images">
<fo:block><xsl:apply-templates select="image"/></fo:block>
</xsl:template>

<xsl:template match="image">
<fo:external-graphic
content-height="200pt" height="auto" width="auto"
padding-after="14px"
src="url('data:{type};base64,{data}')"/>
<xsl:if condition="position() mod 2 = 0">
<fo:block/>
</xsl:if>
<xsl:if condition="position() mod 6 = 0">
<fo:block page-break-before="always"/>
</xsl:if>
</xsl:template>

如果一行中没有足够的空间放置三张图片,那么在两张图片后会自动换行。在这种情况下,不需要手动断开行。分页符的情况也是如此。

<details>
<summary>英文:</summary>

***EDITED ANSWER***
I would output a line break after each 2 pictures or a page break after each 6 pictures using the operator modulo `mod` as in:

<xsl:template match="images">
<fo:block><xsl:apply-templates select="image"/></fo:block>
</xsl:template>

<xsl:template match="image">
<fo:external-graphic
content-height="200pt" height="auto" width="auto"
padding-after="14px"
src="url('data:{type};base64,{data}')"/>
<xsl:if condition="position() mod 2 = 0">
<fo:block/>
</xsl:if>
<xsl:if condition="position() mod 6 = 0">
<fo:block page-break-before="always"/>
</xsl:if>
</xsl:template>

If there is no room for 3 pictures in a row, the line will break automatically after 2 pictures. In that case, it will not be necessary to break the line manually. The same can be said for the page break.


</details>



# 答案2
**得分**: 0

你可以将每个`fo:table-cell`中放置一个包含`fo:external-graphic`的`fo:block`。

实际上,你可以将它们全部放在一个`fo:table-row`中,并在每个第二个`fo:table-cell`上指定`ends-row="true"`。

你可以设置行高和列宽以在每一页上获得常规的3x2排列。

<details>
<summary>英文:</summary>

You could put an `fo:block` containing an `fo:external-graphic` in each `fo:table-cell` of a two-column table.

Actually, you could put them all in one `fo:table-row` and specify `ends-row=&quot;true&quot;` on every second `fo:table-cell`.

You could set the row height and column widths to get your regular 3x2 arrangement on each page.

</details>



huangapple
  • 本文由 发表于 2023年5月30日 03:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定