More compatible way of exporting a map as pdf
This commit is contained in:
@@ -4,8 +4,6 @@ import WKT from '../src/ol/format/WKT.js';
|
|||||||
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
||||||
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
|
import {OSM, Vector as VectorSource} from '../src/ol/source.js';
|
||||||
|
|
||||||
import {toJpeg} from 'html-to-image';
|
|
||||||
|
|
||||||
const raster = new TileLayer({
|
const raster = new TileLayer({
|
||||||
source: new OSM()
|
source: new OSM()
|
||||||
});
|
});
|
||||||
@@ -44,14 +42,6 @@ const dims = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// export options for html-to-image.
|
|
||||||
// See: https://github.com/bubkoo/html-to-image#options
|
|
||||||
const exportOptions = {
|
|
||||||
filter: function(element) {
|
|
||||||
return element.className.indexOf('ol-control') === -1;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const exportButton = document.getElementById('export-pdf');
|
const exportButton = document.getElementById('export-pdf');
|
||||||
|
|
||||||
exportButton.addEventListener('click', function() {
|
exportButton.addEventListener('click', function() {
|
||||||
@@ -68,18 +58,28 @@ exportButton.addEventListener('click', function() {
|
|||||||
const viewResolution = map.getView().getResolution();
|
const viewResolution = map.getView().getResolution();
|
||||||
|
|
||||||
map.once('rendercomplete', function() {
|
map.once('rendercomplete', function() {
|
||||||
exportOptions.width = width;
|
const mapCanvas = document.createElement('canvas');
|
||||||
exportOptions.height = height;
|
mapCanvas.width = width;
|
||||||
toJpeg(map.getViewport(), exportOptions).then(function(dataUrl) {
|
mapCanvas.height = height;
|
||||||
const pdf = new jsPDF('landscape', undefined, format);
|
const mapContext = mapCanvas.getContext('2d');
|
||||||
pdf.addImage(dataUrl, 'JPEG', 0, 0, dim[0], dim[1]);
|
document.querySelectorAll('.ol-layer canvas').forEach(function(canvas) {
|
||||||
pdf.save('map.pdf');
|
if (canvas.width > 0) {
|
||||||
// Reset original map size
|
const transform = canvas.style.transform;
|
||||||
map.setSize(size);
|
// Get the transform parameters from the style's transform matrix
|
||||||
map.getView().setResolution(viewResolution);
|
const matrix = transform.match(/^matrix\(([^\(]*)\)$/)[1].split(',').map(Number);
|
||||||
exportButton.disabled = false;
|
// Apply the transform to the export map context
|
||||||
document.body.style.cursor = 'auto';
|
CanvasRenderingContext2D.prototype.setTransform.apply(mapContext, matrix);
|
||||||
|
mapContext.drawImage(canvas, 0, 0);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
const pdf = new jsPDF('landscape', undefined, format);
|
||||||
|
pdf.addImage(mapCanvas.toDataURL('image/jpeg'), 'JPEG', 0, 0, dim[0], dim[1]);
|
||||||
|
pdf.save('map.pdf');
|
||||||
|
// Reset original map size
|
||||||
|
map.setSize(size);
|
||||||
|
map.getView().setResolution(viewResolution);
|
||||||
|
exportButton.disabled = false;
|
||||||
|
document.body.style.cursor = 'auto';
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set print size
|
// Set print size
|
||||||
|
|||||||
Reference in New Issue
Block a user