May 20, 2020
Estimated Post Reading Time ~

Adobe CQ Prepends Front Header Image to Pages with .pdf Extension

In this post, I'll show you how to render PDF documentation (.pdf) with an extra image page prepended to the front of the whole PDF document, and how to embed an extra image to the top of an existing page.

I'm using CQ5.6.1 which is not too reliable when I played with an extension so, after trial and error, I modified the above file to reference proxy.jsp using the full path and without carriage return at the end of the line to avoid any possible 500 Internal Server Error. The Page.pdf.jsp file now saved have only one line:

<%@include file="/libs/foundation/components/primary/cq/Page/proxy.jsp" %>

For rendering .pdf, I use the out of the box PDF Rewriter mechanism. CQ5 demonstrates the default PDF Rewriter in their demo sites. For example, you can change an existing URL ends with .html extension:

http://localhost:4502/content/geometrixx-media/en/entertainment/summer-blockbuster-hits-and-misses.html

to .pdf extension to see the default behavior of PDF Rewriter output of a page:

http://localhost:4502/content/geometrixx-media/en/entertainment/summer-blockbuster-hits-and-misses.pdf

If you're lucky, your browser should show you a pdf output generated by a default PDF Rewriter (an XSL-FO file named page2fo.xsl located at /libs/wcm/core/content/pdf/page2fo.xsl by default). If not, you need to configure (see references) and modify the PDF rewriter.

In the sample XSL-FO file, the following lines in red are keys to prepend a front cover image page and embed a header image banner to a page. The final output is a 2 pages PDF documentation:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:dam="http://www.day.com/dam/1.0"
xmlns:fox="http://xmlgraphics.apache.org/fop/extensions">

<xsl:param name="resource"/>

<xsl:template match="dam:thumbnails"/>

<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
<fo:simple-page-master master-name="page"
page-height="29.7cm"
page-width="21.6cm"
margin-bottom="2cm">
<fo:region-body margin-top="0cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="-3.5cm"/>
</fo:simple-page-master>

<fo:page-sequence-master master-name="all">
<fo:repeatable-page-master-reference master-reference="page"/>
</fo:page-sequence-master>
</fo:layout-master-set>

<fox:external-document src="sling://content/dam/public/pdf/frontCoverLATL.jpg/jcr:content/renditions/original"/>

<fo:page-sequence master-reference="all">
<fo:static-content flow-name="xsl-region-before">
<fo:block text-align="end">

</fo:block>
</fo:static-content>

<fo:static-content flow-name="xsl-region-after">
<fo:block text-align="center" font-size="10pt" font-family="serif" line-height="14pt">Page <fo:page-number/></fo:block>
</fo:static-content>

<xsl:choose>
<xsl:when test="jcr:content">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</xsl:when>
<xsl:otherwise>
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="18pt" space-before.optimum="12pt" text-align="start">
<xsl:value-of select="jcr:content/@jcr:title"/>
</fo:block>
</fo:flow>
</xsl:otherwise>
</xsl:choose>
</fo:page-sequence>
</fo:root>
</xsl:template>

<xsl:template match="*[contains(@sling:resourceType,'company/components/articleHeading')]">
<xsl:if test="@text">
<fo:block>
<fo:external-graphic src="url('sling://content/dam/public/pdf/frontCoverLATL.jpg/jcr:content/renditions/original')" content-type="content-type:image/jpeg"/>
</fo:block>
<fo:block font-size="18pt" space-before.optimum="12pt" text-align="start"
margin-top="1cm"
margin-left="2.5cm"
margin-right="2.5cm">
<xsl:value-of select="@text"/>
</fo:block>
</xsl:if>
</xsl:template>

</xsl:stylesheet>


Here's the brief explanation for each snippet in red ...

Tell page2fo.xsl the 'fox' namespace is legit so it'll recognize <fox:external-document>:

xmlns:fox="http://xmlgraphics.apache.org/fop/extensions"

Include in an external image as a separate page in the front of the final PDF document. This image will be a separate pdf page before all other page sequences:
<fox:external-document src="sling://content/dam/public/pdf/frontCoverLATL.jpg/jcr:content/renditions/original"/>
Embed an external image as part of the page (block) in a page of the final PDF document before some other text:
<fo:block>
<fo:external-graphic src="url('sling://content/dam/public/pdf/frontCoverLATL.jpg/jcr:content/renditions/original')" content-type="content-type:image/jpeg"/>

</fo:block>


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.