print option

Piero Cavalieri piero.cavalieri at HEIDI.IT
Wed Aug 24 03:42:29 EDT 2005


Carlos Eduardo Sibaja Salazar wrote:

>Hi list.
>
>I'm using mapserver with php mapscript in suse 9.1. All the options are
>working fine.
>But now i need your help because i want to add a print function in my
>application,
>but i'm not sure how to do that with php. My application is based in the
>gmap example
>(http://www2.dmsolutions.ca/gmap/gmap75.phtml)
>
>I need to create a new browser window only with the map, leyend, scale and
>reference map.
>Is important to say that the map could be a zoom of the original map.
>
>Could somebody give me some ideas.
>Thanks
>

If you use pdflib to print, something like this (should do better ...):

1)gmap75.phtml:

<a href="#"
onclick="window.open('printSetup.php?PHPSESSID=<?=session_id()?>&ext=<?php
         $dfMinX = $gpoMap->extent->minx;
         $dfMaxX = $gpoMap->extent->maxx;
         $dfMinY = $gpoMap->extent->miny;
         $dfMaxY = $gpoMap->extent->maxy;
         $ext = array($dfMinX, $dfMinY, $dfMaxX, $dfMaxY);
         echo serialize($ext)?>
        &layerNums=<?php
         $j = -1;
         $layerNums = array();
         for($i=0; $i < $gpoMap->numlayers; $i++) {
          $iLayer = $gpoMap->getLayer($i);
          if($iLayer->status == MS_ON || $iLayer->status == MS_DEFAULT) {
           $j++;
           $layerNums[$j] = $i;
          }
         }
         echo serialize($layerNums);
        ?>', '', 'width=250,height=100')" >
     <img alt="print" src="images/print.jpg" align="center" border="0">
    </a>


2)printSetup.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<HEAD>
<title>print</title>
</HEAD>

<body bgcolor="#E2EFF5">

<form method="post" target="_blank"
action="print.php?PHPSESSID=<?=session_id()?>&ext=<?=$_REQUEST['ext']?>&layerNums=<?=$_REQUEST['layerNums']?>">


<table>
<tr><td>Titolo:&nbsp</td><td colspan="2"><input type="text"
name="title"></td></tr>
<tr><td>Formato:</td><td><select name="format">
       <option value="A4">&nbsp A4 &nbsp
       <option value="A3">&nbsp A3 &nbsp
       <option value="A0">&nbsp A0 &nbsp
      </select>
     </td>
     <td><input type="image" src="images/pdf_print.jpg" BORDER="1"
ALT="Print PDF !"></td>
</tr>


</table>
</form>

</body>
</html>

3) File print.php:

<?php
/*Parametri*/
$format = $_REQUEST['format'];
$thisFileName = "print.php";
$mapOrientation = "west";
$MapResolution = 480; /* non influente? */
$width = 595; /*  A4 in pt - DEFAULT */
$height = 842; /* A4 in pt  - DEFAULT*/
$MapWidth = 1200;
$MapHeight = 1200;
$xpos = 80;
$ypos = 20;
$ArrowWidth = 50;
$ArrowHeight = 50;
$title = $_REQUEST['title'];
$fontWidth = 22;

/*dimension in pt  1pt = 0.3528 mm*/
if($format == "A4") {
 $width =595 ;
 $height = 842;
} else if ($format == "A3") {
 $width = 842;
 $height = 1190;
} else if ($format == "A0") {
 $width = 2380;
 $height = 3368;
}

$ScaleBarWidth = $width/12;
$ScaleBarHeight = $height/12;

if ($format == "A0") {
 $MapWidth = 2400;
 $MapHeight = 2400;
}

$p = PDF_new();

/* This is where font/image/PDF input files live. Adjust as necessary. */
//$searchpath = "images";
//PDF_set_parameter($p, "SearchPath", $searchpath);

/*  open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
    die("Error: " . PDF_get_errmsg($p));
}

/* This line is required to avoid problems on Japanese systems */
PDF_set_parameter($p, "hypertextencoding", "winansi");
PDF_set_info($p, "Author", "Heidi s.p.a.");
PDF_set_info($p, "Title", "Print Sample");

/* User coordinate systems */
PDF_set_parameter($p, "usercoordinates", "true");

/*Generazione mappa*/
//$map = ms_newMapObj($_SESSION["strMapFile"]);
//echo $_SESSION["strMapFile"], "<br>";
$map = ms_newMapObj("gmap75.map");
if(array_key_exists("PATH_TRANSLATED", $_SERVER)) {
$map->set("shapepath", str_replace($thisFileName, "data",
         str_replace("\\", "/", $_SERVER["PATH_TRANSLATED"])));}


$map->set("width", $MapWidth);
$map->set("height", $MapHeight);
$map->set("resolution", $MapResolution);

//localizeMapLabels($map);
//adjustScalebar($map);

/*extensions*/
$ext = unserialize($_REQUEST['ext']);
$map->setextent($ext[0],$ext[1],$ext[2],$ext[3]);

/*Attivo i Layers*/
$layerNums = unserialize($_REQUEST['layerNums']);
//setto tutti a false
for($i=0; $i  < $map->numlayers; $i++) {
 $iLayer = $map->getLayer($i);
 $iLayer->set("status", MS_OFF);
}
//ciclo su $layerNums ed attivo.
for($i=0; $i  < count($layerNums); $i++) {
 $iLayer = $map->getLayer($layerNums[$i]);
 $iLayer->set("status", MS_ON);
// printf("%s\n", $iLayer->name);
}

// change layer scales to ensure pdf image still shows the same extent as
the web image
$map_width = $map->width;
$pdf_ratio = ($map_width/$MapWidth);

$allLayernames = $map->getAllLayerNames();
$numb = count($allLayernames);

for ($i = 0; $i<$numb-1; $i++) {
 $oLayer = $map->getLayerByName($allLayernames[$i]);
 $old_min_scale = $oLayer->minscale;
 $new_min_scale = ($old_min_scale*$pdf_ratio);
 $oLayer->set('minscale', $new_min_scale);
 $old_max_scale = $oLayer->maxscale;
 $new_max_scale = ($old_max_scale*$pdf_ratio);
 $oLayer->set('maxscale', $new_max_scale);
}


/*Immagine mappa*/
$mapObj=$map->draw();
$image_url=$mapObj->saveWebImage();
//$image_map = PDF_load_image($p, "auto", "D:/11b2.jpg", "");
$image_map = PDF_load_image($p, "auto", "C:/tmp".$image_url, "");
if (!$image_map) {
    die("Error: " . PDF_get_errmsg($p));
}

/* Immagine ScaleBar */
$imageScaleBar=$map->drawScaleBar();
$scaleBar_url=$imageScaleBar->saveWebImage();
$image_scaleBar = PDF_load_image($p, "auto", "C:/tmp".$scaleBar_url, "");
if (!$image_scaleBar) {
    die("Error: " . PDF_get_errmsg($p));
}

/* Immagine Leggenda*/
$legendObj=$map->drawLegend();
$legend_url=$legendObj->saveWebImage();
$image_legend = PDF_load_image($p, "auto", "C:/tmp".$legend_url, "");
if (!$image_legend) {
    die("Error: " . PDF_get_errmsg($p));
}

/* Immagine RegerenceMap */
$referenceObj=$map->drawReferenceMap();
$reference_url=$referenceObj->saveWebImage();
$image_reference = PDF_load_image($p, "auto", "C:/tmp".$reference_url, "");
if (!$image_reference) {
    die("Error: " . PDF_get_errmsg($p));
}


/*Legend template*/
$legendWidth = $legendObj->width;
$legendWidth += 50;
$legendHeight = $legendObj->height;
$templateLegend = PDF_begin_template($p, $legendWidth, $legendHeight) ;
 PDF_fit_image($p, $image_legend, 0, 0, "boxsize {".($legendWidth)."
".($legendHeight)."}  fitmethod meet");
 PDF_rect($p, 0, 0, $legendWidth, $legendHeight);
    PDF_stroke($p);
PDF_end_template($p);

/* Map template */
$templateMap = PDF_begin_template($p, $MapWidth, $MapHeight) ;
  PDF_fit_image($p, $image_map, 0, 0, "boxsize {".($MapWidth)." ".
($MapHeight)."}  fitmethod meet");
  PDF_rect($p, 0, 0, $MapWidth , $MapHeight );
  PDF_stroke($p);
PDF_end_template($p);

/* Arrow template */
$templateArrow = PDF_begin_template($p, $ArrowWidth, $ArrowHeight) ;
//northarrow
    pdf_setcolor($p, "fill", "rgb", 0, 0, 0, 0);
    $x_bild = 0;
    $y_bild = 0;
    $font = PDF_findfont($p, "Helvetica", "host", 0);
  pdf_setlinewidth($p, 0.8);
    pdf_circle($p, $x_bild+30, $y_bild+27, 11.2);
 pdf_stroke($p);
 pdf_setlinewidth($p, 1);
 pdf_moveto($p, $x_bild+30, $y_bild+60);
    pdf_lineto($p, $x_bild+23, $y_bild+8);
    pdf_lineto($p, $x_bild+30, $y_bild+11);
    pdf_lineto($p, $x_bild+37, $y_bild+8);
    pdf_closepath_fill_stroke($p);
    pdf_setfont($p, $font, 20);
 // N
 pdf_setlinewidth($p, 3.5);
 pdf_setcolor($p, "fill", "rgb", 0, 0, 0, 0);
 pdf_moveto($p, $x_bild+25, $y_bild+20);
    pdf_lineto($p, $x_bild+25, $y_bild+34);
 pdf_moveto($p, $x_bild+35, $y_bild+20);
 pdf_lineto($p, $x_bild+35, $y_bild+34);
 pdf_stroke($p);
 pdf_setcolor($p, "stroke","rgb", 0.9, 0.9, 0.9, 0);
 pdf_setlinewidth($p, 3.0);
 pdf_moveto($p, $x_bild+25.5, $y_bild+20.9);
    pdf_lineto($p, $x_bild+25.5, $y_bild+33.1);
 pdf_moveto($p, $x_bild+25.5, $y_bild+32.15);
 pdf_lineto($p, $x_bild+34.3, $y_bild+21.85);
 pdf_moveto($p, $x_bild+34.5, $y_bild+20.9);
 pdf_lineto($p, $x_bild+34.5, $y_bild+33.1);
 pdf_stroke($p);
PDF_end_template($p);

/* Reference template */
$referenceWidth = $referenceObj->width;
$referenceHeight = $referenceObj->height;
$templateReference = PDF_begin_template($p, $referenceWidth, $referenceHeight) ;
 PDF_fit_image($p, $image_reference, 0, 0, "boxsize {".($referenceWidth)."
".($referenceHeight)."}  fitmethod meet");
 PDF_rect($p, 0, 0, $referenceWidth , $referenceHeight);
 PDF_stroke($p);
PDF_end_template($p);


/* Dispongo i template e le scritte */
PDF_begin_page_ext($p, $width, $height, "");
$delta = $width * 0.007;
PDF_rect($p, $delta, $delta, $width - 2 * $delta, $height - 2 * $delta);
PDF_stroke($p);
/*title*/
$font = PDF_load_font ($p, "Verdana", "host","");
 if ($font) {
     pdf_setfont($p, $font, $fontWidth);
     pdf_fit_textline($p, $title, $width/85, $height/110, "boxsize
{".($width/13)." ". ($height/1.05)."} orientate ".$mapOrientation."
fitmethod meet position {50 50}");
 }
PDF_fit_image($p, $templateMap, $width/6.5, $height/110, "boxsize
{".($width/1.2)." ". ($height)."} orientate ".$mapOrientation." fitmethod
meet");
PDF_fit_image($p, $templateArrow, $width - $width/8 , $height -
$height/1.01, "boxsize {".($width/10)." ". ($height/10)."} orientate
".$mapOrientation." fitmethod meet");
PDF_fit_image($p, $image_scaleBar, $width * 0.95, $height * 0.5, "boxsize
{".($ScaleBarWidth)." ".($ScaleBarHeight)."} orientate ".$mapOrientation."
fitmethod meet");
PDF_fit_image($p, $templateReference, $width/6.5, $height * 0.65, "boxsize
{".($width/5)." ".($height/5)."} orientate ".$mapOrientation." fitmethod meet");
PDF_fit_image($p, $templateLegend, $width/6.5 + $width/5 + $width/20,
$height * 0.65, "boxsize {".($width/5)." ".($height/5)."} orientate
".$mapOrientation." fitmethod meet");


//PDF_close_image($p, $image_map);
//PDF_close_image($p, $image_legend);
//PDF_close_image($p, $image_scaleBar);

PDF_end_page_ext($p, "");
PDF_end_document($p, "");
$buf = PDF_get_buffer($p);
$len = strlen($buf);


header("Content-type: application/pdf");
header("Content-Length: ".$len);
header("Content-Disposition: inline; filename=image.pdf");
print $buf;
PDF_delete($p);

?>



More information about the mapserver-users mailing list