【问题标题】:Need to scale images to fit using XSL FO需要使用 XSL FO 缩放图像以适应
【发布时间】:2013-10-25 00:18:44
【问题描述】:

我正在处理一个项目,其中我们有图表和表格导出为 PDF。我们使用 Apache FOP 生成 PDF。图表采用 SVG 格式,如果只有一个,则可以正常显示。但是,我们会遇到最多四个的情况,在这些情况下,四个中只有三个适合生成的 PDF。无论数量如何,我都需要所有图像都适合。恐怕当我们需要更多内容时,简单地将页面放在横向将是一个临时解决方案。每个图表的大小都是从 Highcharts 库在客户端生成的 svg 中以像素为单位设置的。即使我按照此处看到的示例为 instream-foreign-object 设置了属性,图像似乎也没有任何缩放就被插入到 pdf 中:Scaling images using scale-to-fit

所以我需要每个图表“缩小以适应”。我尝试设置包含每个包含 instream-foreign 对象的元素的元素的长度和宽度(我也包括我的 xsl)。在 instream-foreign 对象中,我将 svg 宽度和高度分配给实际图表 svg 中的值。我想也许这就是问题所在,但如果我不包括这些维度,图表就不会出现在 pdf 中。我还尝试根据此处的问题将缩放属性设置为“非均匀”:Scale images with fixed height,结果也好不到哪里去。它实际上在整个页面上拉伸了一个图表,因此只显示了四个中的一个。这是我的 xsl:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <fo:root>
    <fo:layout-master-set>
        <fo:simple-page-master master-name="exportPage">
            <fo:region-body />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="exportPage">
        <fo:flow flow-name="xsl-region-body">
            <fo:block id="mainTitleBlock" font-family="arial, helvetica, sans-serif" color="#5c068c" font-weight="bold">
                <xsl:value-of  select="exports/export/charts/@mainTitle"/>
            </fo:block>
            <fo:block id="timestampBlock" font-family="arial, helvetica, sans-serif" font-size="small" color="#6D869F">
                <xsl:value-of  select="exports/export/charts/@timeStamp"/>
            </fo:block>
            <!-- This is where the charts go -->
            <!-- Within this block will be an inline element for each chart--> 
        <fo:block id="chartBlock" width="8.25in" height="6in">

          <xsl:apply-templates select="exports/export/charts/chart"/>

        </fo:block>
                <fo:block id="tableBlock" >
                    <xsl:apply-templates select="exports/export/table"/>
                </fo:block>
            </fo:flow>
            </fo:page-sequence>
         </fo:root>
</xsl:template>
   <!-- Creates the table -->
<xsl:template match="table">
<fo:table table-layout="fixed" width="100%" >
    <fo:table-header>
    <fo:table-row>
    <xsl:apply-templates select="tblRow[position() = 1]"/>
    </fo:table-row>
    </fo:table-header>
    <fo:table-body>
    <xsl:apply-templates select="tblRow[position() > 1]"/>
    </fo:table-body>
</fo:table>
</xsl:template>

<xsl:template match="hdrCell">
    <fo:table-cell background-color="#666" border-right-style="solid" border-right-width="1px" border-right-color="white" empty-cells="show">
    <fo:block color="white" font-family="arial, helvetica, sans-serif" font-size="x-small"><xsl:value-of select="."/></fo:block>
    </fo:table-cell>
</xsl:template>

<xsl:template match="tblCell">
    <fo:table-cell border-bottom-style="solid" border-bottom-width="1px" border-bottom-color="#E3E3E3" >
    <fo:block color="#7E7E7E" font-family="arial, helvetica, sans-serif" font-size="x-small"><xsl:value-of select="."/></fo:block>
    </fo:table-cell>
</xsl:template >

<xsl:template match="tblRow[position() > 1]">
    <fo:table-row>
    <xsl:apply-templates />
    </fo:table-row>
</xsl:template>

<xsl:template match="chart">
<fo:inline>
    <fo:instream-foreign-object xmlns:svg="http://www.w3.org/2000/svg" content-width="scale-to-fit" content-height="100%" width="100%" scaling="uniform">
    <svg:svg width="{svg:svg/@width}" height="{svg:svg/@height}">
    <xsl:copy-of select="svg:svg"/>
    </svg:svg>
    </fo:instream-foreign-object>
</fo:inline>
</xsl:template>

 </xsl:stylesheet>

我错过了什么?在表格单元格中显示图表是否有帮助(从而打破了不使用表格进行布局的惯例——太可怕了!)?我怎样才能让这些缩放并适合页面?

谢谢, 布兰特

【问题讨论】:

    标签: xslt svg xsl-fo apache-fop


    【解决方案1】:

    scale 属性使图表适合包含块。如果您没有为该块设置尺寸,格式化引擎将不知道要缩放到什么尺寸,因此它将使用 100%。 我不知道当您将多个图表放在一个块中时会发生什么。

    您可能需要将图表放在一个表格中,每列一张图表,以便您可以根据图表的数量设置列宽。

    【讨论】:

    • 当您说包含块时,您是在谈论 元素还是 元素?这个想法是在单个块元素中包含多个内联元素。我将封闭 的尺寸设置为 8.25 in x 6 in。但我想知道是否需要设置内联元素尺寸?
    • 我说的是&lt;block&gt; 元素。我不确定,但我认为您不能为&lt;inline&gt; 指定尺寸。您可以将&lt;inline&gt; 替换为另一个&lt;block&gt;。块元素可以嵌套。
    • 但是如果我将图表放在嵌套的块元素中,我该如何并排放置图表?兄弟块元素最终不会显示在彼此下方吗?
    • 如果是这种情况,请改用块容器。
    • 在将“高度”和“宽度”属性添加到包含 instream-foreign-object 的块中并且没有得到任何结果后,我将图像放在表格中并且它起作用了。不知道为什么之前的努力失败了,也许它与一个块中的多个图表有关?
    猜你喜欢
    • 2015-11-18
    • 2014-11-04
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2012-05-10
    • 2023-03-19
    相关资源
    最近更新 更多