PDF Output

ANDY CANFIELD andy_canfield at HOTMAIL.COM
Wed Aug 18 13:18:12 EDT 2004


Well it looks like nobody has answered this yet so here is how I do it:

function MakePdfDoc($gbPdfPath){
        GLOBAL $m_Map;
        GLOBAL $gbBDisplayPdfLink;
        //this function is just a wrapper that I use to get the maps cuurrent
bounds
        //you can do this however you want
        $Bounds = GetCurrentBounds();
        //Set the width and hieght to reflect the resolution I want in the PDF
        $m_Map->set("width",1200);$m_Map->set("height",1200);
        //set the map extent
        $m_Map->setextent($Bounds[0],$Bounds[1],$Bounds[2],$Bounds[3]);
        //change the output format I use gif as my map format but it doesn't go to
pdf well
        //so change to png for this
        $m_Map->selectOutputFormat('png');
        //This function simply loops through my layer settings and makes sure
everything I want
        //in the pdf is currently set to ON
        ApplyPdfLayers();
        //draw the image
        $imgToPdf = $m_Map->draw();
        //save the image
        $urlForPng = $imgToPdf->saveWebImage();
        //create a new pdf memory chunk and get a handle to it
        $my_pdf = pdf_new();
        //open it for write
        pdf_open_file($my_pdf);
        //the path format for this cannot be relative
        //so in IIS i concatenate this with the relative path returned by
        //$imgToPdf->saveWebImage(); and that gives me the full path to the image
        $wroot = "C:/somedir/somedir/somedir/";
        $wroot.=$urlForPng;
        $urlForPng=$wroot;
        //open the image file and get a handle to that binary read content
        $element = pdf_open_image_file($my_pdf,"png",$urlForPng);
        //start a new page in the pdf
        pdf_begin_page($my_pdf,1200,1200);
        //place the image into the page
        pdf_place_image($my_pdf,$element,0,0,1);
        //close the read of the image
        pdf_close_image($my_pdf,$element);
        //end the page
        pdf_end_page($my_pdf);
        //close the pdf for write
        pdf_close($my_pdf);
        //extract the pdf contents into a variable
        $data = pdf_get_buffer($my_pdf);
        //reset the map for web drawing and redraw it
        $m_Map->set("width",$Bounds[4]);$m_Map->set("height",$Bounds[5]);
        $m_Map->setextent($Bounds[0],$Bounds[1],$Bounds[2],$Bounds[3]);
        $m_Map->selectOutputFormat('gif');
        $m_Map->draw();
        //at this point our pdf is simply a buffer in memory it doesn't have an
actual
        //file on the server so I create a file on the server in a temp directory
and push
        //the pdf buffer to it. I have a function that generates a unique pdf path
for their session
        //so one session can write multiple pdfs but they simply overwrite the last
one that user
        //created. This ensures that they can't clog my server with pdf's. If they
don't choose
        //to download it then the next time they create one it will overwrite their
last one.
        $handle = fopen($gbPdfPath,"w");
        fwrite($handle,$data);
        //close the file now we have a pdf on the server
        fclose($handle);
        //I use this flag to set whether or not to display a pdf link in a cell
        //on my web page that points to the newly created pdf.
        $gbBDisplayPdfLink = TRUE;
}

_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/



More information about the mapserver-users mailing list