I am trying to create a pdf document that contains some images. My problem is that the images are hown out of the border of the document. I am using Apache FOP 1.1.
Here it is the xsl-fo document:
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" line-stacking-strategy="font-height">
<fo:layout-master-set>
<fo:simple-page-master margin-bottom="36pt" margin-left="72pt" margin-right="72pt" margin-top="36pt" master-name="pm0" page-height="792pt" page-width="612pt">
<fo:region-body margin-bottom="36pt" margin-top="36pt" overflow="visible" region-name="body"/>
<fo:region-before extent="720pt" overflow="visible" region-name="header"/>
<fo:region-after display-align="after" extent="720pt" overflow="visible" region-name="footer"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="pm0" orphans="2" white-space-collapse="false" widows="2">
<fo:flow flow-name="body">
<fo:block end-indent="0pt" line-height="13.55pt" space-after="8pt" start-indent="0pt" text-align="start" text-indent="0pt">
<fo:inline font-family="Calibri, sans-serif" font-size="11pt">TEST1</fo:inline>
</fo:block>
<fo:block end-indent="0pt" line-height="13.55pt" space-after="8pt" start-indent="0pt" text-align="start" text-indent="0pt">
<fo:external-graphic content-height="214.55pt" content-width="310.55pt" height="214.55pt" scaling="non-uniform" src="url('file:///C:/Development_Test/test1.png')" width="310.55pt"/>
</fo:block>
<fo:block end-indent="0pt" line-height="13.55pt" space-after="8pt" start-indent="0pt" text-align="start" text-indent="0pt">
<fo:inline font-family="Calibri, sans-serif" font-size="11pt">TEST2</fo:inline>
</fo:block>
<fo:block end-indent="0pt" line-height="13.55pt" space-after="8pt" start-indent="0pt" text-align="start" text-indent="0pt">
<fo:external-graphic content-height="356.35pt" content-width="139.9pt" height="356.35pt" scaling="non-uniform" src="url('file:///C:/Development_Test/test2.png')" width="139.9pt"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
I think the problem comes from having the attribute line-stacking-strategy="font-height"
set in the fo:root
, so that its value is inherited by all the blocks in the FO document.
As per the XSL recommendation, the value font-height
makes the processor build lines using the nominal-requested-line-rectangle, meaning that only the properties of the fo:block
will be taken into account when computing the line height, ignoring the inline elements such as fo:external-graphic
.
In other words, as all your fo:block
elements have line-height="13.55pt"
the FO processor creates lines whose height is exactly 13.55pt, regardless of what is inside the blocks. The images, which are taller, are then placed in these lines according to vertical-align
(which defaults to baseline
), overflowing over the preceding lines and the page margins.
Solution: use line-stacking-strategy="max-height"
instead; you may want to set vertical-align
on the images too.
A final note of warning: you are using a 5-year-old version of FOP; at the moment the latest version is 2.2, released in April 2017.