I am trying to use the GeoServer printing module to export a map to pdf. I have installed the plugin as described here.

I'm using the default config.yaml file that was installed with the plugin.

The code to get the map specifications and then set up the POST request:

function getMapLayers() {
  var allLayers = [];
  var mapLayers = window.my_map.getLayers().getArray();

  mapLayers.forEach(function (layer, i) {
      if (layer instanceof Group && layer.getVisible() == true ) {
          layer.getLayers().getArray().forEach(function(sublayer, j, layers) {
      } else if ( !(layer instanceof ol.layer.Group) && layer.getVisible() == true ) {
  var layer_array = [], layer_specs = {};
  layer_specs.baseURL = allLayers[0].values_.source.urls;
  layer_specs.opacity = allLayers[0].values_.opacity;
  layer_specs.singleTile = false;
  layer_specs.type = allLayers[0].type;
  layer_specs.layers = [allLayers[0].values_.title];

  return layer_array;

function getSpecs() {
  var map_layers = getMapLayers();

  var specs={
   "layout":"A4 portrait",
   "layers": map_layers,
   // "layers":[
   // {
   //    "baseURL":"http://localhost:9090/geoserver/BernHardt_Practical/wms",
   //    "opacity":1.0,
   //    "singleTile":false,
   //    "type":"WMS",
   //    "layers":["Nepal"],
   //    "format":"image/png"
   // }
   // ],
  return specs;

var exportButton = document.getElementById('export-pdf');
var url = '';

exportButton.addEventListener('click', function() {
  var mapspecs = getSpecs();
  var json = (JSON.stringify(mapspecs));

  var httpRequest = new XMLHttpRequest()
  httpRequest.open("POST", url, true);
  httpRequest.onreadystatechange = function (returnedData) {
  // code
  httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  httpRequest.open('POST', url)
  httpRequest.send('spec=' + encodeURIComponent(json));


The code already takes into consideration the suggestions in the following: Missing 'Spec' Parameter using print.pdf on GeoServer Print Module

However, running the code gives me a server error 500 and displays "Error whie generating PDF: Missing 'spec' parameter".

I am taking a stab and guessing there's an issue with the way I have built the specs. The map specs printed to the console are as follows:

{layout: "A4 portrait", srs: "EPSG:4326", units: "degrees", dpi: 150, mapTitle: "test", …}
dpi: 150
layers: Array(1)
baseURL: ["https://a.tile.openstreetmap.org/{z}/{x}/{y}.png"]
layers: ["OSM basemap"]
opacity: 1
singleTile: false
type: "TILE"
__proto__: Object
length: 1
__proto__: Array(0)
layout: "A4 portrait"
mapTitle: "test"
pages: Array(1)
0: {center: Array(2), scale: 2000000}
length: 1
__proto__: Array(0)
srs: "EPSG:4326"
units: "degrees"
__proto__: Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.