背景
使用openLayers 6.5.0,html2canvas 1.3.3,使用geoserver发布的地图服务。
问题
1、使用html2canvas打印时,地图一片空白,控制台报错’Unable to clone canvas as it is tainted’
2、打印存放在nginx的静态图片(使用img标签引用图片地址)时空白
解决
1、加载地图服务,配置source时添加配置:
crossOrigin: 'anonymous'
2、geoserver或者放geoserver的tomcat需要配置跨域,否则服务会报跨域的错
以上解决问题1
3、nginx设置跨域(图片用nginx代理地址)
# 静态资源
location /访问地址 {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
alias 本地地址;
}
4、html2canvas设置项添加
useCORS: true
坑
不要在html2canvas里配置foreignObjectRendering为true,会导致打印偏移