Context Navigation

Outputting maps to PDF

0. Background

While 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.

This 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.

1. How does it work?

In brief, we will pass parameters required to render a map to a PHP script that will:

2. Create the PDF document

To create a new PDF document. Get this stage and step 5 working before you try inserting Mapserver elements.

3. Render PNG views at a suitable resolution

Work 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.

$map->set(width,1200);
$map->set(height,1200);

Of 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:

The current extent (say $ext[])

Layer status (say $layer[])

Open your map file and pass these back through to set the map file into the state the user is expecting, something like: