Since I had this laying around, I thought I would post as a CoolTool since it is somewhat related to a recent question asked.
In some environments, we have customers who are doing print production of folded booklets and provide backgrounds for those booklets that normally contain two pages.
In a bookfold print, you typically are combining two pages into into one as well as also duplexing the pages for that the resulting document can be folded, cut and produced.
For example, a four page book which would normally be page 1, page 2, page 3, page 4 is actually output as:
Page 4 | Page 1, Page 2 | Page 3
Meaning, pages 4 and 1 are combined on page 1 in that order and pages 2 and 3 are combined on page 2 in that order.
When duplex printed (flip the second page and print on back of the first) and then fold this, you have a booklet with page 1 on the font, page 2 on the back of page 1, page 3 facing and page 4 on the back.
And most likely, the customer will provide images that bleed into the cut area for the document (hence are larger).
So, below and attached is a sample XSL “bookfolder”. It does both bookfolding the pages as well as injection of images into the bookfolded pages. It is pretty self explanatory but if you have any questions, feel free to post them and I would be happy to answer. This is usually applied to a larger document, one that has many (like 50, 100 documents) in a single file. It operates in the XEP Intermediate format, reorganizing the pages and injecting backgrounds.
The parameters that control it are:
<xsl:param name="pageGroupSize" select="8"/> <!—How many pages are in each document to fold -->
<xsl:param name="inject-images" select="'true'"/><!—Do you have images to inject -->
<xsl:param name="image-name" select="'D:/WIP/AlphaGraphics/Partners OE/Production/a-'"/><!—Path to the injectable images, assuming they are split like a-1.pdf, a-2.pdf, a-3.pdf, a-4.pdf for an eight page booklet-->
<xsl:param name="image-width" select="1296000"/><!—The width of the image in xep units-->
<xsl:param name="image-height" select="864000"/><!—The height of the image in xep units-->
<xsl:param name="image-ext" select="'pdf'"/><!—The file extension of the image file as you could inject PNG or JPG or … but typically you are inserting PDF inside PDF-->
Running this XSL against n XEPOUT file can reorganize the pages for duplex printed output of a booklet, combining two pages into one in the proper order as well as inject full size, two page background images into the output with crop marks set on that the input document.
Kevin Brown
(650) 327-1000 Direct
(650) 328-8008 Fax
(925) 395-1772 Mobile
skype:kbrown01
kevin@renderx.com
sales@renderx.com
****************
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xep="http://www.renderx.com/XEP/xep" version="1.0">
<xsl:key name="keyPageID" match="xep:page" use="@page-id"/>
<xsl:key name="keyPageNumber" match="xep:page" use="@page-number"/>
<xsl:param name="pageGroupSize" select="8"/>
<xsl:param name="inject-images" select="'true'"/>
<xsl:param name="image-name" select="'D:/WIP/AlphaGraphics/Partners OE/Production/a-'"/>
<xsl:param name="image-width" select="1296000"/>
<xsl:param name="image-height" select="864000"/>
<xsl:param name="image-ext" select="'pdf'"/>
<xsl:variable name="require-blank">
<xsl:choose>
<xsl:when test="($pageGroupSize mod 2) = 0">
<xsl:text>false</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>true</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="cropvalues">
<xsl:value-of select="//processing-instruction('xep-pdf-crop-offset')"/>
</xsl:variable>
<xsl:variable name="croptop">
<xsl:call-template name="splitCrop">
<xsl:with-param name="cropValue" select="$cropvalues"/>
<xsl:with-param name="position" select="1"/>
<xsl:with-param name="loop" select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cropbottom">
<xsl:call-template name="splitCrop">
<xsl:with-param name="cropValue" select="$cropvalues"/>
<xsl:with-param name="position" select="2"/>
<xsl:with-param name="loop" select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cropleft">
<xsl:call-template name="splitCrop">
<xsl:with-param name="cropValue" select="$cropvalues"/>
<xsl:with-param name="position" select="3"/>
<xsl:with-param name="loop" select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="cropright">
<xsl:call-template name="splitCrop">
<xsl:with-param name="cropValue" select="$cropvalues"/>
<xsl:with-param name="position" select="4"/>
<xsl:with-param name="loop" select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="groupPages">
<xsl:choose>
<xsl:when test="($pageGroupSize mod 2) = 0">
<xsl:value-of select="$pageGroupSize"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pageGroupSize + 1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="pageCount" select="count(//xep:page)"/>
<xsl:variable name="groupCount" select="$pageCount div $groupPages"/>
<xsl:template match="xep:document">
<xsl:copy>
<xsl:apply-templates select="@*" mode="identity-copy"/>
<xsl:apply-templates select="xep:internal-bookmark"/>
<xsl:call-template name="bookfold">
<xsl:with-param name="currentGroup" select="1"/>
<xsl:with-param name="currentPage" select="1"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="bookfold">
<xsl:param name="currentGroup"/>
<xsl:param name="currentPage"/>
<xsl:message>
<xsl:text>Processing Group </xsl:text>
<xsl:value-of select="$currentGroup"/>
</xsl:message>
<xsl:call-template name="output-pages">
<xsl:with-param name="currentGroup" select="$currentGroup"/>
<xsl:with-param name="currentPage" select="$currentPage"/>
<xsl:with-param name="loop" select="1"/>
</xsl:call-template>
<xsl:if test="$currentGroup < $groupCount">
<xsl:call-template name="bookfold">
<xsl:with-param name="currentGroup" select="$currentGroup + 1"/>
<xsl:with-param name="currentPage" select="$currentPage + $groupPages"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="output-pages">
<xsl:param name="currentGroup"/>
<xsl:param name="currentPage"/>
<xsl:param name="loop"/>
<xsl:variable name="otherPage" select="$currentPage + $groupPages - (2 * $loop) + 1"/>
<xep:page>
<xsl:choose>
<xsl:when test="($currentPage mod 2) = 0">
<xsl:message>
<xsl:text>Output page </xsl:text>
<xsl:value-of select="$currentPage"/>
<xsl:text> and page </xsl:text>
<xsl:value-of select="$otherPage"/>
</xsl:message>
<xsl:call-template name="build-page">
<xsl:with-param name="leftpart" select="//xep:page[$currentPage]"/>
<xsl:with-param name="rightpart" select="//xep:page[$otherPage]"/>
<xsl:with-param name="loop" select="$loop"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Output page </xsl:text>
<xsl:value-of select="$otherPage"/>
<xsl:text> and page </xsl:text>
<xsl:value-of select="$currentPage"/>
</xsl:message>
<xsl:call-template name="build-page">
<xsl:with-param name="leftpart" select="//xep:page[$otherPage]"/>
<xsl:with-param name="rightpart" select="//xep:page[$currentPage]"/>
<xsl:with-param name="loop" select="$loop"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xep:page>
<xsl:if test="$loop < ($groupPages div 2)">
<xsl:call-template name="output-pages">
<xsl:with-param name="currentPage" select="$currentPage + 1"/>
<xsl:with-param name="loop" select="$loop + 1"/>
<xsl:with-param name="currentGroup" select="$currentGroup"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="build-page">
<xsl:param name="leftpart"/>
<xsl:param name="rightpart"/>
<xsl:param name="loop"/>
<xsl:attribute name="width">
<xsl:value-of select="$leftpart/@width + $rightpart/@width + $cropleft + $cropright"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="$leftpart/@height + $croptop + $cropbottom"/>
</xsl:attribute>
<xsl:attribute name="page-number">
<xsl:value-of select="$leftpart/@page-number"/>
</xsl:attribute>
<xsl:attribute name="page-id">
<xsl:value-of select="$leftpart/@page-id"/>
</xsl:attribute>
<!-- Hack for only one background -->
<xsl:if test="$inject-images = 'true'">
<xep:image x-from="0" y-from="0" scale-x="1.0" scale-y="1.0"
width="{$image-width}" height="{$image-height}">
<xsl:attribute name="src">
<xsl:text>file:///</xsl:text>
<xsl:value-of select="$image-name"/>
<xsl:value-of select="$loop"/>
<xsl:text>.</xsl:text>
<xsl:value-of select="$image-ext"/>
</xsl:attribute>
<xsl:attribute name="type">
<xsl:choose>
<xsl:when test="$image-ext = 'pdf'">
<xsl:value-of select="'application/pdf'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'image/png'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xep:image>
</xsl:if>
<xep:translate x="{$cropleft}" y="{$cropbottom}"/>
<xsl:copy-of select="$leftpart/*"/>
<xep:translate x="{$leftpart/@width}" y="0"/>
<xsl:copy-of select="$rightpart/*"/>
</xsl:template>
<xsl:template match="xep:internal-bookmark">
<xep:internal-bookmark>
<xsl:apply-templates select="@*" mode="identity-copy"/>
</xep:internal-bookmark>
</xsl:template>
<!-- identity copy rules -->
<xsl:template match="node() | @*" mode="identity-copy">
<xsl:copy>
<xsl:apply-templates select="@*" mode="identity-copy"/>
<xsl:apply-templates select="node()" mode="identity-copy"/>
</xsl:copy>
</xsl:template>
<xsl:template name="splitCrop">
<xsl:param name="cropValue"/>
<xsl:param name="position"/>
<xsl:param name="loop"/>
<xsl:choose>
<xsl:when test="normalize-space($cropValue) = ''">
<xsl:value-of select="0"/>
</xsl:when>
<xsl:when test="$loop = $position">
<xsl:call-template name="ConvertToXEPUnits">
<xsl:with-param name="value" select="substring-before($cropValue,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$loop = 3">
<xsl:call-template name="ConvertToXEPUnits">
<xsl:with-param name="value" select="substring-after($cropValue,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="splitCrop">
<xsl:with-param name="cropValue" select="substring-after($cropValue,' ')"/>
<xsl:with-param name="loop" select="$loop + 1"/>
<xsl:with-param name="position" select="$position"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ConvertToXEPUnits">
<xsl:param name="value"/>
<xsl:variable name="unit" select="translate($value, '-0123456789.', '')"/>
<xsl:choose>
<xsl:when test="not($unit)">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="numeric-value" select="number(substring-before($value, $unit))"/>
<xsl:choose>
<xsl:when test="$unit = 'pt'">
<xsl:value-of select="$numeric-value * 1000"/>
</xsl:when>
<xsl:when test="$unit = 'mm'">
<xsl:value-of select="$numeric-value * 72000 * 25.4"/>
</xsl:when>
<xsl:when test="$unit = 'cm'">
<xsl:value-of select="$numeric-value * 72000 * 2.54"/>
</xsl:when>
<xsl:when test="$unit = 'in'">
<xsl:value-of select="$numeric-value * 72000"/>
</xsl:when>
<xsl:otherwise>
<!-- suppose the value already is in user units (XEP) -->
<xsl:value-of select="$numeric-value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
_______________________________________________
(*) To unsubscribe, please visit http://lists.renderx.com/mailman/options/xep-support
(*) By using the Service, you expressly agree to these Terms of Service http://w
ww.renderx.com/terms-of-service.html
This archive was generated by hypermail 2.1.8 : Mon Sep 15 2014 - 13:56:23 PDT