Search code examples
xmlimagepdfxsl-foapache-fop

Get images to show at their normal size


I am an XSL-FO newbie. When I render my simple script to pdf using OxygenXML/ XMLmind it automatically resizes my images to their correct size. I have taken the fo output from OxygenXML/ XMLmind and tried to copy it exactly, but the images in my pdf still come out larger than they should be.

What am I doing wrong. PS: I am using Apache Fop 2.6 to process my .fo files.

<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master page-width="8.5in" page-height="11in" master-name="BodyPage">
            <fo:region-body margin-top=".5in" margin-right=".5in" margin-bottom=".5in" margin-left=".5in"/>
            <fo:region-before region-name="header" extent=".5in" display-align="after"/>
            <fo:region-after region-name="footer" extent=".5in" display-align="before"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="BodyPage">
        <fo:flow flow-name="xsl-region-body">
            <fo:block space-before="1pc">
                <fo:external-graphic src="url(images/image11.png)" content-width="316px" content-height="292px" width="auto" scaling="uniform"/>
                <!-- <fo:external-graphic src="url(images/image11.png)" overflow="hidden" content-width="316px" content-height="292px" width="auto" scaling="uniform"/> -->
            </fo:block>
            
            <fo:block space-before="1pc">
                <fo:external-graphic src="url(images/image11.png)" content-width="316px" content-height="292px" width="auto" height="auto"/>
            </fo:block>
            
            <fo:block space-before="1pc">
                <fo:external-graphic src="url(images/image11.png)" content-width="scale-to-fit" content-height="100%" width="100%" height="auto" />
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

Solution

  • It is likely that your PNG image records its resolution as 72 dots per inch. It is likely, also, that FOP is using 1px as 1/72 inch or 1/96 inch. See https://www.w3.org/TR/xsl11/#pixels

    content-width="100%" says to render the image at its intrinsic width, which looks like it will be 316 pixels at, say, 72 pixels per inch or approximately 4.39 inch. (See https://www.w3.org/TR/xsl11/#content-width)

    You could:

    • Edit the graphic in an image editor to change its resolution
    • Edit the graphic to resize it to be a different number of pixels
    • Specify a fixed scale on the fo:external-graphic
    • Maybe change FOP's definition of a pixel. AH Formatter has a setting for this (https://www.antenna.co.jp/AHF/help/en/ahf-optset.html#pxpi), so FOP might as well