We will discuss here how to set up a solution to generate pdf for printing from an interactive mapping page, the OpenLayers 4 encoded page and the Geoserver 2.14 server. Technically, to read the Geoserver doc,everything should be very simple: Geoserver Print plugin installation , installing test, configuration and that’s it
Although installation is quick and easy, the problems start when you want to use the proposed test page to make sure everything is working properly.Impossible to make it work and each time we find the source of the problem and solve it, a new error message appears and another problem arises.
The script paths are no longer valid, the contents of the libraries have changed, in short, if you want to waste your time, you can always try.
You can also look for solutions on the net and as me, find posts in forums with the same questions asked … and without any answer.
Let’s start from the very beginning…
The print module must generate a pdf file containing a map and information. This map is produced by Geoserver and the request is generated by an html page written with OpenLayers. Even if you can do it all without another library, you often use the GeoExt library which simplifies the application coding.
As for the generation of the pdf, it relies on the java Mapfish Print
If for one, Geoserver version does not affect the result, it is not the same for the other components.
The installation described in the Geoserver documentation refers to Mapfish version 2. It has been replaced by MapFish3 since 2015. It should be understood that the two versions are completely different in terms of syntax and workflow.
MapFish V2 is used with the GeoExt V2 library which relies on OpenLayers
2. That is to say old versions.
If you are working with
OpenLayers 4 or 5, the version of GeoExt to use is version 3, and the Mapfish version
Therefore, the installation the Geoserver print plugin (MapFish V2) is not needed.
This is not a problem; MapFish3 can be installed quite easily. The problem or problems come when you are looking in the net for documentation,examples, information on error messages. You will have, anyhow, references of all the versions but without knowing which version it refers to …
Installing Mapfish 3
The Mapfish Print WAR file is a Java archive that can be run on any Javaweb application server such as Tomcat or Jetty. To use Mapfish Print as a stand-alone Web application, you must:
- Install Java 7 or later
- Download the web application server: war file from http://mapfish.github.io/mapfish-print-doc/download.html
- Place the WAR print Mapfish file in the web apps directory of the web application server.
- Rename the Mapfish Print WAR file to a more practical name, such as print.war (the name will be part of the URL).
- Start the web application server(Tomcat or Jetty)
Verify that the application is running correctly by visiting http: // localhost: 8080 / print / index.html.
For the time being, do not do anything with this page. The fact that it is displayed is sufficient to confirm that the installation of MapFish has been successful.
How Mapfish 3 works The lack of online documentation is difficult to grasp because there is no clear introduction to the general architecture
In order for the user to obtain a document of this type by clicking on a button « To print » located on a page with an interactive map(html page with code OpenLayers) there are three elements to consider:
- A form template created with Jasper Studio
- a config.yaml file configured on the print server
- an element “specs” to be added to the POST or GET order of the html page
Let’s discuss in more detail.
To create the form we use Jaspersoft
Studio . You can download it from this page .
JasperStudio allows you to create a form template where you can place blocks of text, images, maps and captions but also other blocks such as graphics
The purpose of this work is to create a .jrxml file with the page template to create. The very content of the page will be defined elsewhere: the layers to be represented, the scale, the projection,…
Configuration file .yaml
Compared to Mapfish Print version 2, there is always a configuration file in yaml format but it is now much more simple, and focuses on the definition of models: the Jasper files to use (the previous.jrxml), and the parameters to send to the Jasper engine to fill the blocks of the model. Here is an example of a very simple configuration file
What has to be understood in this file:
- the A4 portrait line :! template defines the model name. When we want to use this configuration we will do it by referring to the layout A4portrait
- the report Template is used to indicate the name of our .jrxml file created with JaspersoftStudio.
- the title and map attributes contain the information to be passed to the Jasperengine. This means that we have created a title box and a map box in the form template. Here we inform the content of these two boxes.
Of course, there are other options available for configuration files. But for now, let’s keep it simple.
We have a .jrxml form template and a .yaml configuration file. All that remains is to send the pdf creation request.
From the html page we will make a POST to the server with a spec parameter that will contain, in json format, the parameters of our print request.
Here is an example of a spec :
The line “ layout »:« A4 portrait is used to indicate the link with the config.yaml file with its line A4 portrait:! Template
In the list of sent attributes, in this simple example, we only have the map to be displayed (” map « ) with its formatting parameters (centring, rotation, scale, projection, definition) and, above all,the definition of the layers to be mapped with the definition of their data sources (here a local geoJson file).
In response to this post the page receives the download url of the pdf document produced. To verify this, you can now copy and paste the json code corresponding to the spec in the Test Print window:
Click Create and Get Print or Post and Poll Print , in return you will get your pdf file:
The same yaml configuration file and the same jrxml template can be called with different data. Here is another version of the code json spec which requests the data of the countries served in WMS by Geoserver:
“image / png”,
In the print test page we replace the text json and the result is the following:
And if you use the Geoserver print plugin…
If you still decide to use the plugin provided by Geoserver, you should know that:
- there is no way to use Jaspersoft to create a template. Everything you do with Jaspersoft and MapFish 3 you will have to do manually in the config.yaml file. And you will not have the same possibilities as,for example, to include graphics in your pdf output.
- the syntax of the yaml file is totally different. You will find its description on this page .
- If you want to use the GeoExtlibrary examples as a basis for your developments, you will need to install and use the GeoExt2 version. GeoExt 3 is not compatible withthe Geoserver Print module.