Forum How do I...?

What is the best way to reduce whitespace before images rewithout floating them?

thespacecamel
I'm dealing with writers who liberally add images to their documents, but insist that the images appear in the same place relative to the surrounding text (i.e., having them float unless fit isn't an option).
Because the images don't necessarily fit onto the current page, they're often bumped to the next page leaving a lot of whitespace (see attached PDF).
If I were using Microsoft Word, I would just resize the image until it fitted on the current page (unless it would then be tiny, in which case I'd move it to the next page and there wouldn't be too much whitespace anyway).

But what is the best approach with Prince? Or what would designers do in other software?
  1. not snapped.pdf177.2 kB
    notice whitespace before the image
phillipgessert
Simplest solution might be to encourage them to add something like e.g. "(See Figure 1)" in the text so you can still allow the image to float elsewhere as needed. Informative captions on the images can help with that sort of thing too.

Otherwise you can experiment with smaller widths + float:outside, which could at least reduce occurrences, but there aren't a lot of good solutions to fitting more content than remaining space allows.
markbrown
Hi,

Attached is an example that tries to solve this using the Box Tracking API and Multi-Pass formatting. We set a minimum height in the CSS for the first pass, then for the second pass we set the height of each relevant image to the difference between the top of the image and the bottom of the page (minus a bit for leeway).

In the first section, the image fits in its natural position so it doesn't get pushed to the next page, it just increases size so it reaches the bottom. In the second section the image does not fit in its natural position so it gets pushed to the next page. It's height is then increased up to the value of max-height.

Results both with and without running the script are attached for comparison (note that it requires the --javascript option in order to run).

Cheers,
Mark
  1. img-fill-noscript.pdf57.6 kB
    Result without processing
  2. img-fill.html8.2 kB
    Source file
  3. img-fill.pdf57.9 kB
    Result with processing
thespacecamel
😍😍 yessssss
Yes that is exactly what I was hoping for, thanks so much @markbrown! You're a life-saver, thanks!

And thanks also for weighing in @phillipgessert. Ya I think your solution was the best solution that CSS alone could provide.
thespacecamel
Ok I've got it working, although footnotes mess it up. When a footnote is added to the page, the effective pagesize is reduced so the image gets bumped downwards... I'm exploring what data is made available through the box API to compensate for this...
thespacecamel
Found a solution: loop over the page box's children, looking for one of type "FOOTNOTES", and then subtract its height from the new height. Like so:

Prince.trackBoxes = true;

Prince.registerPostLayoutFunc(function() {
    var xs = document.getElementsByClassName("pmb-dynamic-resize");
    for (var i = 0; i < xs.length; ++i) {
        var x = xs[i].getElementsByTagName("img")[0];
        var box = x.getPrinceBoxes()[0];
        var p = PDF.pages[box.pageNum-1];


        var footnotes_height = 0;
        for (var index in p['children']){
            var child = p['children'][index];
            if(child['type'] === 'FOOTNOTES'){
                footnotes_height = child['h'];
            }
        }
        var new_height = box.y - (p.y - p.h) - 10 - footnotes_height;

        x.style.height = new_height + "pt";
    }
});
  1. A Nice Building(39).pdf176.9 kB
    image getting automatically resized