Changes between Initial Version and Version 1 of PHPMapscriptPDF


Ignore:
Timestamp:
Jan 27, 2009, 9:51:03 AM (15 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PHPMapscriptPDF

    v1 v1  
     1= Outputting maps to PDF =
     2
     3== 0. Background ==
     4
     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 (http://www.pdflib.com) compiled into your PHP install.
     6
     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.
     8
     9== 1. How does it work? ==
     10In brief, we will pass parameters required to render a map to a PHP script that will:
     11
     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
     16
     17== 2. Create the PDF document ==
     18I used an example similar to the one given on the PHP website: http://www.php.net/manual/en/ref.pdf.php
     19{{{
     20    $my_pdf = pdf_new();
     21    ....
     22}}}
     23To create a new PDF document. Get this stage and step 5 working before you try inserting Mapserver elements.
     24
     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.
     27{{{
     28    $map->set(width,1200);
     29    $map->set(height,1200);
     30}}}
     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:
     32
     33  - The current extent (say $ext[])
     34  - Layer status (say $layer[])
     35
     36Open your map file and pass these back through to set the map file into the state the user is expecting, something like:
     37{{{
     38    $map->setextent($ext[0], $ext[1], $ext[2], $ext[3]);
     39
     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    }
     48}}}
     49Now you will need to save a rendered view to a PNG file.
     50{{{
     51    $img = $map->draw();
     52    $url = $img->saveWebImage(MS_PNG, 0, 0, 0);
     53}}}
     54Use the same method for all your map elements, such as drawReferenceMap?(), drawScaleBar?() and drawLegend().
     55
     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:
     58{{{
     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);
     62}}}
     63Repeated as needed for any map elements you created.
     64
     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():
     67{{{
     68    ....
     69    pdf_close($my_pdf);
     70
     71    $data = pdf_get_buffer($my_pdf);
     72
     73    header('Content-type: application/pdf');
     74    header('Content-disposition: inline; filename=my_pdf.pdf');
     75    header('Content-length: ' . strlen($data) );
     76
     77    echo $data;
     78
     79?>
     80}}}
     81Gotcha: remember that you cannot send headers if you have at any stage outputed text to the browser.
     82
     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.
     85
     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.