how to export svg form web page in js

https://stackoverflow.com/questions/2483919/how-to-save-svg-canvas-to-local-filesystem

1. server return


2. base64 encode

// This example was created using Protovis & jQuery
// Base64 provided by http://www.webtoolkit.info/javascript-base64.html
// Modern web browsers have a builtin function to this as well 'btoa'
function encode_as_img_and_link(){
// Add some critical information
$("svg").attr({ version: '1.1' , xmlns:"http://www.w3.org/2000/svg"}); var svg = $("#chart-canvas").html();
var b64 = Base64.encode(svg); // or use btoa if supported // Works in recent Webkit(Chrome)
$("body").append($("<img src='data:image/svg+xml;base64,\n"+b64+"' alt='file.svg'/>")); // Works in Firefox 3.6 and Webit and possibly any browser which supports the data-uri
$("body").append($("<a href-lang='image/svg+xml' href='data:image/svg+xml;base64,\n"+b64+"' title='file.svg'>Download</a>"));
}

base64 bug

中文 bug ???


// Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.


const getToday = (symbol = `-`) => {
const date = new Date();
const dd = String(date.getDate()).padStart(2, '0');
// Month January is 0!
const mm = String(date.getMonth() + 1).padStart(2, '0');
const yyyy = date.getFullYear();
// format
const today = `${yyyy}${symbol}${mm}${symbol}${dd}`;
// const today = mm + '/' + dd + '/' + yyyy;
return today;
} const getTodayTimestamp = (symbol = `-`) => {
const date = new Date();
const dd = String(date.getDate()).padStart(2, '0');
// Month January is 0!
const mm = String(date.getMonth() + 1).padStart(2, '0');
const yyyy = date.getFullYear();
const timestamp = date.getTime();
// format
const todayTimestamp = `${yyyy}${symbol}${mm}${symbol}${dd}${symbol}${timestamp}`;
return todayTimestamp;
} const svgBase64StringConvert = () => {
const body = document.querySelector(`body`);
// svg uuid
const svg = document.querySelector(`svg`);
const html = svg.parentElement.innerHTML;
// let html = svg.parentNode.innerHTML;
// let html = svg.innerHTML();
// html = `
// <svg width="100%" height="100%" viewBox="0 0 1000 1000">
// ${html}
// </svg>
// `;
const base64String = btoa(html);
const date = new Date();
const time = date.getFullYear() + date.getMonth() + 1 + date.getDate();
const timestamp = new Date().getTime();
// const timestamp = getTodayTimestamp();
const img = `
<img
src="data:image/svg+xml;base64, ${base64String}"
alt="live-map-${timestamp}.svg"
download="live-map-${timestamp}.svg"
/>
`;
const alink = `
<a
href="data:image/svg+xml;base64, ${base64String}"
alt="live-map-${timestamp}.svg"
download="live-map-${timestamp}.svg"
data-class="visibility: none;"
/>
`;
body.insertAdjacentHTML(alink);
alink.click();
// remove alink
} const svgToBase64String = () => {
const body = document.querySelector(`body`);
// svg uuid
const svg = document.querySelector(`svg`);
const html = svg.parentElement.innerHTML;
const base64String = btoa(html);
const date = new Date();
const time = date.getFullYear() + date.getMonth() + 1 + date.getDate();
const timestamp = new Date().getTime();
const alink = `
<a
href="data:image/svg+xml;base64, ${base64String}"
alt="live-map-${timestamp}.svg"
download="live-map-${timestamp}.svg"
data-class="visibility: none;"
/>
`;
body.insertAdjacentHTML(`beforeend`, alink);
alink.click();
// remove alink
}

https://stackoverflow.com/questions/23218174/how-do-i-save-export-an-svg-file-after-creating-an-svg-with-d3-js-ie-safari-an/23218877

3. XMLSerializer & serializeToString


//get svg element.
var svg = document.getElementById("svg"); //get svg source.
var serializer = new XMLSerializer();
var source = serializer.serializeToString(svg); //add name spaces.
if(!source.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/)){
source = source.replace(/^<svg/, '<svg xmlns="http://www.w3.org/2000/svg"');
}
if(!source.match(/^<svg[^>]+"http\:\/\/www\.w3\.org\/1999\/xlink"/)){
source = source.replace(/^<svg/, '<svg xmlns:xlink="http://www.w3.org/1999/xlink"');
} //add xml declaration
source = '<?xml version="1.0" standalone="no"?>\r\n' + source; //convert svg source to URI data scheme.
var url = "data:image/svg+xml;charset=utf-8,"+encodeURIComponent(source); //set url value to a element's href attribute.
document.getElementById("link").href = url;
//you can download svg file by right click menu.

solution

svg.outerHTML & svg.parentElement.innerHTML


const getTodayTimestamp = (symbol = `-`) => {
const date = new Date();
const dd = String(date.getDate()).padStart(2, '0');
// Month January is 0!
const mm = String(date.getMonth() + 1).padStart(2, '0');
const yyyy = date.getFullYear();
const timestamp = date.getTime();
// format
const todayTimestamp = `${yyyy}${symbol}${mm}${symbol}${dd}${symbol}${timestamp}`;
return todayTimestamp;
} const autoSVGDownload = (uuid = ``) =>{
const timestamp = getTodayTimestamp();
const filename = `SVG 现场图-${timestamp}.svg`;
// const filename = `live-map-${timestamp}.svg`;
// uuid
const svg = document.querySelector(`svg`);
// const html = svg.parentElement.innerHTML;
const html = svg.outerHTML;
// xml namespace, support browser open preview
const xml = `<?xml version="1.0" encoding="UTF-8"?>\n${html}`;
const element = document.createElement('a');
element.setAttribute('href', 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(xml));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
} export {
getTodayTimestamp,
autoSVGDownload,
}; export default autoSVGDownload;

how to export svg form web page in js的更多相关文章

  1. VML/SVG在Web开发中一些常见的框架

    1.借鉴自: http://www.codefans.net/soft/3061.shtml 来源于网上. flowchart.js  http://adrai.github.io/flowchart ...

  2. [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework

    1. Background Though it's always difficult to give child a perfect name, parent never give up trying ...

  3. Snap.svg – 现代 Web 开发必备的 JavaScript SVG 库

    SVG 是一种很好的 Web 技术方案,可以用来创建互动,在任何大小的屏幕上都会很好看.与分辨率无关的矢量图形.而这里推荐的 Snap.svg 这个 JavaScript 可以让你像 jQuery 操 ...

  4. 解读Web Page Diagnostics网页细分图

    解读Web Page Diagnostics网页细分图 http://blog.sina.com.cn/s/blog_62b8fc330100red5.html Web Page Diagnostic ...

  5. 网页细分图结果分析(Web Page Diagnostics)

    Discuz开源论坛网页细分图结果分析(Web Page Diagnostics) 续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场 ...

  6. Atitit.web三大编程模型 Web Page Web Forms 和 MVC

    Atitit.web三大编程模型 Web Page    Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2.  ...

  7. [转]Calling Web Service Functions Asynchronously from a Web Page 异步调用WebServices

    本文转自:http://www.codeproject.com/Articles/70441/Calling-Web-Service-Functions-Asynchronously-from Ove ...

  8. Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop

    In this tutorial, you will learn how to import a table of data from a Web page and create a report t ...

  9. Android WebView常见问题的解决方案总结----例如Web page not available

    之前android虚拟机一直都可以直接联网,今天写了一个WebView之后,突然报出了Web page not available的错误,但是查看虚拟机自带的浏览器,是可以上网的,所以检查还是代码的问 ...

随机推荐

  1. 基于nginx的频率控制方案思考和实践

    基于nginx的频率控制方案思考 标签: 频率控制 nginx 背景 nginx其实有自带的limit_req和limit_conn模块,不过它们需要在配置文件中进行配置才能发挥作用,每次有频控策略的 ...

  2. Java 从数组来看值传递和引用传递

    从数组来看值传递和引用传递 惯例先看一段代码 public class DemoCollection14 { public static void main(String[] args) { Stri ...

  3. JSP标签使用的代码记录——《%= %》(神奇的CSDN为啥标题不让打英文的尖括号)

    关于JSP的一些标签,在用到的时候有些生疏,就去找了找资源重新温习了一下. 附上两个JSP<%= %>标签的博客,同时也记录当前项目里用到的方法. jsp页面中<%@ %>.& ...

  4. java架构《并发线程高级篇二》

    本章主要记录讲解并发线程的线程池.使用Executor框架自定义线程池. 自定义线程池使用Queue队列所表示出来的形式: 1 ArrayBlockingQueue<Runnable>(3 ...

  5. 从官方文档中探索MySQL分页的几种方式及分页优化

    概览 相比于Oracle,SQL Server 等数据库,MySQL分页的方式简单得多了,官方自带了分页语法 limit 语句: select * from test_t LIMIT {[offset ...

  6. elasticsearch7.8权限控制和规划

    由于在版本7开始,x-pack可以免费使用了,但是权限控制免费的不够细,但是控制到索引级别都基本够用了.付费的可以体验更细致的权限控制.本文的基础是已经有了es集群的基础上进行的. 官网:https: ...

  7. 一次小模块的使用过程-LC12S无线模块介绍

    前言: 最近帮人做了个小设备,使用了无线模块.触摸芯片,主要功能就是把触摸按键的信号无线传到控制继电器输出,MCU是STM8系列的芯片,其中使用过程中调试无线模块LC21S觉得挺好用的,就写了这篇文章 ...

  8. Kubernetes-5-2:Harbor仓库的几种高可用方案与搭建

    高可用Harbor搭建 思路及介绍 Harbor官方有推出主从架构和双主架构来实现Harbor的高可用及数据备份.   一.主从架构:  说白了,就是往一台Harbor仓库中push镜像,然后再通过这 ...

  9. CSAPP_AttackLab实验报告

    目录 屏幕截图 考察内容 各题答案 level1 level2 level3 level4 level5 解题思路 level1 任务 思路 level2 任务 思路 level3 任务 思路 lev ...

  10. Codeforces Round #540 (Div. 3) D2. Coffee and Coursework (Hard Version) (二分,贪心)

    题意:有\(n\)个数,每次可以选\(k(1\le k\le n)\)个数,并且得到\(a_1+max(0,a_2-1)+max(0,a_3-2)+...+max(0,a_k-k+1)\)的贡献,问最 ...