Developer’s Corner: Improving Printing for GeoServer – Part 2

Dear all,
recently we worked on the MapFish print module used by the Geoserver Printing Module to improve it in several ways, in particular for what concerns legends printing, but not limited to that.

In this post we would like to share the improvements we made, so that they can be useful to other Geoserver printing module users.

Mapfish print module 2.0

First of all, we did all our improvements on a 2.0.x version of the MapFish print module, while the official Geoserver Printing Module is still stuck on the 1.2 version.
The good news is that the 2.0-SNAPSHOT version seems to work perfectly with the current stable release of Geoserver, and only one jar needs to be updated (together with the inclusion of a Spring application context xml). It’s good news because, of course, the 2.0 version has some new features that can be useful to Geoserver users.

For this reason we are going to push for the inclusion of the new version for the 2.5 series of Geoserver.

Legends improvements

Some new options have been added to the legends block definition in config.yaml.
Some of them are basic ones:

failOnBrokenUrl: false (defaults to true)

This one is a legends specific version of the brokenUrlPlaceholder flag that avoids errors when GeoServer returns an empty image for a legend (it can happen if the item is not visible at the requested scale).

Legends flow: multicolumn and multipage

Another set of options permit to control how legend items flow in the PDF document. First we should mention that the MapFish print module 2.0 itself added support for multicolumn legends. We pushed that further by allowing multipage legends (on a single or multiple columns).

maxHeight:  (defaults to 0, meaning no limit)

This option enables multicolumn layout, fixing a max height of a single column.

maxColumns: <# of columns> (defaults to 0, meaning no limit)

This option makes the max number of columns fixed (for a single page if multipage is enabled, or in total). The legends maxWidth space will be equally distributed for the number of columns used.

overflow: true (defaults to false)

This option enables multipage layout. When using it, maxHeight and maxColumns options are mandatory. Please note that the overflowing legend items will be drawn on one or more pages following the one where the legends block is defined. The overflowing pages will have the same format (size, margins and so on) of the first one, but only legend items will be put on them, no other element present in the original page will be copied.

dontBreakItems: true (defaults to false)

This option avoids that a title and its related legend icons could be drawn on different columns / pages.

reorderColumns: true (defaults to false)

This option allows the reordering of legend items to better fit columns space.

maxIconHeight:  (defaults to 0, meaning unlimited)

This option allows the resizing of icons taller than the column max height value.

Legends flow: fixed space usage

fitWidth:   (defaults to 0)

fitHeight:   (defaults to 0)

This couple of options allow having an automatically resized legend to fit a given space (width, height, or both). When only one option is given, the aspect ratio is preserved.

Some examples

Here some sample configurations of multicolumn / multipage layouts for your legends.
A4 portrait 1 column x page
- !legends      
failOnBrokenUrl: false
horizontalAlignment: left
iconMaxWidth: 0
iconMaxHeight: 700
maxHeight: 750
maxColumns: 1
maxWidth: 800
layerSpace: 5
layerFontSize: 12
classIndentation: 5
classFontSize: 8
classSpace: 4
backgroundColor: #ffffff
reorderColumns: true
dontBreakItems: true
overflow: true

A4 landscape 2 column x page
- !legends      
failOnBrokenUrl: false
horizontalAlignment: left
iconMaxWidth: 0
iconMaxHeight: 500
maxHeight: 535
maxColumns: 2
maxWidth: 780
layerSpace: 5
layerFontSize: 12
classIndentation: 5
classFontSize: 8
classSpace: 4
backgroundColor: #ffffff
reorderColumns: true
dontBreakItems: true
overflow: true

Client-side rendered images

We added support for client-side generated images (only svg format at the moment) in the printing.
This can be accomplished in two steps:
  • include an image block in the config.yaml layout, with a name and no content defined
  • include a named block in the print spec with the svg content to match the yaml one
Obviously you can configure as many images as you like, just give them a different name.
For example:

Config.yaml
- !columns
width: 580
height: 271
absoluteX:70
absoluteY:400
items:
- !image
name: chart1
maxWidth: 580
maxHeight: 271
rotation: '${rotation}'
create.json

{
...
chart1:{
content: 'SVG content'
},
...
}

So, that’s it for the moment. If you wish to test our improvements, just clone our fork of the MapFish print module on Github, you can find the latest code at https://github.com/geosolutions-it/mapfish-print.

You can also find some more documentation on these improvements on the related wiki pages and on the original  MapFish print module documentation page.

We are going to continue improving the printing support in Geoserver in the following months, so stay in touch with us!

If you are interested in learning about how we can help you achieving your goals with our Open Source products and professional services, do not hesitate to contact us!

The GeoSolutions team,