英文:
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:
<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>
And the XML data:
<images>
<image>
<type>image/png</type>
<data>iVBORw0KGgoAAAANS...</data>
</image>
<image>
<type>image/png</type>
<data>iVBORw0KGgoAAAANS...</data>
</image>
/// ... more images
</images>
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="true"` 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论