print page

Gregor Mosheh stigmata_blackangel at YAHOO.COM
Wed Apr 13 13:59:27 EDT 2005


--- william paul <williampaul28 at YAHOO.COM> wrote:
> I want to create a print page which contain the
> image from Mapsever window and the legend, but I
> don't know how can I do this.

We use PDF output via PHP/MapScript to accomplish
this. The following PHP/MapScript program does
something close to what you want. Credits: Matthew
Perry, and mods by me


$mapfile = $_REQUEST['map'];
$fontfile =
'/usr/local/apache/hostgis/dev/mwc-lib/arial.ttf';

// 828 and 576 are the x/y of the pdf document..
should be the same as 11.5 x 8 in
// at 72 pixels per inch
$pdf = pdf_new();
if (!pdf_open_file($pdf)) {echo "PDF Doesn't work"; }
pdf_begin_page($pdf, 828, 576);

//Set font
pdf_set_parameter($pdf, "FontOutline",
"Arial=$fontfile");
$font = pdf_findfont($pdf, "Arial", "host", 1);
pdf_setfont($pdf, $font , 24);

// Load the mapfile
$map = ms_newMapObj($mapfile);

// Read optional CGI vars
if ($_REQUEST['mapext']) {
        $mapext = $_REQUEST['mapext'];
        $extent = explode(" ", $mapext);
        //echo "$extent[0], $extent[1], $extent[2],
$extent[3]";
        $map->setExtent($extent[0], $extent[1],
$extent[2], $extent[3]);
}

if (isset($_REQUEST['layers'])) {
        $layerlist = $_REQUEST['layers'];
        $layer = explode(" ", $layerlist);

        //turn off all layers 1st
        $numLayers = $map->numlayers;
        for ($i = 0; $i < $numLayers; $i++) {
                $activeLayer = $map->getlayer($i);
                $activeLayer->set("status", 0);
        }

        // Turn on all given layers
        foreach ($layer as $l) {
                $activeLayer =
$map->getlayerbyname($l);
                $activeLayer->set("status", 1);
        }
}

$map->set("width", 1230);
$map->set("height", 1000);

// *** Generate map images, and autodetect their image
type so they can be loaded into the PDF
$imgMap = $map->draw();
$imgLegend = $map->drawLegend();
$imgScalebar = $map->drawScaleBar();
$imgReferenceMap = $map->reference->status ?
$map->drawReferenceMap() : false;

$urlMap = $imgMap->saveWebImage();
$urlLegend = $imgLegend->saveWebImage();
$urlScalebar = $imgScalebar->saveWebImage();
$urlReferenceMap = $imgReferenceMap ?
$imgReferenceMap->saveWebImage() : false;

// figure out the file extension for the generated
images
$fileextensions = array(
   'jpg'  => 'jpeg',
   'jpeg' => 'jpeg',
   'tif'  => 'tiff',
   'tiff' => 'tiff',
   'gif'  => 'gif',
   'png'  => 'png',
);
$extMap          = $fileextensions[
array_pop(explode('.', basename($urlMap))) ];
$extLegend       = $fileextensions[
array_pop(explode('.', basename($urlLegend))) ];
$extScalebar     = $fileextensions[
array_pop(explode('.', basename($urlScalebar))) ];
$extReferenceMap = @$fileextensions[
array_pop(explode('.', basename($urlReferenceMap))) ];

// Filename
$filePrefix = $map->name;

// *** Add images to pdf document
$element = pdf_open_image_file($pdf, $extMap,
$htdocs."/$urlMap");
pdf_place_image($pdf, $element, 200, 30, 0.5);
// 120, 25, .8 => xoffset, yoffset, size_factor
(offsets refer to bottom left corner of the image)
pdf_close_image($pdf, $element);

$element = pdf_open_image_file($pdf, $extLegend,
$htdocs."/$urlLegend");
pdf_place_image($pdf, $element, 10, 200, 1);
pdf_close_image($pdf, $element);

$element = pdf_open_image_file($pdf, $extScalebar,
$htdocs."/$urlScalebar");
pdf_place_image($pdf, $element, 600, 1, 1);
pdf_close_image($pdf, $element);

if ($imgReferenceMap) {
$element = pdf_open_image_file($pdf, $extReferenceMap,
$htdocs."/$urlReferenceMap");
pdf_place_image($pdf, $element, 10, 400, 1);
pdf_close_image($pdf, $element);
}

//Add Title
pdf_show_xy($pdf, $map->name, 10, 540);
// Add Date
pdf_setfont($pdf, $font , 14);
$date = getdate();
$dateStr = $date['mon'] . "/" . $date['mday'] . "/" .
$date['year'];
pdf_show_xy($pdf, $dateStr, 650, 538);

//Add disclaimer
pdf_setfont($pdf, $font , 10);
$disclaimer = "Map generated by PHP/Mapscript 4.0 with
pdflib.";
pdf_show_xy($pdf, $disclaimer, 2, 4);

// Close out pdf file
pdf_end_page($pdf);
pdf_close($pdf);

// *** Send the pdf file back to the web browser
$data = pdf_get_buffer($pdf);

header("Cache-control: private");
header("Pragma: public");
header("Content-type: application/x-unknown");
header("Content-disposition: attachment;
filename=$filePrefix.pdf");
header("Content-length: " . strlen($data));

print $data;




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com



More information about the mapserver-users mailing list