Use html2canvas instead of dom-to-image
This commit is contained in:
@@ -5,11 +5,11 @@
|
|||||||
"common": false,
|
"common": false,
|
||||||
"createMapboxStreetsV6Style": false,
|
"createMapboxStreetsV6Style": false,
|
||||||
"d3": false,
|
"d3": false,
|
||||||
"domtoimage": false,
|
"html2canvas": false,
|
||||||
"geojsonvt": false,
|
"geojsonvt": false,
|
||||||
"gifler": false,
|
"gifler": false,
|
||||||
"GyroNorm": false,
|
"GyroNorm": false,
|
||||||
"jsPDF": false,
|
"jspdf": false,
|
||||||
"jsts": false,
|
"jsts": false,
|
||||||
"JSZip": false,
|
"JSZip": false,
|
||||||
"mapboxgl": false,
|
"mapboxgl": false,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ docs: >
|
|||||||
Example of exporting a map as a PDF using the <a href="https://github.com/MrRio/jsPDF" target="_blank">jsPDF</a> library.
|
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"
|
tags: "export, pdf, openstreetmap"
|
||||||
resources:
|
resources:
|
||||||
- https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js
|
- https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js
|
||||||
---
|
---
|
||||||
<div class="row-fluid">
|
<div class="row-fluid">
|
||||||
<div class="span12">
|
<div class="span12">
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ exportButton.addEventListener(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
const pdf = new jsPDF('landscape', undefined, format);
|
const pdf = new jspdf.jsPDF('landscape', undefined, format);
|
||||||
pdf.addImage(
|
pdf.addImage(
|
||||||
mapCanvas.toDataURL('image/jpeg'),
|
mapCanvas.toDataURL('image/jpeg'),
|
||||||
'JPEG',
|
'JPEG',
|
||||||
|
|||||||
@@ -7,12 +7,11 @@ docs: >
|
|||||||
The print is exported as a PDF using the <a href="https://github.com/MrRio/jsPDF" target="_blank">jsPDF</a> library.
|
The print is exported as a PDF using the <a href="https://github.com/MrRio/jsPDF" target="_blank">jsPDF</a> library.
|
||||||
Unlike the <a href="export-pdf.html">Export PDF example</a> the on screen map is only used to set the center and rotation.
|
Unlike the <a href="export-pdf.html">Export PDF example</a> the on screen map is only used to set the center and rotation.
|
||||||
The extent printed depends on the scale and page size. To print the scale bar and attributions the example uses the
|
The extent printed depends on the scale and page size. To print the scale bar and attributions the example uses the
|
||||||
<a href="https://github.com/1904labs/dom-to-image-more" target="_blank">dom-to-image-more</a> library. Due to browser
|
<a href="https://html2canvas.hertzen.com/" target="_blank">html2canvas</a> library.
|
||||||
limitations and restrictions <b>Internet Explorer and Safari are not supported</b>.
|
|
||||||
tags: "print, printing, scale, scaleline, export, pdf"
|
tags: "print, printing, scale, scaleline, export, pdf"
|
||||||
resources:
|
resources:
|
||||||
- https://unpkg.com/dom-to-image-more@2.8.0/dist/dom-to-image-more.min.js
|
- https://html2canvas.hertzen.com/dist/html2canvas.min.js
|
||||||
- https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js
|
- https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js
|
||||||
---
|
---
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<div id="map" class="map"></div>
|
<div id="map" class="map"></div>
|
||||||
|
|||||||
@@ -68,12 +68,13 @@ const dims = {
|
|||||||
a5: [210, 148],
|
a5: [210, 148],
|
||||||
};
|
};
|
||||||
|
|
||||||
// export options for html-to-image.
|
// export options for html2canvase.
|
||||||
// See: https://github.com/bubkoo/html-to-image#options
|
// See: https://html2canvas.hertzen.com/configuration
|
||||||
const exportOptions = {
|
const exportOptions = {
|
||||||
filter: function (element) {
|
useCORS: true,
|
||||||
|
ignoreElements: function (element) {
|
||||||
const className = element.className || '';
|
const className = element.className || '';
|
||||||
return (
|
return !(
|
||||||
className.indexOf('ol-control') === -1 ||
|
className.indexOf('ol-control') === -1 ||
|
||||||
className.indexOf('ol-scale') > -1 ||
|
className.indexOf('ol-scale') > -1 ||
|
||||||
(className.indexOf('ol-attribution') > -1 &&
|
(className.indexOf('ol-attribution') > -1 &&
|
||||||
@@ -108,11 +109,16 @@ exportButton.addEventListener(
|
|||||||
map.once('rendercomplete', function () {
|
map.once('rendercomplete', function () {
|
||||||
exportOptions.width = width;
|
exportOptions.width = width;
|
||||||
exportOptions.height = height;
|
exportOptions.height = height;
|
||||||
domtoimage
|
html2canvas(map.getViewport(), exportOptions).then(function (canvas) {
|
||||||
.toJpeg(map.getViewport(), exportOptions)
|
const pdf = new jspdf.jsPDF('landscape', undefined, format);
|
||||||
.then(function (dataUrl) {
|
pdf.addImage(
|
||||||
const pdf = new jsPDF('landscape', undefined, format);
|
canvas.toDataURL('image/jpeg'),
|
||||||
pdf.addImage(dataUrl, 'JPEG', 0, 0, dim[0], dim[1]);
|
'JPEG',
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
dim[0],
|
||||||
|
dim[1]
|
||||||
|
);
|
||||||
pdf.save('map.pdf');
|
pdf.save('map.pdf');
|
||||||
// Reset original map size
|
// Reset original map size
|
||||||
scaleLine.setDpi();
|
scaleLine.setDpi();
|
||||||
|
|||||||
Reference in New Issue
Block a user