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. STM32 定时器详细篇(基于HAL库)

    l  16位的向上.向下.向上/向下(中心对齐)计数模式,支持自动重装载 l  16位的预分频器 l  每个定时器都有多个独立通道,每个通道可用于 *  输入捕获 *  输出比较 *  PWM输出 * ...

  2. windows.open、 window.location.href

    windows.open("URL","窗口名称","窗口外观设定");打开新窗口,window对象的方法 不一定打开新窗口,只要有窗口的名 ...

  3. (25)Vim 1

    1.安装Vim CentOS 系统中,使用如下命令即可安装 Vim: yum install vim 需要注意的是,此命令运行时,有时需要手动确认 [y/n] 遇到此情况,选择 "y&quo ...

  4. 学会lambda表达式,能让你少敲1000行代码!

    01.什么是 lambda 表达式 1. 函数式接口 在聊起 lambda 表达式之前,我们不得不提起函数式接口:一个接口只包含唯一的方法,那么它就是函数式接口.例如: public class La ...

  5. jvm系列二内存结构

    二.内存结构 整体架构 1.程序计数器 作用 用于保存JVM中下一条所要执行的指令的地址 特点 线程私有 CPU会为每个线程分配时间片,当当前线程的时间片使用完以后,CPU就会去执行另一个线程中的代码 ...

  6. docker(9)Dockerfile制作镜像

    前言 如果我们已经安装了一个python3的环境,如果另一台机器也需要安装同样的环境又要敲一遍,很麻烦,这里可以配置Dockerfile文件,让其自动安装,类似shell脚本 Dockerfile编写 ...

  7. 2019 China Collegiate Programming Contest Qinhuangdao Onsite F. Forest Program(DFS计算图中所有环的长度)

    题目链接:https://codeforces.com/gym/102361/problem/F 题意 有 \(n\) 个点和 \(m\) 条边,每条边属于 \(0\) 或 \(1\) 个环,问去掉一 ...

  8. Codeforces Round #673 (Div. 2) B. Two Arrays(数学)

    题目链接:https://codeforces.com/contest/1417/problem/B 题意 定义 $f(a)$ 为数组 $a$ 中满足: $i < j$ $a_i + a_j = ...

  9. Codeforces Round #697 (Div. 3) G. Strange Beauty (DP,数学)

    题意:给你一组数,问你最少删去多少数,使得剩下的数,每个数都能整除数组中其它某个数或被数组中其它某个数整除. 题解:我们直接枚举所有因子,\(dp[i]\)表示\(i\)在数组中所含的最大因子数(当我 ...

  10. hdu 5316 Magician 线段树维护最大值

    题目链接:Magician 题意: 给你一个长度为n的序列v,你需要对这个序列进行m次操作,操作一共有两种,输入格式为 type a b 1.如果type==0,你就需要输出[a,b]区间内的美丽序列 ...