[mapguide-users] Re: Generate a pdf view of the current map

ram282 ram282 at gmail.com
Fri Jul 9 00:18:25 EDT 2010


I had successfully generated pdf in this way.... 

In this, I am temporarily saving the image files and after generating the
pdf, I am deleting them. Yo should require the php file called fpdf.php,
through which u are going to generate the pdf.  

And I also changed some configuration settings in the php.ini (LOCATED AT
C:\Program Files\OSGeo\MapGuide\Web\Php\php.ini)

remove the semicolon at the start of the lines
;extension=php_curl.dll
;extension=php_gd2.dll    in the php.ini file....

 Here is the code..

<?php

$fusionMGpath = '../../layers/MapGuide/php/';
include $fusionMGpath . 'Common.php';
require($fusionMGpath . 'fpdf.php'); 


  $locale = "en";
    $mapName = "";
    $sessionId = "";
    $isTitle = "";
    $isLegend = "";
    $isArrow = "";
    $title = "";
    $scale = "";
    $centerX = "";
    $centerY = "";
    $dpi = "";
    $Server= "";
    
    $clAgent = "Fusion%20Viewer";
    $tempMapImgPath="";
    GetRequestParameters();
    $agent = GetRootVirtualFolder() . "/mapagent/mapagent.fcgi";
    
    $imgRequest = $Server . $agent .
"?OPERATION=GETMAPIMAGE&VERSION=1.0.0&FORMAT=PNG&LOCALE=en&MAPNAME=" .
$mapName . "&SESSION=" . $sessionId .
"&SETDISPLAYWIDTH=480&SETDISPLAYHEIGHT=580&SETDISPLAYDPI=" . $dpi .
"&SETVIEWSCALE=" . $scale . "&SETVIEWCENTERX=" . $centerX .
"&SETVIEWCENTERY=" . $centerY . "&SEQ=0.2873515576651681&CLIENTAGENT=" .
$clAgent;

    $LegendImgRequest = $Server . $agent .
"?OPERATION=GETMAPLEGENDIMAGE&VERSION=1.0.0&MAPNAME=" . $mapName .
"&SESSION=" . $sessionId . "&CLIENTAGENT=" . $clAgent .
"&WIDTH=180&HEIGHT=580&FORMAT=PNG";
    

// declaring the abcissa and ordinates of the images for the pdf.
    $LegendA=10;
    $LegendO=20;
    $MapA=45;
    $MapO=20;
    $NorthA=145;
    $NorthO=120;

class PDF extends FPDF
{
//Page header
function Header()
{
   global  $isTitle;
   global $title;
  if($isTitle=='1')
  {
    $this->SetFont('Arial','B',15);
    $this->Cell(80);
    $this->Cell(30,10,$title,0,1,'C');
    $this->Ln(20);
  }
}
//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');

}
}


//Instanciation of inherited class
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();

if($isTitle=='0')
{ 
    $LegendO=$LegendO-10;
    $MapO=$MapO-10;
    $NorthO=$NorthO-10;
    
   
}
if($isLegend=='0')
{ 
    $MapA=$MapA-40;
    $NorthA=$NorthA-40;
    saveImage($imgRequest,$sessionId,"Map",".png");
    $pdf->Image($tempMapImgPath,$MapA,$MapO,100,100);
    unlink($tempMapImgPath);
   
}

if($isLegend=='1')
{
    saveImage($LegendImgRequest,$sessionId,"Legend",".png");
    $pdf->Image($tempMapImgPath,$LegendA,$LegendO,35,100);
    unlink($tempMapImgPath);
}
    saveImage($imgRequest,$sessionId,"Map",".png");
    $pdf->Image($tempMapImgPath,$MapA,$MapO,100,100);
    unlink($tempMapImgPath);
    
if($isArrow=='1')
{ 
    
   $pdf->Image("pr_north.gif",$NorthA,$NorthO,15,15);
}

$pdf->Output();


function saveImage($imageSource,$sessionId,$ImageType,$ImageFormat)
{   
    global  $tempMapImgPath;
    $ch = curl_init($imageSource);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close($ch);
    $tempMapImgPath = $ImageType.$sessionId.time().$ImageFormat;
    $fp = fopen($tempMapImgPath,'w');
    fwrite($fp, $rawdata);
    fclose($fp);
}

function GetParameters($params)
{
    global $mapName, $sessionId, $title, $locale;
    global $scale, $centerX, $centerY, $dpi;
    global $isTitle, $isLegend, $isArrow;
    global $Server;

    if(isset($params['LOCALE']))
        $locale = $params['LOCALE'];
    $mapName = $params['MAPNAME'];
    $sessionId = $params['SESSION'];
    $isTitle = $params['ISTITLE'];
    $isLegend = $params['ISLEGEND'];
    $isArrow = $params['ISARROW'];
    $title = $params['TITLE'];
    $scale = $params['SCALE'];
    $centerX = $params['CENTERX'];
    $centerY = $params['CENTERY'];
    $dpi = $params['DPI'];
    $Server = $params['SERVER'];
}

function GetRequestParameters()
{
    if($_SERVER['REQUEST_METHOD'] == "POST")
        GetParameters($_POST);
    else
        GetParameters($_GET);
}

?>



save the above code as some file  mappdf.php in the folder  C:\Program
Files\OSGeo\MapGuide\Web\www\fusion\widgets\Print


I will call this pdf file from print.js (widget) 

 showPdf: function() {

        var mainMap = this.getMap();

//   this.printablePdfURL  is  Fusion.getFusionURL() + widgetTag.location +
'Print/mappdf.php';

        var url = this.printablePdfURL + '?';
        var extents = mainMap.getCurrentExtents();
        var centerX = (extents.left + extents.right) / 2;
        var centerY = (extents.top + extents.bottom) / 2;
        var dpi = mainMap._nDpi;
        var scale = mainMap.getScale();
        var maps = mainMap.getAllMaps();
        url = url + 'MAPNAME=' + mainMap.getMapName();
        url = url + '&SESSION=' + maps[0].getSessionID();
        url = url + '&CENTERX=' + centerX;
        url = url + '&CENTERY=' + centerY;
        url = url + '&DPI=' + dpi;
        url = url + '&SCALE=' + scale;
        url = url + '&ISTITLE=' + (this.showTitle != '' ? '1' : '0');
        url = url + '&ISLEGEND=' + (this.showLegend ? '1' : '0');
        url = url + '&ISARROW=' + (this.showNorthArrow ? '1' : '0');
        url = url + '&SERVER=' +
Fusion.getFusionURL().split('/mapguide')[0];
        if (this.pageTitle != '') {
            url = url + '&TITLE=' + this.pageTitle;
        }

        window.open(url, 'printablepdf', '');
    }


-- 
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Generate-a-pdf-view-of-the-current-map-tp5264270p5272913.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list