0

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();
  //console.log(mapLayers);

  mapLayers.forEach(function (layer, i) {
      if (layer instanceof Group && layer.getVisible() == true ) {
          layer.getLayers().getArray().forEach(function(sublayer, j, layers) {
              allLayers.push(sublayer);
          })
      } else if ( !(layer instanceof ol.layer.Group) && layer.getVisible() == true ) {
              allLayers.push(layer);
      }
  });
  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];
  layer_array.push(layer_specs);
  console.log(layer_array);


  return layer_array;
}

function getSpecs() {
  //console.log(getAllofEm());
  var map_layers = getMapLayers();
  console.log(map_layers);

  var specs={
   "layout":"A4 portrait",
   "srs":"EPSG:4326",
   "units":"degrees",
   "dpi":150,
   "mapTitle":"test",
   "layers": map_layers,
   // "layers":[
   // {
   //    "baseURL":"http://localhost:9090/geoserver/BernHardt_Practical/wms",
   //    "opacity":1.0,
   //    "singleTile":false,
   //    "type":"WMS",
   //    "layers":["Nepal"],
   //    "format":"image/png"
   // }
   // ],
   "pages":[
   {
      "center":[84.2,28.2],
      "scale":2000000
   }
   ]
  };
  return specs;
}

var exportButton = document.getElementById('export-pdf');
var url = 'http://192.9.200.16:8080/geoserver/pdf/print.pdf';

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

  var httpRequest = new XMLHttpRequest()
  httpRequest.open("POST", url, true);
  httpRequest.onreadystatechange = function (returnedData) {
  // code
    alert("Passed");
  }
  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)
0:
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.