OpenLayers6(9):Vue中使用ol-ext插件中的PrintDialog打印控件

0 版本

  • OpenLayers:6.14.1

  • ol-ext:3.2.24(条件:"ol": ">= 5.3.0")

1 相关配置

// 1、安装ol-ext
npm i ol-ext
 
// 2、在main.js中引入样式
import 'ol-ext/dist/ol-ext.min.css';

// 3、安装jspdf用于导出pdf
npm install jspdf --save

// 4、安装FileSaver.js用于导出数据
npm i file-saver


2 使用PrintDialog控件

2.1 构造函数的参数说明

  • @param {string} [options.lang=en] control language, default en
  • @param {string} options.imageType A string indicating the image format, default image/jpeg
  • @param {number} options.quality Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp
  • @param {string} options.orientation Page orientation (landscape/portrait), default guest the best one
  •  @param {boolean} options.immediate force print even if render is not complete,  default false
  • @param {boolean} [options.openWindow=false] open the file in a new window on print
  • @param {boolean} [options.copy=true] add a copy select option
  • @param {boolean} [options.print=true] add a print select option
  • @param {boolean} [options.pdf=true] add a pdf select option
  • @param {function} [options.saveAs] a function to save the image as blob
  •  @param {*} [options.jsPDF] jsPDF object to save map as pdf

2.2 openlayers中使用

import {
    saveAs
} from 'file-saver'
import {
    jsPDF
} from "jspdf";
import OlExtPrintDialog from 'ol-ext/control/PrintDialog'

OlExtMap.prototype.addPrintDialog = () => {
    const printControl = new OlExtPrintDialog({
        lang: "zh",
        pdf: true,
        openWindow: false,
    });
    printControl.setSize('A4');
    this_.mMap.addControl(printControl);
    printControl.on(['print', 'error'], function (e) {
        if (e.image) {
            if (e.pdf) { // 导出PDF
                const pdf = new jsPDF({
                    orientation: e.print.orientation,
                    unit: e.print.unit,
                    format: e.print.size
                });
                pdf.addImage(e.image, 'JPEG', e.print.position[0], e.print.position[0],  e.print.imageWidth, e.print.imageHeight);
                pdf.save(e.print.legend ? 'legend.pdf' : 'map.pdf');
            } else { // 导出图片
                e.canvas.toBlob(function (blob) {
                    const name = (e.print.legend ? 'legend.' : 'map.') + e.imageType.replace('image/', '');
                    saveAs(blob, name);
                }, e.imageType, e.quality);
            }
        } else {
            console.warn('No canvas to export');
        }
    });
}

3 效果

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碰碰qaq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值