【问题标题】:Leaflet data visualization framework export as image传单数据可视化框架导出为图像
【发布时间】:2016-10-07 01:37:26
【问题描述】:

我正在使用 Leaflet 和 Leaflet data visualization framework 在上面绘制地图和图表。

现在我正在尝试将map with charts 保存为 PNG 图像,但只能成功导出一层 - 地图或图表。

这可能是因为 .leaflet-overlay-pane 中的图表是使用 SVG 元素绘制的,而 .leaflet-tile-pane 中的地图是使用 DIV/IMG 元素绘制的。

发现相关问题:
1) leaflet+canvg+html2canvas = MyImage.png
2)export to image leaflet map with SVG markers in javascript

如何处理?有什么方法可以“展平” SVG 和 DIV/IMG/Canvas 元素以导出为图像?还是应该有别的办法?

谢谢!

【问题讨论】:

    标签: javascript leaflet angular-leaflet-directive


    【解决方案1】:

    使用 dom-to-image 库

    您可以使用dom to image library 将传单转换为 png 图像。

    请看下面的例子:

    var mymap = L.map('my-node').setView([51.505, -0.09], 13);
    
    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
                '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
                'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
            id: 'mapbox/streets-v11'
        }).addTo(mymap);
    
    var node = document.getElementById('my-node');
    var btn = document.getElementById('foo');
    
    // node.innerHTML = "I'm an image now."
    
    btn.onclick = function() {
    
      domtoimage.toBlob(document.getElementById('my-node'))
        .then(function(blob) {
          window.saveAs(blob, 'my-node.png');
        });
    }
    #my-node {
      height: 180px;
    }
    
    .button {
      padding: 4px;
      margin-bottom: 10px;
    }
    <script src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
    
    <script src="https://cdn.bootcss.com/dom-to-image/2.6.0/dom-to-image.min.js"></script>
    
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
       integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
       crossorigin=""/>
    
     <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
       integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
       crossorigin=""></script>
    
    <button id="foo" class="button">download img</button>
    
    <div id="my-node">
      You will get a image downloaded.
    </div>

    【讨论】:

      【解决方案2】:

      leaflet-image 不能光栅化 html。我认为你最好的选择是让一些服务器端脚本根据请求生成页面的屏幕截图并将它们返回给浏览器。

      我使用 wkhtmltopdf 将传单地图转换为 PDF,这需要一些调整才能正确。

      【讨论】:

      • 为什么leaflet-image 不能重新格式化html?我没有使用L_PREFER_CANVAS = true;,我在代码 IMG 标记中看到。此外,我可以在leafletImage(map, callback) 函数中删除对map._pathRoot 的检查并创建leaflet/leaflet-dvf 的图像,但没有leaflet-dvf svg 元素(图表)。
      • 引用leaflet-image README.md:“这个库不会光栅化HTML,因为浏览器不能光栅化HTML。因此,L.divIcon 和其他基于HTML 的地图功能,如缩放控件或图例, 不包含在输出中,因为它们是 HTML。” Leaflet-image 仅适用于 L_PREFER_CANVAS = true;或它的leaflet-1.0 等效项
      • 奇怪,因为我让它在没有L_PREFER_CANVAS 的情况下在0.7.7 上工作。现在我正在尝试制作两个PNG。一个来自leaflet-image,另一个来自 SVG,并在服务器上组合。问题是这个 SVG(来自leaflet-dvf)的尺寸不同(更大)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 2013-06-23
      • 2014-12-27
      相关资源
      最近更新 更多