1.资源

<script type="text/javascript" src="./js/canvg2.js"></script>
<script type="text/javascript" src="./js/html2canvas-0.4.1.js"></script>
<script type="text/javascript" src="./js/jspdf.min.js"></script>
<script type="text/javascript" src="./js/jquery-2.1.4.min.js"></script>

2.主要代码

function changeImg(target) {
$('body').find('canvas').each(function (i, v) {
var imgSrc = v.toDataURL("image/png");
$(v).prop('outerHTML', '<img src="' + imgSrc + '" /> ');
});
var targetDom = $(target);
var copyDom = targetDom.clone();
copyDom.width(targetDom.width() + "px");
copyDom.height(targetDom.height() + "px");
$('body').append(copyDom);
svg2canvas(copyDom);
html2canvas(copyDom, {
onrendered: function (canvas) {
var imgData = canvas.toDataURL('image/jpeg');
var img = new Image();
img.src = imgData;
img.onload = function () {
if (this.width > this.height) {
var doc = new jsPDF('l', 'mm', [this.width * 0.225, this.height * 0.225]);
} else {
var doc = new jsPDF('p', 'mm', [this.width * 0.225, this.height * 0.225]);
}
doc.addImage(imgData, 'jpeg', 0, 0, this.width * 0.225, this.height * 0.225);
doc.save(getQueryString('name', decodeURIComponent(location.href)) + '.pdf');
};
copyDom.remove();
},
background: "#fff",
allowTaint: true
});
}
function svg2canvas(targetElem) {
var svgElem = targetElem.find('svg');
svgElem.each(function (index, node) {
var parentNode = node.parentNode;
var tempNode = document.createElement('div');
tempNode.appendChild(node);
var svg = tempNode.innerHTML;
var canvas = document.createElement('canvas');
canvg(canvas, svg);
parentNode.appendChild(canvas);
});
}

3.参考资源

Javascript 将html转成pdf,下载,支持多页哦(html2canvas 和 jsPDF)

纯js实现html转pdf

最新文章

  1. 规则引擎集成接口(七)规则引擎调用Java类
  2. DGV换行操作
  3. 怎么查看Mac电脑的开机记录?
  4. Ubuntu14.04.1 阿里apt源
  5. mysql指定某行或者某列的排序
  6. Tomcat 优化 java.lang.OutOfMemoryError: Java heap space 的解决方法
  7. U盘文件夹被病毒隐藏,且不能取消解决办法
  8. 关于网络连接方式的总结(HostOnly,NAT....)
  9. tomcat启动
  10. [python] virtualenv下解决matplotlib中文乱码
  11. (step5.1.6)hdu 1272(小希的迷宫——并查集)
  12. 网站上flv,MP4等格式的视频文件播放不出来的解决办法
  13. POJ 2609 Ferry Loading
  14. Server from URL
  15. vue.js实现瀑布流之vue-waterfall-easy
  16. SQL查询一个表里面某个字段值相同的数据记录
  17. UITableView 之 点击cell 实现两个自定义cell之间的切换
  18. 关于getchar-scanf函数的相关坑!
  19. img-html-2
  20. python winpdb远程调试

热门文章

  1. MVC视图布局页常用代码
  2. Max Sum Plus Plus---hdu1024(动态规划求M段的最大和)
  3. 流畅的python 读书笔记 第二章 序列构成的数组 列表推导
  4. C#知识点备忘
  5. samba文件共享服务配置(multiuser机制)二 (共两节)
  6. 剑指offer-有序二维数组中的查找
  7. node必知必会之node简介
  8. Rails的HashWithIndifferentAccess
  9. 逆分布函数法生成随机数(指数分布) R语言实现
  10. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) Solution