跳轉到內容

XQuery/XSL-FO SVG

來自華夏公益教科書

您想將 XML 轉換為 PDF 並將 SVG 影像嵌入到輸出中。

eXist 從版本 2 開始支援將 svg 放置在 fop 渲染的 pdf 中,唯一的步驟是在構建原始碼之前啟用 XSLFO 模組。

echo "include.module.xslfo = true" > $EXIST_HOME/extensions/local.build.properties

注意:在無頭 *nix 系統上,確保在啟動 eXist-db 時沒有設定 DISPLAY 環境變數,否則 apache batik svg 渲染器可能會丟擲異常。

XQuery 示例

[編輯 | 編輯原始碼]

xquery 亮點:FO 是從 xsl 轉換建立的:我知道,這可以用 xquery 完成,但我可以在其他地方重用 xsl。文件是透過搜尋找到的。xsl 樣式表位於“views”中,就像 pdf.xq 一樣。我建立了很多變數,但這樣更好。檔名從請求中推斷出來。可能存在錯誤,因為我剝離了工作程式碼!由於我對 xquery 功能的不瞭解,它可能過於複雜。我認為你會找到自己的方式。應用程式目錄的佈局如 XRX wiki 中建議的那樣。

let $host := "https://:8080/exist/rest/"
let $home := "/db/apps/myApp"
let $match := collection(concat($home, "/data"))//item[@uuid=$uuid]
let $coll := util:collection-name($match)
let $file := util:document-name($match)
let $xsls := concat($home, "/views/foPDF.xsl")
let $rand := concat("?", util:random())

let $params :=
 <parameters>
   <param name="svgfile" value="{$host}{$home}{$file}.svg"/>
   <param name="rand" value="{$rand}"/>
 </parameters>

let $tmp := transform:transform(doc(concat($coll, "/", $file)), doc($xsls), $params)
let $pdf := xslfo:render($tmp, "application/pdf", (), ())

(: substring-afer-last is in functx :)
let $fname := substring-after(request:get-path-info(), "/")
return response:stream-binary($pdf, "application/pdf", $fname)

另一種可能更高效能的方式是透過 xmldb:exist 限定符獲取 svg 檔案

let $host := "xmldb:exist://"

XSL 亮點

[編輯 | 編輯原始碼]
<!-- passed in parameter, a fully qualified URI to the SVG file, eg:
    "https://:8080/exist/rest//db/apps/myApp/data/Some.svg"
    or in exist-db "xmldb:exist///db/apps/myApp/data/Some.svg"
-->
<xsl:param name="svgfile" select="''"/>

<!-- passed in parameter, value eg: "?123", changes with every call
    used to force fop to reread the svg file each time
    starts with ? to be discarded in the end...
    can be left blank to have fop cache the svgfile
-->
<xsl:param name="rand" select="''"/>


<!-- place SVG in PDF output -->
<fo:block-container>
       <fo:block>
               <fo:external-graphic src="{$svgfile}{$rand}"/>
       </fo:block>
</fo:block-container>
華夏公益教科書