Java 如何使用XSLT和Apache FOP在PDF文档中生成条形图
我正在寻找一种使用JavaApacheFop在XSLT中生成条形图的方法 我试图用下面的XML和XSLT生成条形图,但它生成的是空的.PDF文件,没有任何图表 data.xmlJava 如何使用XSLT和Apache FOP在PDF文档中生成条形图,java,xml,xslt,charts,apache-fop,Java,Xml,Xslt,Charts,Apache Fop,我正在寻找一种使用JavaApacheFop在XSLT中生成条形图的方法 我试图用下面的XML和XSLT生成条形图,但它生成的是空的.PDF文件,没有任何图表 data.xml <?xml version="1.0" encoding="UTF-8"?> <sales> <region> <title>Eastern Region Quarterly Sales (Second/'04)</title> <key1 ar
<?xml version="1.0" encoding="UTF-8"?>
<sales>
<region>
<title>Eastern Region Quarterly Sales (Second/'04)</title>
<key1 area="New York Area">.95</key1>
<key2 area="Virginia Area">.89</key2>
<key3 area="Maryland Area">.67</key3>
<key4 area="Connecticut Area">.65</key4>
<key5 area="Delaware Area">.45</key5>
</region>
</sales>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="sales">
<svg width="650" height="500">
<g id="axis" transform="translate(0 500) scale(1 -1)">
<line id="axis-y" x1="30" y1="20" x2="30" y2="450" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
<line id="axis-x" x1="30" y1="20" x2="460" y2="20" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
</g>
<xsl:apply-templates select="region" />
</svg>
</xsl:template>
<xsl:template match="region">
<g id="bars" transform="translate(30 479) scale(1 -430)">
<rect x="30" y="0" width="50" height="{key1}" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="100" y="0" width="50" height="{key2}" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="170" y="0" width="50" height="{key3}" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="240" y="0" width="50" height="{key4}" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="310" y="0" width="50" height="{key5}" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:0" />
</g>
<g id="scale" transform="translate(29 60)">
<text id="scale1" x="0px" y="320px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$25K</text>
<text id="scale2" x="0px" y="215px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$50K</text>
<text id="scale3" x="0px" y="107.5px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$75K</text>
<text id="scale4" x="0px" y="0px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$100K</text>
</g>
<g id="key">
<rect id="key1" x="430" y="80" width="25" height="15" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key2" x="430" y="100" width="25" height="15" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key3" x="430" y="120" width="25" height="15" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key5" x="430" y="140" width="25" height="15" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key4" x="430" y="160" width="25" height="15" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1" />
</g>
<text id="key1-text" x="465px" y="92px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key1/@area" />
</text>
<text id="key2-text" x="465px" y="112px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key2/@area" />
</text>
<text id="key3-text" x="465px" y="132px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key3/@area" />
</text>
<text id="key4-text" x="465px" y="152px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key4/@area" />
</text>
<text id="key5-text" x="465px" y="172px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key5/@area" />
</text>
<g id="title">
<text x="325px" y="20px" style="text-anchor:middle;fill:rgb(0,0,0);font-size:24;font-family:Arial">
<xsl:value-of select="title" />
</text>
</g>
</xsl:template>
</xsl:stylesheet>
东部地区季度销售额(2004年第二季度)
.95
.89
.67
.65
.45
bar.xsl
<?xml version="1.0" encoding="UTF-8"?>
<sales>
<region>
<title>Eastern Region Quarterly Sales (Second/'04)</title>
<key1 area="New York Area">.95</key1>
<key2 area="Virginia Area">.89</key2>
<key3 area="Maryland Area">.67</key3>
<key4 area="Connecticut Area">.65</key4>
<key5 area="Delaware Area">.45</key5>
</region>
</sales>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="sales">
<svg width="650" height="500">
<g id="axis" transform="translate(0 500) scale(1 -1)">
<line id="axis-y" x1="30" y1="20" x2="30" y2="450" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
<line id="axis-x" x1="30" y1="20" x2="460" y2="20" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
</g>
<xsl:apply-templates select="region" />
</svg>
</xsl:template>
<xsl:template match="region">
<g id="bars" transform="translate(30 479) scale(1 -430)">
<rect x="30" y="0" width="50" height="{key1}" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="100" y="0" width="50" height="{key2}" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="170" y="0" width="50" height="{key3}" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="240" y="0" width="50" height="{key4}" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="310" y="0" width="50" height="{key5}" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:0" />
</g>
<g id="scale" transform="translate(29 60)">
<text id="scale1" x="0px" y="320px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$25K</text>
<text id="scale2" x="0px" y="215px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$50K</text>
<text id="scale3" x="0px" y="107.5px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$75K</text>
<text id="scale4" x="0px" y="0px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$100K</text>
</g>
<g id="key">
<rect id="key1" x="430" y="80" width="25" height="15" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key2" x="430" y="100" width="25" height="15" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key3" x="430" y="120" width="25" height="15" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key5" x="430" y="140" width="25" height="15" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key4" x="430" y="160" width="25" height="15" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1" />
</g>
<text id="key1-text" x="465px" y="92px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key1/@area" />
</text>
<text id="key2-text" x="465px" y="112px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key2/@area" />
</text>
<text id="key3-text" x="465px" y="132px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key3/@area" />
</text>
<text id="key4-text" x="465px" y="152px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key4/@area" />
</text>
<text id="key5-text" x="465px" y="172px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key5/@area" />
</text>
<g id="title">
<text x="325px" y="20px" style="text-anchor:middle;fill:rgb(0,0,0);font-size:24;font-family:Arial">
<xsl:value-of select="title" />
</text>
</g>
</xsl:template>
</xsl:stylesheet>
2.5万美元
5万美元
75000美元
10万美元
我不知道为什么我得到的是空的pdf,我还试图生成svg文件-它也没有显示任何图形。非常感谢您的帮助。您可以使用创建SVG并将其嵌入XSL-FO中 因为您想要使用XSLT和FOP,所以我假设您有XML格式的输入数据。(如果不是,可能XSLT和FOP不是适合这项工作的工具?) 因此XSLT将从XML输入创建XSL-FO和嵌入式SVG。FOP将从XSL-FO创建PDF 例如 XML输入
<list>
<fruit>
<type>Apples</type>
<qty>5</qty>
</fruit>
<fruit>
<type>Oranges</type>
<qty>2</qty>
</fruit>
<fruit>
<type>Bananas</type>
<qty>7</qty>
</fruit>
<fruit>
<type>Peaches</type>
<qty>9</qty>
</fruit>
</list>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>Example embedded SVG:</fo:block>
<fo:block space-before="4pt">
<fo:instream-foreign-object>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="420" height="150">
<title id="title">List of Fruit</title>
<g>
<rect width="50" height="20" y="0"/>
<text x="100" y="10" dy=".35em">5 Apples</text>
</g>
<g>
<rect width="70" height="20" y="25"/>
<text x="100" y="35" dy=".35em">7 Bananas</text>
</g>
<g>
<rect width="20" height="20" y="50"/>
<text x="100" y="60" dy=".35em">2 Oranges</text>
</g>
<g>
<rect width="90" height="20" y="75"/>
<text x="100" y="85" dy=".35em">9 Peaches</text>
</g>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
苹果
5.
橘子
2.
香蕉
7.
桃子
9
XSLT1.0
<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 indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="list">
<fo:block>Example embedded SVG:</fo:block>
<fo:block space-before="4pt">
<fo:instream-foreign-object>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="420" height="150">
<title id="title">List of Fruit</title>
<xsl:apply-templates select="fruit">
<xsl:sort select="type" order="ascending" data-type="text"/>
</xsl:apply-templates>
</svg>
</fo:instream-foreign-object>
</fo:block>
</xsl:template>
<xsl:template match="fruit">
<xsl:variable name="y" select="((position() - 1) * 20) + (position() * 5 - 5)"/>
<g xmlns="http://www.w3.org/2000/svg">
<rect width="{qty * 10}" height="20" y="{$y}"/>
<text x="100" y="{$y + 10}" dy=".35em">
<xsl:value-of select="concat(qty,' ',type)"/>
</text>
</g>
</xsl:template>
</xsl:stylesheet>
嵌入式SVG示例:
水果清单
XSL-FO输出
<list>
<fruit>
<type>Apples</type>
<qty>5</qty>
</fruit>
<fruit>
<type>Oranges</type>
<qty>2</qty>
</fruit>
<fruit>
<type>Bananas</type>
<qty>7</qty>
</fruit>
<fruit>
<type>Peaches</type>
<qty>9</qty>
</fruit>
</list>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>Example embedded SVG:</fo:block>
<fo:block space-before="4pt">
<fo:instream-foreign-object>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="420" height="150">
<title id="title">List of Fruit</title>
<g>
<rect width="50" height="20" y="0"/>
<text x="100" y="10" dy=".35em">5 Apples</text>
</g>
<g>
<rect width="70" height="20" y="25"/>
<text x="100" y="35" dy=".35em">7 Bananas</text>
</g>
<g>
<rect width="20" height="20" y="50"/>
<text x="100" y="60" dy=".35em">2 Oranges</text>
</g>
<g>
<rect width="90" height="20" y="75"/>
<text x="100" y="85" dy=".35em">9 Peaches</text>
</g>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
嵌入式SVG示例:
水果清单
5个苹果
7个香蕉
2个橙子
9个桃子
呈现的PDF将是一个8.5英寸x 11英寸的页面,包含以下内容:
注:
- 我使用Saxon HE进行XSLT转换。Saxon是一个XSLT 3.0处理器,因此如果您决定使用它,就不会受到XSLT 1.0的限制。我只使用XSLT1.0,因为这是一个非常基本的示例
- 我使用FOP2.1版(包含在OxygenXML编辑器中)处理XSL-FO以创建PDF输出
根据修改后的问题更新了XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:instream-foreign-object>
<xsl:apply-templates/>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="sales">
<svg width="650" height="500">
<g id="axis" transform="translate(0 500) scale(1 -1)">
<line id="axis-y" x1="30" y1="20" x2="30" y2="450" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
<line id="axis-x" x1="30" y1="20" x2="460" y2="20" style="fill:none;stroke:rgb(0,0,0);stroke-width:2" />
</g>
<xsl:apply-templates select="region" />
</svg>
</xsl:template>
<xsl:template match="region">
<g id="bars" transform="translate(30 479) scale(1 -430)">
<rect x="30" y="0" width="50" height="{key1}" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="100" y="0" width="50" height="{key2}" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="170" y="0" width="50" height="{key3}" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="240" y="0" width="50" height="{key4}" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:0" />
<rect x="310" y="0" width="50" height="{key5}" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:0" />
</g>
<g id="scale" transform="translate(29 60)">
<text id="scale1" x="0px" y="320px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$25K</text>
<text id="scale2" x="0px" y="215px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$50K</text>
<text id="scale3" x="0px" y="107.5px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$75K</text>
<text id="scale4" x="0px" y="0px" style="text-anchor:end;fill:rgb(0,0,0);font-size:10;font-family:Arial">$100K</text>
</g>
<g id="key">
<rect id="key1" x="430" y="80" width="25" height="15" style="fill:rgb(255,0,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key2" x="430" y="100" width="25" height="15" style="fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key3" x="430" y="120" width="25" height="15" style="fill:rgb(255,255,0);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key5" x="430" y="140" width="25" height="15" style="fill:rgb(0,255,255);stroke:rgb(0,0,0);stroke-width:1" />
<rect id="key4" x="430" y="160" width="25" height="15" style="fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1" />
</g>
<text id="key1-text" x="465px" y="92px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key1/@area" />
</text>
<text id="key2-text" x="465px" y="112px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key2/@area" />
</text>
<text id="key3-text" x="465px" y="132px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key3/@area" />
</text>
<text id="key4-text" x="465px" y="152px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key4/@area" />
</text>
<text id="key5-text" x="465px" y="172px" style="fill:rgb(0,0,0);font-size:18;font-family:Arial">
<xsl:value-of select="key5/@area" />
</text>
<g id="title">
<text x="325px" y="20px" style="text-anchor:middle;fill:rgb(0,0,0);font-size:24;font-family:Arial">
<xsl:value-of select="title" />
</text>
</g>
</xsl:template>
</xsl:stylesheet>
2.5万美元
5万美元
75000美元
10万美元
PDF输出
<list>
<fruit>
<type>Apples</type>
<qty>5</qty>
</fruit>
<fruit>
<type>Oranges</type>
<qty>2</qty>
</fruit>
<fruit>
<type>Bananas</type>
<qty>7</qty>
</fruit>
<fruit>
<type>Peaches</type>
<qty>9</qty>
</fruit>
</list>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>Example embedded SVG:</fo:block>
<fo:block space-before="4pt">
<fo:instream-foreign-object>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="420" height="150">
<title id="title">List of Fruit</title>
<g>
<rect width="50" height="20" y="0"/>
<text x="100" y="10" dy=".35em">5 Apples</text>
</g>
<g>
<rect width="70" height="20" y="25"/>
<text x="100" y="35" dy=".35em">7 Bananas</text>
</g>
<g>
<rect width="20" height="20" y="50"/>
<text x="100" y="60" dy=".35em">2 Oranges</text>
</g>
<g>
<rect width="90" height="20" y="75"/>
<text x="100" y="85" dy=".35em">9 Peaches</text>
</g>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
您可以使用
FO:instream-foreign-object
在XSL-FO中创建并嵌入SVG。这里有一个例子:这并不是您想要做的,因为在这个答案中SVG已经存在。您必须在XSLT中创建SVG,以便在fo:instream foreign object
中使用。感谢您的回复!如何使用xml或xslt创建SVG图表?Daniel Haley的建议很好。在XSLT中创建条形图条形图的坐标,将其输出为SVG,然后在ApacheFop中的fo:instream外来对象中使用它们,以将它们嵌入到PDF中。这听起来像是一个很好的小练习,如果你在这里发布一个工作示例,我会投赞成票。@zx485-在我的答案中添加了一个工作示例。:-)@丹尼尔·海利:看起来不错,所以我把它投了更高的票:-)谢谢,丹尼尔我试过你的例子——它很有效。我尝试了另一个条形图的例子,但是我得到了空的pdf。我已经用示例XML和XSLT更新了原始文章。你能告诉我遗漏了什么吗?@learngroovy-我会认为你的PDF是空的,因为你没有创建任何XSL-FO(除非你试图让FOP仅仅从SVG创建PDF,我不确定这是否可行(但我也不经常使用FOP))。为了使样式表正常工作,我添加了一个模板来输出XSL-FO(match=“/”
),还添加了svg名称空间(xmlns=)http://www.w3.org/2000/svg“
)到xsl:stylesheet。请看我的编辑。嗨,丹尼尔-我喜欢你的例子,我有类似的要求-如何用动态数据创建条形图和折线图?我看的是这样的图表,如果您可以重用相同的示例以动态方式填充数据并生成折线图,那将非常感激。谢谢。@mark-我的第一个示例是相当动态的,因为大多数值都是生成的。例如,我可以添加更多的水果
,而不必对样式表进行任何更改。我建议您手动创建一个示例svg图表,然后找出如何使用输入xml在样式表中生成该svg。