Changes between Initial Version and Version 1 of PHPMapscriptPDF

Jan 27, 2009, 9:51:03 AM (13 years ago)



  • PHPMapscriptPDF

    v1 v1  
     1= Outputting maps to PDF =
     3== 0. Background ==
     5While Mapserver can render to PDF directly, I found I got more aesthetic results by rendering to a PNG and inserting that into a PDF document. This is not the only way to create a PDF document, but it works for me. You will need to have support for PDFLib ( compiled into your PHP install.
     7This example shows the key parts of the process, you will need to furnish parts of the script yourself (depending on your app) and repeat the process for each map element you want to include.
     9== 1. How does it work? ==
     10In brief, we will pass parameters required to render a map to a PHP script that will:
     12  - create a PDF document
     13  - Render a PNG view at a suitably higher resolution
     14  - Insert the PNG
     15  - Buffer it and send it to the user
     17== 2. Create the PDF document ==
     18I used an example similar to the one given on the PHP website:
     20    $my_pdf = pdf_new();
     21    ....
     23To create a new PDF document. Get this stage and step 5 working before you try inserting Mapserver elements.
     25== 3. Render PNG views at a suitable resolution ==
     26Work back from the assumption that you will need no more than 300 dpi on your page for your map to look presentable. For an A4 map, I am using 150 dpi for an 8' x 8' main map, which is 1200 x 1200 pixels.
     28    $map->set(width,1200);
     29    $map->set(height,1200);
     31Of course, our map will not be very useful unless it is zoomed in to the extent our user requested, and the layers they selected are switched on. Maintain arrays in your application that record:
     33  - The current extent (say $ext[])
     34  - Layer status (say $layer[])
     36Open your map file and pass these back through to set the map file into the state the user is expecting, something like:
     38    $map->setextent($ext[0], $ext[1], $ext[2], $ext[3]);
     40    while($layer[]) {
     41        $layer=$map->getLayer($n);
     42        if($layer[$n]==1) {
     43            $layer->set(status,1);
     44        } else {
     45            $layer->set(status,0);
     46        }
     47    }
     49Now you will need to save a rendered view to a PNG file.
     51    $img = $map->draw();
     52    $url = $img->saveWebImage(MS_PNG, 0, 0, 0);
     54Use the same method for all your map elements, such as drawReferenceMap?(), drawScaleBar?() and drawLegend().
     56== 4. Insert the PNG elements into your PDF document. ==
     57This is really easy, use the pdf_open_image_file() function to import the map elements into your PDF document:
     59    $element = pdf_open_image_file($my_pdf, "png", "$webroot/$url");
     60    pdf_place_image($my_pdf, $element, $xpos, $ypos);
     61    pdf_close_image($my_pdf, $element);
     63Repeated as needed for any map elements you created.
     65== 5. Buffer the PDF and send it to the user. ==
     66Assuming we have been creating the document $my_pdf, when we are done, we merely buffer it and send it to the user using echo():
     68    ....
     69    pdf_close($my_pdf);
     71    $data = pdf_get_buffer($my_pdf);
     73    header('Content-type: application/pdf');
     74    header('Content-disposition: inline; filename=my_pdf.pdf');
     75    header('Content-length: ' . strlen($data) );
     77    echo $data;
     81Gotcha: remember that you cannot send headers if you have at any stage outputed text to the browser.
     83== 6. Additional stuff to try ==
     84Rendering everything as PNG can look ugly, so I step through the key and extract labels so I can render them using PDF's text functions.
     86This can be done for other map element, such as map titles, layer descriptions, or anything else that can be read from the map file.