有没有人有将XML转换为PDF的实际经验?

时间:2012-03-31 02:53:49

标签: xml xslt pdf

我有一个客户想要从XML数据集提供PDF文档的项目。我和XSLT合作多年,但从未创建过PDF。我使用XSLT生成了新的XML和HTML,但从来没有使用PDF。

2 个答案:

答案 0 :(得分:3)

您必须采用两步法,首先应用xsl-fo转换,创建特定的fo标签,然后通过fo到pdf流程运行。
下面是我用于此的一段VB代码,以及我的一小部分xsl-fo。

'Transform to create fo file
foName = "C:\test\foName_test.fo"
xslName = "C:\test\xhtml2fo.xslt"

Call Transform(flName, xslName, foName)

'Call FOP PDF generator
Set shell = CreateObject("WScript.Shell")

pdfName = "C:\test\foName_test.pdf"

cmd = "java -Dfop.home=C:\fop -cp C:\fop\build\fop.jar org.apache.fop.cli.Main -fo " & foName & " -pdf " & pdfName
Call shell.Run(cmd, vbWindowFrame, True)

Private Sub Transform(sourceFile, stylesheetFile, resultFile)
Dim source As New MSXML2.DOMDocument60
Dim stylesheet As New MSXML2.DOMDocument60
Dim result As New MSXML2.DOMDocument60
Dim objStream As New ADODB.Stream

Dim msg As String

objStream.Open
objStream.Position = 0
objStream.Charset = "utf-8"
objStream.LoadFromFile (sourceFile)

' Load data
source.async = False
source.loadXML (objStream.ReadText())

' Load style sheet
stylesheet.async = False
stylesheet.Load stylesheetFile

' Do the transform
source.transformNodeToObject stylesheet, result
result.Save resultFile    
End Sub

XSLT代码段:

<xsl:template name="make-layout-master-set">
<fo:layout-master-set>
  <fo:simple-page-master master-name="all-pages" xsl:use-attribute-sets="page">
    <fo:region-body margin-top="{$page-margin-top}" margin-right="{$page-margin-right}" margin-bottom="{$page-margin-bottom}" margin-left="{$page-margin-left}" column-count="{$column-count}" column-gap="{$column-gap}"/>
    <xsl:choose>
      <xsl:when test="$writing-mode = 'tb-rl'">
        <fo:region-before extent="{$page-margin-right}" precedence="true"/>
        <fo:region-after extent="{$page-margin-left}" precedence="true"/>
        <fo:region-start region-name="page-header" extent="{$page-margin-top}" writing-mode="lr-tb" display-align="before"/>
        <fo:region-end region-name="page-footer" extent="{$page-margin-bottom}" writing-mode="lr-tb" display-align="after"/>
      </xsl:when>
      <xsl:when test="$writing-mode = 'rl-tb'">
        <fo:region-before region-name="page-header" extent="{$page-margin-top}" display-align="before"/>
        <fo:region-after region-name="page-footer" extent="{$page-margin-bottom}" display-align="after"/>
        <fo:region-start extent="{$page-margin-right}"/>
        <fo:region-end extent="{$page-margin-left}"/>
      </xsl:when>
      <xsl:otherwise><!-- $writing-mode = 'lr-tb' -->
        <fo:region-before region-name="page-header" extent="{$page-margin-top}" display-align="before"/>
        <fo:region-after region-name="page-footer" extent="{$page-margin-bottom}" display-align="after"/>
        <fo:region-start extent="{$page-margin-left}"/>
        <fo:region-end extent="{$page-margin-bottom}"/>
      </xsl:otherwise>
    </xsl:choose>
  </fo:simple-page-master>
</fo:layout-master-set>
</xsl:template>

答案 1 :(得分:0)

您还可以使用TeX将XML转换为PDF。您可以使用ConTeXtXMLTeX。请参阅TeX.SX