From 7cdfc33d15c88ba843254f6b5525029f8ee78c80 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Thu, 4 Jul 2019 10:07:31 +0100 Subject: [PATCH] Export PDF example extent corrections Fixes #9460 and #9665 Replace calculateExtent and fit with getResolution and setResolution as the former do not produce the desired result when the view is rotated. Export the viewport instead of the map div (which doesn't resize) and specify the width and height in the export options This is a minimal fix for the two issues. Further enhancement including a fallback to export layer canvases where the browser doesn't support html-to-image could be done separately if necessary. --- examples/export-pdf.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/export-pdf.js b/examples/export-pdf.js index c1e8c256a2..b37d23cc5e 100644 --- a/examples/export-pdf.js +++ b/examples/export-pdf.js @@ -65,18 +65,18 @@ exportButton.addEventListener('click', function() { const width = Math.round(dim[0] * resolution / 25.4); const height = Math.round(dim[1] * resolution / 25.4); const size = map.getSize(); - const extent = map.getView().calculateExtent(size); + const viewResolution = map.getView().getResolution(); map.once('rendercomplete', function() { - toJpeg(map.getTargetElement(), exportOptions).then(function(dataUrl) { + exportOptions.width = width; + exportOptions.height = height; + toJpeg(map.getViewport(), exportOptions).then(function(dataUrl) { const pdf = new jsPDF('landscape', undefined, format); pdf.addImage(dataUrl, 'JPEG', 0, 0, dim[0], dim[1]); pdf.save('map.pdf'); // Reset original map size map.setSize(size); - map.getView().fit(extent, { - size: size - }); + map.getView().setResolution(viewResolution); exportButton.disabled = false; document.body.style.cursor = 'auto'; }); @@ -85,6 +85,7 @@ exportButton.addEventListener('click', function() { // Set print size const printSize = [width, height]; map.setSize(printSize); - map.getView().fit(extent, {size: printSize}); + const scaling = Math.min(width / size[0], height / size[1]); + map.getView().setResolution(viewResolution / scaling); }, false);