Linux 软件 - 使用 xsl 将 xml 转换为 pdf
我有两个文件。
我想用这两个文件创建pdf。
- 有人知道 Linux 上可以做到这一点的程序吗?
- 命令是什么?
第一个是 xhtml/xml 文件:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<table border="1">
<tr>
<td>Day/Time</td>
<td>7.30</td>
<td>8.15</td>
<td>9.05</td>
<td>9.50</td>
<td>10.40</td>
<td>11.25</td>
<td>12.15</td>
<td>13.00</td>
<td>13.50</td>
<td>14.35</td>
<td>15.25</td>
<td>16.10</td>
<td>17.00</td>
<td>17.45</td>
<td>18.35</td>
</tr>
<tr>
<td>Monday</td>
<td colspan="4"></td>
<td colspan="2">UvIn 134 1.P</td>
<td colspan="3">Mult 134 3.P -> 137</td>
<td colspan="6"></td>
</tr>
<tr>
<td>Tuesday</td>
<td colspan="2">InTe 135 1.P</td>
<td></td>
<td colspan="3">MaAn 336 1.P</td>
<td></td>
<td colspan="2">AlSu 134 1.P</td>
<td colspan="2"></td>
<td colspan="2">AlSu 135 1.P</td>
<td colspan="2"></td>
</tr>
<tr>
<td>Wednesday</td>
<td colspan="2"></td>
<td colspan="3">Kart 134 2.PV</td>
<td colspan="2">INTE 041 2.PV</td>
<td></td>
<td colspan="2">Aj 139 1.P</td>
<td colspan="5"></td>
</tr>
<tr>
<td>Thursday</td>
<td colspan="2"></td>
<td colspan="2">GeIn 139 2.PV</td>
<td colspan="3">SePr 135 1.P</td>
<td colspan="8"></td>
</tr>
<tr>
<td>Friday</td>
<td colspan="15"></td>
</tr>
</table>
</body>
</html>
第二个是 .xsl 文件 - 组合 xslt+xsl-fo:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:a="http://www.w3.org/1999/xhtml">
<xsl:template match="a:html">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="a4">
<fo:region-body padding-top="1in" padding-left="1.5mm" background-color="#222"/>
</fo:simple-page-master>
</fo:layout-master-set>
<xsl:apply-templates/>
</fo:root>
</xsl:template>
<xsl:template match="a:body">
<fo:page-sequence master-reference="a4">
<fo:flow flow-name="xsl-region-body">
<fo:table>
<fo:table-body background-color="#333">
<xsl:for-each select="a:table/a:tr">
<fo:table-row>
<xsl:for-each select="a:td">
<fo:table-cell padding="0.5mm" border-width="2mm" border-style="outset" border-color="#bbb" color="#aaff00" font-weight="bold" font-family="arial" text-align="center">
<xsl:if test="@colspan"><xsl:attribute name="number-columns-spanned"><xsl:value-of select="@colspan"/></xsl:attribute></xsl:if>
<xsl:value-of select="."/>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</xsl:template>
</xsl:stylesheet>
I have two files.
I want to create pdf with these two files.
- does anyone know program on linux that can do that?
- whats the command?
The first one is xhtml/xml file:
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<table border="1">
<tr>
<td>Day/Time</td>
<td>7.30</td>
<td>8.15</td>
<td>9.05</td>
<td>9.50</td>
<td>10.40</td>
<td>11.25</td>
<td>12.15</td>
<td>13.00</td>
<td>13.50</td>
<td>14.35</td>
<td>15.25</td>
<td>16.10</td>
<td>17.00</td>
<td>17.45</td>
<td>18.35</td>
</tr>
<tr>
<td>Monday</td>
<td colspan="4"></td>
<td colspan="2">UvIn 134 1.P</td>
<td colspan="3">Mult 134 3.P -> 137</td>
<td colspan="6"></td>
</tr>
<tr>
<td>Tuesday</td>
<td colspan="2">InTe 135 1.P</td>
<td></td>
<td colspan="3">MaAn 336 1.P</td>
<td></td>
<td colspan="2">AlSu 134 1.P</td>
<td colspan="2"></td>
<td colspan="2">AlSu 135 1.P</td>
<td colspan="2"></td>
</tr>
<tr>
<td>Wednesday</td>
<td colspan="2"></td>
<td colspan="3">Kart 134 2.PV</td>
<td colspan="2">INTE 041 2.PV</td>
<td></td>
<td colspan="2">Aj 139 1.P</td>
<td colspan="5"></td>
</tr>
<tr>
<td>Thursday</td>
<td colspan="2"></td>
<td colspan="2">GeIn 139 2.PV</td>
<td colspan="3">SePr 135 1.P</td>
<td colspan="8"></td>
</tr>
<tr>
<td>Friday</td>
<td colspan="15"></td>
</tr>
</table>
</body>
</html>
and the second one is .xsl file - combined xslt+xsl-fo:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:a="http://www.w3.org/1999/xhtml">
<xsl:template match="a:html">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="a4">
<fo:region-body padding-top="1in" padding-left="1.5mm" background-color="#222"/>
</fo:simple-page-master>
</fo:layout-master-set>
<xsl:apply-templates/>
</fo:root>
</xsl:template>
<xsl:template match="a:body">
<fo:page-sequence master-reference="a4">
<fo:flow flow-name="xsl-region-body">
<fo:table>
<fo:table-body background-color="#333">
<xsl:for-each select="a:table/a:tr">
<fo:table-row>
<xsl:for-each select="a:td">
<fo:table-cell padding="0.5mm" border-width="2mm" border-style="outset" border-color="#bbb" color="#aaff00" font-weight="bold" font-family="arial" text-align="center">
<xsl:if test="@colspan"><xsl:attribute name="number-columns-spanned"><xsl:value-of select="@colspan"/></xsl:attribute></xsl:if>
<xsl:value-of select="."/>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个 XSLT 处理器,
xsltproc
可能已经在您的 Linux 发行版中。然后您需要一个处理器将 FO(格式化对象)转换为 PDF。 Apache 有一个免费的 FO 处理器 (FOP):Apache™ FOP:下载发行版一次如果您下载并提取了 FOP,您的管道可能如下所示:
我已尝试使用您提供的 XML 源和 XSLT,但运行 Apache FOP 时出现错误。我对您的 XSLT 一无所知,因此您也许能够避免这些错误。
You'll need an XSLT processor,
xsltproc
is probably already in your Linux distribution. Then you'll need a processor to convert the FO (Formatting Objects) to a PDF. Apache has a free FO processor (FOP): Apache™ FOP: Downloading A DistributionOnce you have a FOP downloaded and extracted, your pipeline might look something like this:
I've tried as much with your provided XML source and XSLT, and there were errors when running Apache FOP. I don't know anything about your XSLT, so you might be able to get around the errors.
xsltproc 可以应用样式表。 Apache FOP 可以生成 PDF。
xsltproc can apply the stylesheet. Apache FOP can generate the PDF.