Skip to content

Instantly share code, notes, and snippets.

@br3nt
Last active November 13, 2018 00:23
Show Gist options
  • Select an option

  • Save br3nt/67d3097278d1f1423ea5e9a3759d64ed to your computer and use it in GitHub Desktop.

Select an option

Save br3nt/67d3097278d1f1423ea5e9a3759d64ed to your computer and use it in GitHub Desktop.
Letterhead XSL:FO example
<?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:hsqueue="xalan://com.helpers.xsl.MessageQueue"
xmlns:hshtml="xalan://com.helpers.xsl.HTML2FO">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="correspondence" select="document(concat('/correspondence/', /request/id, '.xml'))"/>
<xsl:variable name="letter" select="document(concat('/patients/', /request/patient_id, '/letters/', /request/letter_id, '.xml'))"/>
<!-- sets up the overall page sequences, dimensions, and layout -->
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<!-- defines the letterhead page (first page) -->
<fo:simple-page-master
master-name="letterhead"
page-height="297mm"
page-width="210mm"
margin-top="10mm"
margin-bottom="10mm"
margin-left="10mm"
margin-right="10mm">
<!-- define which document regions are used in this page template -->
<fo:region-body region-name="region-body" margin-top="50mm" margin-bottom="30mm"/>
<fo:region-before region-name="region-header" extent="40mm"/>
<fo:region-after region-name="region-footer" extent="15mm"/>
</fo:simple-page-master>
<!-- defines the blank page (all pages after the letterhead) -->
<fo:simple-page-master
master-name="blank-page"
page-height="297mm"
page-width="210mm"
margin-top="10mm"
margin-bottom="10mm"
margin-left="10mm"
margin-right="10mm">
<!-- define which document regions are used in this page template -->
<fo:region-body region-name="region-body"/>
</fo:simple-page-master>
<!-- define the sequence of pages templates -->
<fo:page-sequence-master master-name="letterhead-sequence">
<fo:repeatable-page-master-alternatives>
<!-- the first page uses the letterhead template -->
<fo:conditional-page-master-reference page-position="first" master-reference="letterhead"/>
<!-- all subsequent pages use the blank page template -->
<fo:conditional-page-master-reference master-reference="blank-page"/>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="letterhead-sequence">
<!-- static content for the header -->
<fo:static-content flow-name="region-header">
<fo:block>
<xsl:call-template name="layout-header"/>
</fo:block>
</fo:static-content>
<!-- static content for the footer -->
<fo:static-content flow-name="region-footer">
<fo:block>
<xsl:call-template name="layout-footer"/>
</fo:block>
</fo:static-content>
<!-- dynamic for the body -->
<fo:flow flow-name="region-body">
<fo:block>
<xsl:call-template name="layout-address"/>
<xsl:call-template name="layout-body"/>
<xsl:call-template name="layout-cc"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- TEMPLATES -->
<xsl:template name="layout-body">
<fo:block>
<xsl:variable name="detail" select="$letter/letter/letter-text"/>
<xsl:copy-of select="hshtml:html2fo($detail, 0.60)"/>
</fo:block>
</xsl:template>
<xsl:template name="layout-header">
<fo:block text-align="right" width="79mm">
<fo:external-graphic src="system_images/SCFLogo" content-width="79mm" />
</fo:block>
</xsl:template>
<xsl:template name="layout-footer">
<fo:block text-align="center" width="190mm">
<!--Add a html source link to like a footer graphic-->
<fo:external-graphic src="system_images/SCFFooter" />
</fo:block>
</xsl:template>
<xsl:template name="layout-cc">
<fo:block>
<xsl:variable name="cc" select="$letter/letter/letter-cc"/>
<xsl:copy-of select="hshtml:html2fo($cc, 0.60)"/>
</fo:block>
</xsl:template>
<xsl:template name="layout-address">
<fo:block text-align="left" margin-left="25mm">
<xsl:variable name="add-to" select="$correspondence/correspondence/letter-name"/>
<xsl:copy-of select="hshtml:html2fo($add-to, 0.60)"/>
</fo:block>
<fo:block text-align="left" margin-left="25mm" margin-bottom="10mm">
<xsl:variable name="address" select="$correspondence/correspondence/letter-address"/>
<xsl:copy-of select="hshtml:html2fo($address, 0.60)"/>
</fo:block>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment