From 55e1a475c00bd54bca6faab3e03f6c9646d1164a Mon Sep 17 00:00:00 2001 From: Dan Little Date: Mon, 21 Dec 2015 17:48:17 -0600 Subject: [PATCH] Added legend support for PDF printing, refs: geomoose/geomoose#99 --- php/print.php | 52 +++++++++++++++++++++++++++++++++++++++++++++- php/print_util.php | 21 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/php/print.php b/php/print.php index 10d4ff7..407540f 100644 --- a/php/print.php +++ b/php/print.php @@ -1,5 +1,5 @@ addPage('P', $templateSize); + $pdf->SetFont('Helvetica', '', 36.0); + $pdf->SetXY(72.0, 72.0); + # TODO: Bad for internationalization. + $pdf->Cell(0, .25, "Legends"); + $legend_images = getLegendImages($mapbook, $print_info); + + $pdf_legend_i = 0; + $legend_x = 36; + $legend_y = 100; + $legend_w = 0; + foreach($legend_images as $legend_image) { + $legend_filename = $tempDir.$uniqueId.'_pdf_legend'.$pdf_legend_i.'.jpg'; + # write the file out as a jpeg + imagejpeg($legend_image, $legend_filename); + # get the size of the legend for placement. + $legend_size = getimagesize($legend_filename); + # check that the legend image is valid. + if($legend_size[0] > 0 and $legend_size[1] > 0) { + # place the new legend + $pdf->Image($legend_filename, $legend_x, $legend_y, $legend_size[0], $legend_size[1]); + + # move the legend down the page. + $legend_y += $legend_size[1]; + + # track the widest legend image. + $legend_w = max($legend_size[0], $legend_w); + + # when the legend's height rolls over, go back to the top, + # and shift over to the right. + if($legend_y > 7*72 ) { + $legend_y = 100; + $legend_x += $legend_w + 10; + # the width is only needed on a column-by-column basis. + $legend_w = 0; + } + } + $pdf_legend_i += 1; + } # end legends loop + } # end render legends + $pdf->Output($tempDir.$uniqueId.'.pdf'); printf('Download PDF
', $uniqueId); diff --git a/php/print_util.php b/php/print_util.php index b056064..2ca40cd 100644 --- a/php/print_util.php +++ b/php/print_util.php @@ -195,6 +195,27 @@ function getAgsImage($layer, $mapW, $mapH, $extent, $debug=false) { return getImage(normalizeURL($url), $debug); } +function getLegendImages($mapbook, $layers_json, $debug=false) { + global $CONFIGURATION; + + $mapserverUrl = $CONFIGURATION['mapserver_url']; + + + $legend_images = []; + for($i = 0; $i < sizeof($layers_json); $i++) { + $legends = $layers_json[$i]['legends']; + if(isset($legends)) { + foreach($legends as $url) { + $normal_url = normalizeURL($url); + $legend_images[] = getImage($normal_url, $debug); + error_log($normal_url); + } + } + } + return $legend_images; + +} + function renderImage($mapbook, $layers_json, $mapImageWidth, $mapImageHeight, $extent, $debug=false) { global $CONFIGURATION;