html2canvas 实现页面转图片并下载
一 前言
最近做了一个周报,从不同的数据表抓取数据,然后展示到前端页面显示。这个过程不难,让我烦恼的是:要把周报的数据导出来,然后打印,打印也必须在一张纸上。想到这里,我整理了一下思绪,我要写几个存储过程,存储过程里有很多复杂的逻辑元素。要做个导出功能,还必须在一张A4纸上打印出来。纳尼?必须在一张纸上打印!然后我翻阅了资料和问了度娘。html2canvas插件可以实现我的需求。一开始我是很高兴的,因为能行。接下来我就发现了一些问题:
1.下载下来的js,运行直接报错,Oh,天呐!
2.有些浏览器根本不支持a标签下载图片。
3.图片没有那么清晰,比较模糊。
4.下载到本地的图片不完全,这个很致命。
兜兜转转,终于搞定了。记录此刻!
二 准备
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/html2canvas.min.js"></script>
<script src="~/Scripts/bluebird.js"></script>
三 Demo/js
@using PlatForm.ERP.Models.Reports
@using PlatForm.ERP.Models.Daily
@model WeekReportSummaryAndPlanModel
@{
var webSiteName = ViewBag.WebSiteName;
var weekName = ViewBag.WeekName;
var operateDayOrWeekReport = ViewBag.OperateDayOrWeekReport as OperateDayOrWeekReportModel;
var storeVisitorFlowRateDayOrWeekReports = ViewBag.StoreVisitorFlowRateDayOrWeekReports as IList<StoreVisitorFlowRateDayOrWeekReportModel>;
var singleProductDayOrWeekReports = ViewBag.SingleProductDayOrWeekReports as IList<SingleProductDayOrWeekReportModel>;
var weekReportSummaryAndPlan = ViewBag.WeekReportSummaryAndPlan as WeekReportSummaryAndPlanModel;
}
<style type="text/css">
td {
border: 1px solid #ccc;
}
</style>
<div style="float:right;"><input type="button" id="btn" class="k-button" value="保存为图片" /></div>
<div id="imgDiv" style="padding-top:30px;">
<div>
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;text-align:center;background-color:white;">
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">店铺运营基础数据</td>
</tr>
<tr>
<td style="font-weight:bolder;">日期</td>
<td style="font-weight:bolder;">成交金额</td>
<td style="font-weight:bolder;">访客数</td>
<td style="font-weight:bolder;">去年访客数</td>
<td style="font-weight:bolder;">访客数同比</td>
<td style="font-weight:bolder;">浏览量</td>
<td style="font-weight:bolder;">去年浏览量</td>
<td style="font-weight:bolder;">浏览量同比</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td style="font-weight:bolder;" rowspan="">@operateDayOrWeekReport.WeekName</td>
<td>@operateDayOrWeekReport.DealAmount</td>
<td>@operateDayOrWeekReport.VisitorCount</td>
<td>@operateDayOrWeekReport.BeforeYearVisitorCount</td>
<td>@operateDayOrWeekReport.VisitorCountYearOnYear</td>
<td>@operateDayOrWeekReport.BrowseVolume</td>
<td>@operateDayOrWeekReport.BeforeYearBrowseVolume</td>
<td>@operateDayOrWeekReport.BrowseVolumeYearOnYear</td>
</tr>
}
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">下单</td>
</tr>
<tr>
<td style="font-weight:bolder;">下单客户数</td>
<td style="font-weight:bolder;">下单单量</td>
<td style="font-weight:bolder;">下单商品件数</td>
<td style="font-weight:bolder;">下单金额</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td>@operateDayOrWeekReport.OrderCustomerCount</td>
<td>@operateDayOrWeekReport.OrderQuantity</td>
<td>@operateDayOrWeekReport.OrderProductCount</td>
<td>@operateDayOrWeekReport.OrderAmount</td>
</tr>
}
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">成交</td>
</tr>
<tr>
<td style="font-weight:bolder;">成交客户数</td>
<td style="font-weight:bolder;">成交单量</td>
<td style="font-weight:bolder;">成交商品件数</td>
<td style="font-weight:bolder;">客单价</td>
<td style="font-weight:bolder;">实际销售件数</td>
<td style="font-weight:bolder;">实际销售金额</td>
<td style="font-weight:bolder;">去年实际销售</td>
<td style="font-weight:bolder;">退货件数</td>
<td style="font-weight:bolder;">退货金额</td>
<td style="font-weight:bolder;">去年退货金额</td>
<td style="font-weight:bolder;">下单转化率</td>
<td style="font-weight:bolder;">成交转化率</td>
<td style="font-weight:bolder;">下单成交转化率</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td>@operateDayOrWeekReport.DealCustomerCount</td>
<td>@operateDayOrWeekReport.DealQuantity</td>
<td>@operateDayOrWeekReport.DealProductCount</td>
<td>@operateDayOrWeekReport.CustomerPrice</td>
<td>@operateDayOrWeekReport.RealSaleCount</td>
<td>@operateDayOrWeekReport.RealSaleAmount</td>
<td>@operateDayOrWeekReport.BeforeYearRealSale</td>
<td>@operateDayOrWeekReport.ReturnCount</td>
<td>@operateDayOrWeekReport.ReturnAmount</td>
<td>@operateDayOrWeekReport.BeforeYearReturn</td>
<td>@operateDayOrWeekReport.OrderConversionRate</td>
<td>@operateDayOrWeekReport.ConversionRate</td>
<td>@operateDayOrWeekReport.OrderDealConversionRate</td>
</tr>
}
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">费用</td>
</tr>
<tr>
<td style="font-weight:bolder;">补单佣金</td>
<td style="font-weight:bolder;">活动</td>
<td style="font-weight:bolder;">快车</td>
<td style="font-weight:bolder;">其他</td>
</tr>
@if (operateDayOrWeekReport != null)
{
<tr>
<td>@operateDayOrWeekReport.SupplementOrderCommission</td>
<td>@operateDayOrWeekReport.Activitie</td>
<td>@operateDayOrWeekReport.KuaiChe</td>
<td>@operateDayOrWeekReport.OtherFee</td>
</tr>
}
</table>
</div>
<div style="padding-top:50px;">
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;text-align:center; background-color:white;">
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">店铺流量</td>
</tr>
<tr>
<td style="font-weight:bolder;">日期</td>
<td style="font-weight:bolder;">来源</td>
<td style="font-weight:bolder;">访客数</td>
<td style="font-weight:bolder;">访客数占比</td>
<td style="font-weight:bolder;">上周访客数</td>
<td style="font-weight:bolder;">上周访客数占比</td>
<td style="font-weight:bolder;">去年同时期访客数</td>
<td style="font-weight:bolder;">去年同时期访客数占比</td>
<td style="font-weight:bolder;">环比(访客数)</td>
<td style="font-weight:bolder;">同比(访客数)</td>
</tr> @if (storeVisitorFlowRateDayOrWeekReports != null)
{
foreach (var item in storeVisitorFlowRateDayOrWeekReports)
{
<tr>
<td style="font-weight:bolder;">@item.WeekName</td>
<td>@item.Source</td>
<td>@item.VisitorCount</td>
<td>@item.VisitorCountProportion</td>
<td>@item.BeforeDayOrWeekVisitorCount</td>
<td>@item.BeforeDayOrWeekVisitorCountProportion</td>
<td>@item.BeforeYearVisitorCount</td>
<td>@item.BeforeYearVisitorCountProportion</td>
<td>@item.Ratio</td>
<td>@item.YearRatio</td>
</tr>
}
} </table>
</div>
<div style="padding-top:50px;">
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;text-align:center; background-color:white;">
<tr>
<td colspan="" style="font-weight:bolder;text-align:center;background-color:#87CEFA;">单品</td>
</tr>
<tr>
<td style="font-weight:bolder;">货号</td>
<td style="font-weight:bolder;">日期</td>
<td style="font-weight:bolder;">实际销售件数</td>
<td style="font-weight:bolder;">实际销售金额</td>
<td style="font-weight:bolder;">退货件数</td>
<td style="font-weight:bolder;">退货金额</td>
<td style="font-weight:bolder;">搜索排名</td>
<td style="font-weight:bolder;">浏览量</td>
<td style="font-weight:bolder;">访客数</td>
<td style="font-weight:bolder;">加购人数</td>
<td style="font-weight:bolder;">加购率</td>
<td style="font-weight:bolder;">下单客户数</td>
<td style="font-weight:bolder;">下单转化率</td>
<td style="font-weight:bolder;">成交客户数</td>
<td style="font-weight:bolder;">成交商品数</td>
<td style="font-weight:bolder;">成交转化率</td>
</tr>
<tr>
@if (singleProductDayOrWeekReports != null)
{
foreach (var item in singleProductDayOrWeekReports)
{
<tr>
<td style="font-weight:bolder;">@item.StyleNo</td>
<td style="font-weight:bolder;">@(item.Time.Value.ToString("yyyy-MM-dd"))</td>
<td>@item.RealSaleCount</td>
<td>@item.RealSaleAmount</td>
<td>@item.ReturnProductCount</td>
<td>@item.ReturnProductAmount</td>
<td>@item.SearchRanking</td>
<td>@item.BrowseVolume</td>
<td>@item.VisitorCount</td>
<td>@item.AddPurchasePeopleCount</td>
<td>@item.AddPurchaseRate</td>
<td>@item.OrderCustomerCount</td>
<td>@item.OrderConversionRate</td>
<td>@item.DealCustomerCount</td>
<td>@item.DealProductCount</td>
<td>@item.DealConversionRate</td>
</tr>
}
}
</table>
</div>
<div style="padding-top:50px;">
<form id="summaryAndPlan">
<table class="zTable" cellpadding="" cellspacing="" style="width:80%;">
<tr>
<td class="adminTitle" style="font-weight:bolder;">
总结:
</td>
<td class="adminData">
<textarea id="Summary" name="Summary" style="width:600px;height:100px;">@(weekReportSummaryAndPlan==null?"":weekReportSummaryAndPlan.Summary.Trim())</textarea>
</td>
</tr>
<tr>
<td class="adminTitle" style="font-weight:bolder;">
计划:
</td>
<td class="adminData">
<textarea id="UpPlan" name="UpPlan" style="width:600px;height:100px;">@(weekReportSummaryAndPlan == null ? "" : weekReportSummaryAndPlan.UpPlan.Trim())</textarea>
</td>
</tr>
<tr>
<td class="adminData" colspan="" style="text-align: center;">
<input type="button" id="confirmPlan" class="k-button" value="确定" />
</td>
</tr>
</table>
</form>
</div>
</div>
<script>
$("#confirmPlan").click(function () {
if (confirm("确定吗?")) {
var summary =$("#@Html.FieldIdFor(x=>x.Summary)").val();
var plan =$("#@Html.FieldIdFor(x=>x.UpPlan)").val();
$.ajax({
url: "@Url.Action("WeekReportSummaryAndPlanConfirm", "Daily")",
data: { "webSiteName":"@webSiteName","summary": summary,"plan":plan,"weekName":"@weekName"},
type: "POST",
success: function (result) {
if (result.split("|")[] == "true") {
alertBox(result.split("|")[], result.split("|")[]);
} else {
alertBox(result.split("|")[], result.split("|")[]);
}
}
});
}
});
</script>
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="~/Scripts/html2canvas.min.js"></script>
<script src="~/Scripts/bluebird.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// canvas生成图片
$("#btn").on("click", function () {
var getPixelRatio = function (context) { // 获取设备的PixelRatio
var backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 0.5;
return (window.devicePixelRatio || 0.5) / backingStore;
};
//生成的图片名称
var imgName = "@(webSiteName)@(weekName).jpg";
var shareContent = document.getElementById("imgDiv");
var width = shareContent.offsetWidth;
var height = shareContent.offsetHeight;
var canvas = document.createElement("canvas");
var context = canvas.getContext('2d');
var scale = getPixelRatio(context); //将canvas的容器扩大PixelRatio倍,再将画布缩放,将图像放大PixelRatio倍。
canvas.width = width * scale;
canvas.height = height * scale;
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
context.scale(scale, scale); var opts = {
scale: ,
background: '#FFFFFF'
};
window.pageYoffset = ;
document.documentElement.scrollTop = ;
document.body.scrollTop = ;
html2canvas(shareContent, opts).then(function (canvas) {
context.imageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
var dataUrl = canvas.toDataURL('image/jpeg', 1.0);
dataURIToBlob(imgName, dataUrl, callback);
});
});
}) // edited from https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob#Polyfill
var dataURIToBlob = function (imgName, dataURI, callback) {
var binStr = atob(dataURI.split(',')[]),
len = binStr.length,
arr = new Uint8Array(len); for (var i = ; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
} callback(imgName, new Blob([arr]));
} var callback = function (imgName, blob) {
var triggerDownload = $("<a>").attr("href", URL.createObjectURL(blob)).attr("download", imgName).appendTo("body").on("click", function () {
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(blob, imgName);
}
});
triggerDownload[].click();
triggerDownload.remove();
};
</script>
四 效果图
五 最后
自己记录一下,有这方面的需求,大家可以借鉴一下,不喜勿喷!
html2canvas 实现页面转图片并下载的更多相关文章
- js转换页面为图片并下载
<div style="background:red;width: 600px;height: 600px;" class="test"> < ...
- html2canvas页面截图图片不显示
前两天在一个群里,有人问使用html2canvas屏幕截图的时候为什么页面的图片不显示只显示了文字,我没有做过屏幕截图的需求,所以不是很清楚,今天稍稍测试了一下. 在github上将html2canv ...
- 前端js保存页面为图片下载到本地
前端js保存页面为图片下载到本地 手机端点击下载按钮将页面保存成图片到本地 前端js保存页面为图片下载到本地的坑 html2canvas 识别 svg 解决方案 方案 html2canvas.js:可 ...
- 下载远程(第三方服务器)文件、图片,保存到本地(服务器)的方法、保存抓取远程文件、图片 将图片的二进制字节字符串在HTML页面以图片形式输出 asp.net 文件 操作方法
下载远程(第三方服务器)文件.图片,保存到本地(服务器)的方法.保存抓取远程文件.图片 将一台服务器的文件.图片,保存(下载)到另外一台服务器进行保存的方法: 1 #region 图片下载 2 3 ...
- JS - 使用 html2canvas 将页面转PDF
JS - 使用 html2canvas 将页面转PDF 本方法可以将页面元素块转为pdf. 网站地址 jspdf.js 官网地址:http://jspdf.com GitHub 主页:https:// ...
- [C#基础实例]指定地址解析图片并下载
需求:查找页面图片并下载至本地: 实现: 首先:读取通过网络html内容,并用正则表达式查找图片地下. 其次:使用WebRequest.Create创建图片请求. 最后:把获取图片网络流数据通过Fil ...
- 【图文详解】python爬虫实战——5分钟做个图片自动下载器
python爬虫实战——图片自动下载器 之前介绍了那么多基本知识[Python爬虫]入门知识,(没看的先去看!!)大家也估计手痒了.想要实际做个小东西来看看,毕竟: talk is cheap sho ...
- 如何用Python爬虫实现百度图片自动下载?
Github:https://github.com/nnngu/LearningNotes 制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求 分析网页源代码,配合开发者工具 编写正则表达式或 ...
- h5页面转图片长按保存
5页面经常会遇到此类需求.将最后的结果页转换为图片长按保存.下面介绍一下实现此需求的过程 1,依赖安装 cnpm install html2canvas --save 2,依赖引入,使用 绑定 初始化 ...
随机推荐
- maven配置错误之Unable to import maven project: See logs for details
很多朋友在初次使用maven时,都会出现这个问题. 一加载maven项目,即会报出Unable to import maven project这样的错误,哪怕是新的maven项目也不例外. 我查阅了很 ...
- S7-1200视频教程: S7-1200的功能与特点-跟我学 - 1/112
S7-1200视频教程: S7-1200的功能与特点-跟我学 - 1/112 观看连接: http://www.elearning.siemens.com.cn/video/Course/201012 ...
- k8s的两种网络方案与多种工作模式[flannel与calico]
k8s的两种网络方案与多种工作模式 1. Flannel: flannel有三种工作模式: 1. vxlan(隧道方案) 2. host-gw(路由方案) 2. udp(在用户态实现的数据封装解封装, ...
- OS_页面置换算法:C++实现
一.实验目的: 通过模拟实现请求页式存储管理的几种基本页面置换算法,了解虚拟存储技术的特点,掌握虚拟存储请求页式存储管理中几种页面置换算法的基本思想和实现过程,并比较它们的效率. 二.实验内容: 本实 ...
- Jmeter Response Data 乱码问题解决方法
1. 进入jmeter\apache-jmeter-4.0\bin,打开jmeter.properties 2. 搜索“sampleresult.default.encoding” 设置sampler ...
- VB.NET在基类中定义共享事件(类似于C#中的静态事件)
基类: Public Class userFun Private Shared _PnlStatus As String ‘必须设为共享字段,如果不设为Shared,将不能传递字符串内容 Public ...
- Hystrix总结
Hystrix 能使你的系统在出现依赖服务失效的时候,通过隔离系统所依赖的服务,防止服务级联失败,同时提供失败回退机制,更优雅地应对失效,并使你的系统能更快地从异常中恢复. Hystrix能做什么? ...
- layer.open弹框中的表单数据无法获取
layer.open弹框中的表单数据无法获取 表单数据模板 layer.open() 页面效果: 当点击确定后,radio和textarea获取的值总是为空,解决办法: var setPriCustB ...
- express高效入门教程(5)
5.ejs模版 5.1.什么是模版引擎? 为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在后端开发中,处理数据的代码和展示 ...
- Sql sever 声明变量,赋值变量
语句: --声明变量DECLARE @idcard nvarchar () , @rowid nvarchar () --给变量赋值SELECT @idcard = '{0}', @rowid = ' ...