[Chameleon-dev] [Bug 1291] New: pdfMap -- code to be made into a proper widget

bugzilla-daemon at bugzilla.maptools.org bugzilla-daemon at bugzilla.maptools.org
Mon Feb 13 10:09:34 EST 2006


http://bugzilla.maptools.org/show_bug.cgi?id=1291

           Summary: pdfMap -- code to be made into a proper widget
           Product: Chameleon
           Version: 2.2
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Widget
        AssignedTo: chameleon-dev at lists.maptools.org
        ReportedBy: jsears at nrcan.gc.ca


This code should be put in a file called "pdfmap.phtml" in a widget folder
called 'pdfMap'. Also in the 'pdfMap' folder there should be:
1. a template called 'blankmap.pdf'
2. pdfMap.widget.php (essentially the same as the file 'template.widget.php' as
given in the cham. document called " Developing a new Chameleon Widget" found
at:  
http://chameleon-tiki.maptools.org/tiki-index.php?page=Developing+a+new+Chameleon+Widget
)
3. pdfMap.dbf (see note 2.)
4. a folder called 'include' - this is the phppdflib class code, free for use
from : http://www.potentialtech.com/ppl.php

*****************************************************************************
following is a breif readme the has some pointers on using the code:
*****************************************************************************
/*PdfMap widget readme
/*Feb09-2006
/* Jeremy Sears, NRCAN, CCRS, Ottawa ON

Description:
This is a custom widget developed for use with chameleon 2.2.1. It will create a
pdf map based on the current map screen.
Essentially, pdfMap will grab/generate essential map elements and paint them on
to a pre-existing .pdf map template (blankmap.pdf).
Currently, if you want to have a scale bar and a north arrow included in the pdf
output, you must make sleight modifications to the
widgets that control these elements. Also one will want to create a proper pdf
map template and play around with the placement of 
the map elements.


Modifications Are:

1. scalebar.widget.php (found in the /scalebar dir)
Add the following on (approx) line 387:

$_SESSION['scaleImageLoc'] = $url;

hint: 
after the line beginning with "$url ..."
before the line beginning with "if($bCWCJSAPI)"

Why? this adds a session variable to track the location of the scale bar.

2. trueNorth.widget.php (found in the /truenorth dir)

Add the following on (approx) line 240:

$_SESSION['TNorthArrowImageloc'] = $szfilename;

why? this adds a session variable to track the location of the north arrow.


*NOTE* if you dont use the above-mentioned widgets in your tools template, you
probably don’t need to modify the widget.php files.

Also, you can include a line like:

$_SESSION['whatMap'] = 'Cummulative Hot Spots';
in the phtml files of your app. This will pass the maptype name on to the pdf
making code


**********************************************************************************************************************************
MapFileNotes
**********************************************************************************************************************************
1. make sure mapserver outputs png images with a line like:
IMAGETYPE png24

2. Use absolute paths, not relative like:
FONTSET "d:/cwfis2/etc/fonts.txt"
** this can dealt with by using pgpmapscript's "new_map_path" ** 


**********************************************************************************************************************************
General Use:
**********************************************************************************************************************************
Copy the pdfMap folder into chameleon’s../htdocs/widgets/ folder.

INclude somthing like the following in your tools template.html

	<cwc2 type="pdfMap" image="icons/icon_print.png" styleresource="TextButtons" 
popupstyleresource="TextButtons" PopupWidth="500" PopupHeight="550"
Toolbar="false" Status="true" Menubar="false"  Visible="true" ImageTip="Generate
Printable PDF" Label="Print PDF">
        <image state="normal"/>
        <image state="selected"/>
        <image state="hover"/>
        </cwc2>




Contents of pdfMap + description:
/include		INclude folder hold phppdflib functions, used to generate/manipulate
the pdf
			thanks a million to http://www.potentialtech.com/ppl.php for great code!

blankmap.pdf		the map template
pdfmap.phtml		code to create pdfmap
pdfMap.widget.php	pdfMap widget nessities
pdfMap.dbf		language stuff... maby need to be changed?

*******************************************************************************
Following is the code contained in pdfMap.phtml:
*******************************************************************************
<?php
// PHPpdf map creation script for chameleon (mapserver)
// Date Feb 2006
// By Jeremy Sears, NRCan, CCRS, ESS

/* notes on further development ...
1. 'clean-up' delete (or ..bool unlink ( string filename [, resource context]
).. ) files needed only for creation of map
2. function(s) to check for, and write if necessary, lines of code in other
widget files necessary for this script
3. Create actual widget for this script. Perhaps have a diagnostic script to
determin what map elements are (not) available
4. mas?
*/
// phppdf class defs
require('include/phppdflib.class.php');


// include the cham session handling file (currently only required for MLT)
include_once("../session.inc.php");
include_once("../CWC2ButtonCache.php");

//figure out session #, etc
$aPath = $_SESSION['gszTmpMapPath'];
$aMapFile = $_SESSION['gszCurrentState'];
$aMapFile = $aMapFile.'.map';        //adds .map to the end of the value of
$aMapFile
$mapfileloc='D:'.$aPath.$aMapFile;        // this now contains the system path
to the mapfile
$searchpath = "D:/ms4w/tmp";  // where mapservers output images go

dl('php_mapscript_46.dll'); // windows   

// create map object and write it to the tmp dir
$map = ms_newMapObj($mapfileloc);
$map->set(width,600);   //600
$map->set(height,600);
$image=$map->draw();
$image_url=$image->saveWebImage();
// concat that path and use gd to convert it into a jpg
$imagefile = $image_url;
$imagefile = $searchpath.$imagefile;
$im = imagecreatefrompng($imagefile);  //get the png image
$bol = imagejpeg($im, $imagefile, 100);
// all images are converted from png to jpg so that phppdflib can use em
// in the conversion process, php rewrites a map element in jpg format 


//draw reference map and write it to the tmp dir
$refImage = $map->drawReferenceMap();
$refImageLoc = $refImage->saveWebImage();
// concat and convert to jpg
$refImageLoc = $searchpath.$refImageLoc;
$im = imagecreatefrompng($refImageLoc);  //get the png image
$bol = imagejpeg($im, $refImageLoc, 100); // output the image as jpeg

// Starts a new pdffile object
$pdf = new pdffile;
$pdf->enable('import'); // functionality to enable import of preexisting pdf
template
ob_start();
$filename = "blankmap.pdf";
$handle = fopen($filename, "r");
$d = fread($handle, filesize($filename));
fclose($handle);

/* Append simply adds the imported PDF to the end of the existing PDF
 * In the case of this example, the imported PDF is the first thing in
 * the file.
 */

$pdf->import->append($d);

/* ->get_pages() returns an array of all the page IDs created by the import
 * process.  Here we use it to get the page ID of the first imported page and
 * draw the words "Hello Word"
 */

$pages = $pdf->import->get_pages();

// note the scale parameter must be stored in the parameter array as an array
itself.
$scaleArray = array("x" => .945, "y" => .81);
$p["scale"] = $scaleArray;

// GET MAP IMAGE and add it to the pdf template
$fh = fopen($imagefile, "r");
$filedata = fread($fh, filesize($imagefile));
fclose($fh);
$image = $pdf->jfif_embed($filedata);
$placement = $pdf->image_place($image, -32, -34, $pages[0], $p);


// get keymap and add it to the pdf template
$fh = fopen($refImageLoc, "r");
$filedata = fread($fh, filesize($refImageLoc));
fclose($fh);
$image = $pdf->jfif_embed($filedata);
$placement = $pdf->image_place($image, 300, 600, $pages[0], $p);



// get scalebar
// $_SESSION['ScaleImagLoc'] holds the relative path to the scalebar image
$scalebarImage = $searchpath.$_SESSION['ScaleImagLoc'];
// convert it to a jpeg
$im = imagecreatefrompng($scalebarImage);  //get the png image
$bol = imagejpeg($im, $scalebarImage, 100);
// add to pdf
$fh = fopen($scalebarImage, "r");
$filedata = fread($fh, filesize($scalebarImage));
fclose($fh);
$image = $pdf->jfif_embed($filedata);
$placement = $pdf->image_place($image, -65, 307.05, $pages[0]);



// get north arrow
//$_SESSION['TNorthArrowImagLoc'] holds relative path to n.arrow image
$NorthImageName = $_SESSION['TNorthArrowImagLoc'];
// convert to jpeg
$im = imagecreatefrompng($NorthImageName);  //get the png image
$bol = imagejpeg($im, $NorthImageName, 100);
// add to pdf
$fh = fopen($NorthImageName, "r");
$filedata = fread($fh, filesize($NorthImageName));
fclose($fh);
$image = $pdf->jfif_embed($filedata);
$placement = $pdf->image_place($image, 200, 600, $pages[0]);


// determine what page the pdf request came from:
// $_SESSION['whatmap'] needs to be set in each of CWFIS's(the chameleon app)
coresponding phtml files
$maptype = $_SESSION['whatMap'];
// get the time from the system
$time = date("D M j G:i:s Y");

// out put these variables' values as text on the pdf. testing 
// $pdf->draw_text(0, 500,'imagefile='.$imagefile, $pages[0]); //jut print variable
// $pdf->draw_text(0, 400, 'mapfileloc='.$mapfileloc, $pages[0]); 
// $pdf->draw_text(0, 300, '$scalebarimage= '.$scalebarImage, $pages[0]);
// $pdf->draw_text(0, 200, '$Northimagloc= '.$NorthImageName, $pages[0]);
 $pdf->draw_text(0, 100, 'Maptype = '.$maptype, $pages[0]);
 $pdf->draw_text(0, 001, 'Mapcreated on = '.$time.' (UTC+5)', $pages[0]);
// $pdf->draw_text(0, 75, '$refImageLoc = '.$refImageLoc, $pages[0]);
 
 
 // littl' loop to help determine image placement
 // creates an 'indexgrid' on the pdf. Allows on to adjust pdf element placement
/* 
$pf["height"] = 1.5;
$pf["width"] = 0.125; 

for ($x=-100; $x <=1000; $x=$x + 10){
	for ($y=-100; $y <= 1000; $y=$y+10){
		 $pdf->draw_text($x, $y, '*'.$x.','.$y, $pages[0], $pf);
		 }
	}
	
*/
 ob_end_clean();
 
 // Now, spit the pdf out to the browser
 //header("Content-Disposition: filename=example.pdf");
header("Content-Type: application/pdf");
$temp = $pdf->generate(0);
header('Content-Length: ' . strlen($temp));
echo $temp;

//  clean-up for now Im only removing the files created explictly for the
creation of the pdf
// use:
// bool unlink ( string filename [, resource context] )

unlink($imagefile);
unlink($refImageLoc);
?>



------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.


Please do NOT reply to this email, use the link above instead to 
login to bugzilla and submit your comment. Any email reply to this
address will be lost.


More information about the Chameleon-dev mailing list