@@ -18,6 +18,7 @@
|
|||||||
"externs/example.js",
|
"externs/example.js",
|
||||||
"externs/fastclick.js",
|
"externs/fastclick.js",
|
||||||
"externs/geojson.js",
|
"externs/geojson.js",
|
||||||
|
"externs/jspdf.js",
|
||||||
"externs/jquery-1.9.js",
|
"externs/jquery-1.9.js",
|
||||||
"externs/proj4js.js",
|
"externs/proj4js.js",
|
||||||
"externs/tilejson.js",
|
"externs/tilejson.js",
|
||||||
|
|||||||
3
examples/export-pdf.css
Normal file
3
examples/export-pdf.css
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.map {
|
||||||
|
max-width: 566px;
|
||||||
|
}
|
||||||
33
examples/export-pdf.html
Normal file
33
examples/export-pdf.html
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
---
|
||||||
|
layout: example.html
|
||||||
|
title: Export PDF example
|
||||||
|
shortdesc: Example of exporting a map as a PDF.
|
||||||
|
docs: >
|
||||||
|
Example of exporting a map as a PDF using the <a href="https://github.com/MrRio/jsPDF" target="_blank">jsPDF</a> library.
|
||||||
|
tags: "export, pdf, openstreetmap"
|
||||||
|
resources:
|
||||||
|
- http://mrrio.github.io/jsPDF/dist/jspdf.min.js
|
||||||
|
---
|
||||||
|
<div class="row-fluid">
|
||||||
|
<div class="span12">
|
||||||
|
<div id="map" class="map"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<form class="form">
|
||||||
|
<label>Page size </label>
|
||||||
|
<select id="format">
|
||||||
|
<option value="a0">A0 (slow)</option>
|
||||||
|
<option value="a1">A1</option>
|
||||||
|
<option value="a2">A2</option>
|
||||||
|
<option value="a3">A3</option>
|
||||||
|
<option value="a4" selected>A4</option>
|
||||||
|
<option value="a5">A5 (fast)</option>
|
||||||
|
</select>
|
||||||
|
<label>Resolution </label>
|
||||||
|
<select id="resolution">
|
||||||
|
<option value="72">72 dpi (fast)</option>
|
||||||
|
<option value="150">150 dpi</option>
|
||||||
|
<option value="300">300 dpi (slow)</option>
|
||||||
|
</select>
|
||||||
|
</form>
|
||||||
|
<button id="export-pdf">Export PDF</button>
|
||||||
109
examples/export-pdf.js
Normal file
109
examples/export-pdf.js
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
goog.require('ol.Map');
|
||||||
|
goog.require('ol.View');
|
||||||
|
goog.require('ol.control');
|
||||||
|
goog.require('ol.format.WKT');
|
||||||
|
goog.require('ol.layer.Tile');
|
||||||
|
goog.require('ol.layer.Vector');
|
||||||
|
goog.require('ol.source.OSM');
|
||||||
|
goog.require('ol.source.Vector');
|
||||||
|
|
||||||
|
var raster = new ol.layer.Tile({
|
||||||
|
source: new ol.source.OSM()
|
||||||
|
});
|
||||||
|
|
||||||
|
var format = new ol.format.WKT();
|
||||||
|
var feature = format.readFeature(
|
||||||
|
'POLYGON((10.689697265625 -25.0927734375, 34.595947265625 ' +
|
||||||
|
'-20.1708984375, 38.814697265625 -35.6396484375, 13.502197265625 ' +
|
||||||
|
'-39.1552734375, 10.689697265625 -25.0927734375))');
|
||||||
|
feature.getGeometry().transform('EPSG:4326', 'EPSG:3857');
|
||||||
|
|
||||||
|
var vector = new ol.layer.Vector({
|
||||||
|
source: new ol.source.Vector({
|
||||||
|
features: [feature]
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
var map = new ol.Map({
|
||||||
|
layers: [raster, vector],
|
||||||
|
target: 'map',
|
||||||
|
controls: ol.control.defaults({
|
||||||
|
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
|
||||||
|
collapsible: false
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
view: new ol.View({
|
||||||
|
center: [0, 0],
|
||||||
|
zoom: 2
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
var dims = {
|
||||||
|
a0: [1189, 841],
|
||||||
|
a1: [841, 594],
|
||||||
|
a2: [594, 420],
|
||||||
|
a3: [420, 297],
|
||||||
|
a4: [297, 210],
|
||||||
|
a5: [210, 148]
|
||||||
|
};
|
||||||
|
|
||||||
|
var loading = 0;
|
||||||
|
var loaded = 0;
|
||||||
|
|
||||||
|
var exportButton = document.getElementById('export-pdf');
|
||||||
|
|
||||||
|
exportButton.addEventListener('click', function(e) {
|
||||||
|
|
||||||
|
exportButton.disabled = true;
|
||||||
|
document.body.style.cursor = 'progress';
|
||||||
|
|
||||||
|
var format = document.getElementById('format').value;
|
||||||
|
var resolution = document.getElementById('resolution').value;
|
||||||
|
var dim = dims[format];
|
||||||
|
var width = Math.round(dim[0] * resolution / 25.4);
|
||||||
|
var height = Math.round(dim[1] * resolution / 25.4);
|
||||||
|
var size = /** @type {ol.Size} */ (map.getSize());
|
||||||
|
var extent = map.getView().calculateExtent(size);
|
||||||
|
|
||||||
|
var source = raster.getSource();
|
||||||
|
|
||||||
|
var tileLoadStart = function() {
|
||||||
|
++loading;
|
||||||
|
};
|
||||||
|
|
||||||
|
var tileLoadEnd = function(callback) {
|
||||||
|
++loaded;
|
||||||
|
if (loading === loaded) {
|
||||||
|
var canvas = this;
|
||||||
|
window.setTimeout(function() {
|
||||||
|
loading = 0;
|
||||||
|
loaded = 0;
|
||||||
|
var data = canvas.toDataURL('image/png');
|
||||||
|
var pdf = new jsPDF('landscape', undefined, format);
|
||||||
|
pdf.addImage(data, 'JPEG', 0, 0, dim[0], dim[1]);
|
||||||
|
pdf.save('map.pdf');
|
||||||
|
source.un('tileloadstart', tileLoadStart);
|
||||||
|
source.un('tileloadend', tileLoadEnd, canvas);
|
||||||
|
source.un('tileloaderror', tileLoadEnd, canvas);
|
||||||
|
map.setSize(size);
|
||||||
|
map.getView().fit(extent, size);
|
||||||
|
map.renderSync();
|
||||||
|
exportButton.disabled = false;
|
||||||
|
document.body.style.cursor = 'auto';
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
map.once('postcompose', function(event) {
|
||||||
|
source.on('tileloadstart', tileLoadStart);
|
||||||
|
source.on('tileloadend', tileLoadEnd, event.context.canvas);
|
||||||
|
source.on('tileloaderror', tileLoadEnd, event.context.canvas);
|
||||||
|
});
|
||||||
|
|
||||||
|
map.setSize([width, height]);
|
||||||
|
map.getView().fit(extent, /** @type {ol.Size} */ (map.getSize()));
|
||||||
|
map.renderSync();
|
||||||
|
|
||||||
|
}, false);
|
||||||
46
externs/jspdf.js
Normal file
46
externs/jspdf.js
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview jsPDF PDF generator.
|
||||||
|
* @see https://github.com/MrRio/jsPDF
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @param {string=} orientation One of `portrait` or `landscape`
|
||||||
|
* (or shortcuts `p` (default), `l`).
|
||||||
|
* @param {string=} unit Measurement unit to be used when coordinates are specified.
|
||||||
|
* One of `pt`, `mm` (default), `cm`, `in`.
|
||||||
|
* @param {string=} format Default: `a4`.
|
||||||
|
* @param {boolean=} compressPdf
|
||||||
|
*/
|
||||||
|
var jsPDF = function(orientation, unit, format, compressPdf) {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} imageData
|
||||||
|
* @param {string} format
|
||||||
|
* @param {number} x
|
||||||
|
* @param {number} y
|
||||||
|
* @param {number} w
|
||||||
|
* @param {number} h
|
||||||
|
* @param {string=} alias
|
||||||
|
* @param {number=} compression
|
||||||
|
* @return {jsPDF}
|
||||||
|
*/
|
||||||
|
jsPDF.prototype.addImage = function(imageData, format, x, y, w, h, alias, compression) {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {jsPDF}
|
||||||
|
*/
|
||||||
|
jsPDF.prototype.autoPrint = function() {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} type
|
||||||
|
* @param {Object=} options
|
||||||
|
* @return {jsPDF}
|
||||||
|
*/
|
||||||
|
jsPDF.prototype.output = function(type, options) {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} filename
|
||||||
|
* @return {jsPDF}
|
||||||
|
*/
|
||||||
|
jsPDF.prototype.save = function(filename) {};
|
||||||
Reference in New Issue
Block a user