This is the code used in the printing function for the MapServer application running at

Much of this code is hard coded specifically for this application, but hopefully bits and pieces can serve as a useful example.

Code sample created by David Bitner, frequent lurker on MapServer mailing list and IRC channels.

$map = ms_newMapObj($mapfile);
$map->selectOutputFormat('jpeg'); //fpdf seems to play better with jpeg

//get map extent
if ($_REQUEST['mapext']) { 
       $mapext = $_REQUEST['mapext'];
       $extent = explode(" ", $mapext);
       $map->setExtent($extent[0], $extent[1],$extent[2], $extent[3]);

//get list of layers and cycle through to turn them on
if (isset($_REQUEST['layers'])) {
       $layerlist = rtrim($_REQUEST['layers']);
       $layers = explode(" ", $layerlist);
       $numLayers = $map->numlayers;
       for ($i = 0; $i < $numLayers; $i++) {
               $activeLayer = $map->getlayer($i);
			   if ($activeLayer->status != 2) $activeLayer->set("status", 0);
       foreach ($layers as $l) {
               $activeLayer = $map->getlayerbyname($l);
               $activeLayer->set("status", 1);
// this is used to allow cgi substitutions in data strings
for ($i = 0; $i < $map->numlayers; $i++) {
	$activeLayer = $map->getlayer($i);
	if (preg_match_all("/%([\w].+?)%/",$datastring,$matches,PREG_SET_ORDER)>0){
		foreach ($matches as $match){
			if (isset($_REQUEST[$replace])){
			} else {
				if (!($activeLayer->name == 'FlightTracks' && isset($_REQUEST['sDate']) && isset($_REQUEST['eDate']))) $activeLayer->set("status", 0);

//conditionals used to only display certain messages when certain layers are turned on
if ($ftstat==1) $headerstring=$headerstring.'Time Period: '.$_REQUEST['sDate'].'-'.$_REQUEST['eDate'];
if ($addstat==1) $headerstring=$headerstring.' Address Shown: '.$_REQUEST['add'];

$resolution=isset($_REQUEST['resolution'])?$_REQUEST['resolution']:150; //allows to set resolution or defaults to 150dpi
$scalefactor=$resolution/$mapfile_resolution; //how much everything needs to be multiplied by
$map->set('resolution',$resolution); // set new resolution for min/max scale renderings

// loop through layers to extract class images and names and to resize styles and truetype symbols
$numLayers = $map->numlayers;
for ($i=0; $i < $numLayers; $i++) {
	$layer = $map->getlayer($i);
	if ($layer->status == 1 ){
	for ($j=0; $j < $layer->numclasses; $j++) {
		$class = $layer->getClass($j);
		$label = $class->label;
		if ($layer->type !=4){
		if ($label->mindistance>1){
		if ($label->type == 'TRUETYPE') {
		for ($k=0; $k < $class->numstyles; $k++) {
			$style = $class->getStyle($k);
			$style->set("size", $style->size * $scalefactor);
			$style->set("offsetx", $style->offsetx * $scalefactor);
			$style->set("offsety", $style->offsety * $scalefactor);
$nrows=ceil(count($classnames)/4); // make 4 columns to display legend
$legheight=$nrows*.25; // determine height of legend
$mapheight=9.3-$legheight; // determine area left to draw map
$legstart=$mapheight+1.05; // determine where to start the legend

$map->set("width", 7.6*$resolution);
$map->set("height", $resolution*$mapheight); // make map rectangle fit page
//echo $mapheight*$resolution;
//draw map
$imgMap = $map->draw();
$urlMap = $imgMap->saveWebImage();

class PDF extends FPDF
//Page header
function Header()

//Page footer
function Footer()
function draw_legend($textarray,$imgarray,$startx,$starty,$nr){ // function draws the 4 column table
	for ($i=0;$i<count($textarray);$i++){
		if ($i==$nr or $i==2*$nr or $i==3*$nr) {

//Instanciation of inherited class
$pdf=new PDF('P','in','letter');



$pdf->Cell(0,.50,'MACNOISE Interactive Map',0,1,'C');




$date=date('M j, Y g:i');
$datemessage="Map created by on $date. This information is to be used for reference purposes only.";
$disclaimer="The Metropolitan Airports Commission does not guarantee accuracy of the material contained herein and is not responsible for misuse or misinterpretation.";
$pdf->SetTitle('MACNoise Interactive Map');


back to PHPMapScript

Last modified 15 years ago Last modified on Feb 9, 2009, 12:55:24 PM
Note: See TracWiki for help on using the wiki.