import jsPDF from 'jspdf';
const dims = {
a0: [1189, 841],
a1: [841, 594],
a2: [594, 420],
a3: [420, 297],
a4: [297, 210],
a5: [210, 148],
};
export default function downLoadPDF(format, resolution, map) {
const dim = dims[format];
console.log(dim);
const width = Math.round((dim[0] * resolution) / 25.4);
const height = Math.round((dim[1] * resolution) / 25.4);
const size = map.getSize();
const viewResolution = map.getView().getResolution();
console.log(map.getView());
console.log(viewResolution);
console.log(map);
map.once('rendercomplete', function () {
console.log(111);
console.log('34324234');
const mapCanvas = document.createElement('canvas');
mapCanvas.width = width;
mapCanvas.height = height;
const mapContext = mapCanvas.getContext('2d');
Array.prototype.forEach.call(
document.querySelectorAll('.ol-layer canvas'),
function (canvas) {
if (canvas.width > 0) {
const opacity = canvas.parentNode.style.opacity;
mapContext.globalAlpha = opacity === '' ? 1 : Number(opacity);
const transform = canvas.style.transform;
const matrix = transform
.match(/^matrix\(([^(]*)\)$/)[1]
.split(',')
.map(Number);
CanvasRenderingContext2D.prototype.setTransform.apply(
mapContext,
matrix
);
mapContext.drawImage(canvas, 0, 0);
}
}
);
mapContext.globalAlpha = 1;
mapContext.setTransform(1, 0, 0, 1, 0, 0);
const pdf = new jsPDF('landscape', undefined, format);
console.log(mapCanvas);
mapCanvas.toBlob((blob) => {
downloadBlob(blob, 'test.png');
});
pdf.addImage(
mapCanvas.toDataURL('image/jpeg'),
'JPEG',
0,
0,
dim[0],
dim[1]
);
pdf.save('map.pdf');
map.setSize(size);
map.getView().setResolution(viewResolution);
document.body.style.cursor = 'auto';
});
const printSize = [width, height];
map.setSize(printSize);
const scaling = Math.min(width / size[0], height / size[1]);
console.log(scaling);
map.getView().setResolution(viewResolution / scaling);
console.log('222');
}
function downloadBlob(blob, fileName) {
const eleLink = document.createElement('a');
eleLink.download = fileName;
eleLink.style.display = 'none';
eleLink.href = URL.createObjectURL(blob);
document.body.appendChild(eleLink);
eleLink.click();
document.body.removeChild(eleLink);
}