Geoserver print process involves two steps.
First at the server side, you have to configure your yaml file, called config.yaml. Go through the detailed documentation at MapFish print module documentation page.
Once this done, the second step is for client side. Considering you are using openlayers for your front-end, in order to get the list of visible layers, you will need a simple loop with visibility check, e.g.
var layers = "";
for (var i = 0; i < map.layers.length; i++) {
if(map.layers[i].visibility == true){
//get a string of visible layers
layers = layers + map.layers[i].name + ','
}
}
//remove the trailing ','
layers = layers.slice(0, -1);
Now you have to pass this to your print url. e.g
var maptitle= "This is the map title";
var mapcomment= "This is the map comment"
var printurl = "http://host:post/geoserver/pdf/print.pdf?spec={"units":"degrees","srs":"EPSG:4326","layout":"A4","dpi":"300","maptitle":"This is the map title","comment":"This is the map comment","resourcesUrl": "http://host:port/img","layers":[{"baseURL":"http://host:port/geoserver/workspace/wms","opacity":1,"singleTile":true,"type":"WMS","layers":["' + layers + '"],"format":"image/jpeg","styles":[""]}],"pages":[{"center":[' + map.getCenter().lon + ',' + map.getCenter().lat + '],"scale":' + getMapScale(Math.ceil(map.getScale())) + ',"rotation":0}]}'
Of course you will have to make relevant changes in the URL. Next assign this URL to your print button and then call a click function pragmatically.
$("#btnprint").attr("href", printUrl);
$('#btnprint')[0].click();
And done!!!
Best Answer
According to this thread at the GeoExt mailing list: