Saturday, January 29, 2011

PDF Generation in XQuery

The steps required to generate a PDF document are:

* retrieve or compute the base XML document
This Means we can refer any xml document which serves data to our pdf report.Compute means some time we need to format the output as a users need(ex:I need to generate letter with the data from a db).

* transform to XSL-FO, perhaps using XSL
For the case am using eXistdb i must enable the XSL-FO module for this transformation.
First of all we write XSL file for our output.This XSL is responsible for output of the pdf document.

Here we compute the XML for pdf report


Here we get the XSL document

$bid := 'myfilename' ---> Usually it comes from db
let $filename := concat('letter-',$bid) ---->Creating Letter name using variable data
let $xslfilepath := concat('/vserve/xsl/','inst_req','.xsl') ----> get the xsl document
let $xslfile := concat('http://localhost:8080/exist/rest/db/test/',$xslfilepath)

This params parameter is optional one

let $params :=



Here we declare the output method

let $serialization-options := 'method=xml media-type=text/xml omit-xml-declaration=yes indent=no'


* transform the XSL-FO to PDF using Apache FOP

This transform function used for transform XSL file + XML into XSL-FO file


let $fo := transform:transform($form, $xslfile, $params, $serialization-options)

Here we using render() - built-in function to convert XSL-FO into PDF

let $pdf := xslfo:render($fo, 'application/pdf', ())

This file can be saved directly to the XML file system. It will be stored as a non-searchable binary.
You can then view this directly by providing a link to the file or you can send it directly to the browser by using the response:stream-binary() function as follows:
let $pdf1 := response:stream-binary($pdf, 'application/pdf', concat($filename,'.pdf'))

No comments:

Post a Comment