Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

Commit

Permalink
Added legend support for PDF printing, refs: geomoose/geomoose#99
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Little committed Dec 21, 2015
1 parent f63c0e9 commit 55e1a47
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
52 changes: 51 additions & 1 deletion php/print.php
@@ -1,5 +1,5 @@
<?php
/*Copyright (c) 2009, Dan "Ducky" Little & GeoMOOSE.org
/*Copyright (c) 2009-2015, Dan "Ducky" Little & GeoMOOSE.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -55,6 +55,13 @@
$units = 'm';
}

$renderLegends = $_REQUEST['legends'];
if(isset($renderLegends)) {
$renderLegends = parseBoolean($renderLegends);
} else {
$renderLegends = true;
}

$template_path = '../../conf/print/';
$template = $_REQUEST['template'];
# some sanitization...
Expand Down Expand Up @@ -255,6 +262,49 @@
}


if($renderLegends) {
# put the legends on a second page.
$pdf->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('<a target="_blank" href="php/download.php?id=%s&ext=pdf">Download PDF</a><br/>', $uniqueId);
Expand Down
21 changes: 21 additions & 0 deletions php/print_util.php
Expand Up @@ -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;

Expand Down

0 comments on commit 55e1a47

Please sign in to comment.