Andy Iverson
Andy Iverson

Reputation: 11

Make an image take up the rest of a page (and crop it)

I am generating PDFs from XSL-FO with Apache FOP. I would like to be able to format images such that they take up the rest of the space on the page. I would also like to have either the top/bottom or left/right of the image cropped depending on whether the image aspect ratio is too wide or too tall to fit the space in the rest of the page.

Here is a small test I'm working on:

<?xml version="1.0" encoding="iso-8859-1"?>

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="test-page">
            <fo:region-body margin="1in"/>
        </fo:simple-page-master>
    </fo:layout-master-set>

    <fo:page-sequence master-reference="test-page">
        <fo:flow flow-name="xsl-region-body"> 
            <fo:block>Some content.</fo:block>
            <fo:block-container display-align="center" height="100%" overflow="hidden" break-after="page">
                <fo:block>
                    <fo:external-graphic width="100%" content-width="scale-to-fit" src="bgtall.png"/>
                </fo:block>
            </fo:block-container>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

Two problems:

Currently we're solving this problem by generating the PDF twice and cropping the images to fit perfectly. I don't like this solution and I would love to find a better way.

Upvotes: 1

Views: 916

Answers (2)

Tony Graham
Tony Graham

Reputation: 8068

You could use the XSLTExtensions (https://github.com/pplcg/XSLTExtensions, https://www.w3.org/community/ppl/wiki/XSLTExtensions) from the Print and Page Layout Community Group (http://www.w3.org/community/ppl/) to:

  • Do the first formatting pass within the XSLT transformation
  • Get the formatted size of the figures (and of the areas into which they don't fit) from the area tree from the formatting pass
  • Generate the final XSL-FO with the figures centered in their available space (which you seem to already know how to do)

If XSLTExtensions doesn't work for you:

  1. Raise an issue on GitHub
  2. Use scripting, Ant, XProc or whatever to run FOP to generate its area tree XML (there are two formats) and process that in a second stylesheet to generate the final XSL-FO

Upvotes: 0

Kevin Brown
Kevin Brown

Reputation: 8877

While it should be simple, apparently it does not work in FOP. I will show you results from RenderX and FOP:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master margin-top="1in" margin-left="1in"
            margin-bottom="18pt" margin-right="18pt"
            page-width="700pt" page-height="780pt" master-name="first">
            <fo:region-body margin-top="0pt"/>
            <fo:region-before extent="0pt"/>
            <fo:region-after extent="0pt"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="first">
        <fo:flow flow-name="xsl-region-body">
            <fo:block space-before="18pt">Bingo</fo:block>
            <fo:block space-before="18pt">Bango</fo:block>
            <fo:block space-before="18pt">Bongo</fo:block>
            <fo:block>
                <fo:block-container reference-orientation="90" background-color="yellow">
                    <fo:block><fo:leader/></fo:block>
                </fo:block-container>
            </fo:block>
            <fo:block>I should be on the next page</fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

Which gives you the following in RenderX --- the yellow area takes the full remaining area on the page and content after is on the next page. It does not matter how much content is before, it would "expand" to take the fill progression dimension and since it is rotated, this is the space from the start to the top of the footer area. The second part of your question would just involve inserting and scaling an image in the yellow area.

enter image description here

What you will get from FOP is completely wrong. It puts the yellow block on the second page the content following on the third page and the yellow block is drawn completely wrong. This is from FOP for the exact same file:

enter image description here

If you are going to use FOP, you should submit a bug as their formatting is wrong. There is no reason to go onto part two of your question (to insert and scale an image) because you cannot achieve part one.

Upvotes: 0

Related Questions