国内做网站群平台的公司,原创主题 wordpress,阿里云主机 搭建网站,网站建设公司调研汇报ppt在 Web 开发中#xff0c;有时我们希望用户能够将网页上的 HTML 内容保存为 Word 文档#xff0c;以便更方便地分享和打印。
html样式 word文档 工具准备
1、 html-docx-js - npm
html-docx-js是一个 JavaScript 库#xff0c;用于将 HTML 内容转换为 Word 文档的格式。它…在 Web 开发中有时我们希望用户能够将网页上的 HTML 内容保存为 Word 文档以便更方便地分享和打印。
html样式 word文档 工具准备
1、 html-docx-js - npm
html-docx-js是一个 JavaScript 库用于将 HTML 内容转换为 Word 文档的格式。它提供了简单的 API使得在浏览器环境中可以轻松地生成并导出 Word 文档
2、 file-saver - npm
file-saver是一个 JavaScript 库用于在浏览器中保存文件。它简化了通过 Blob 对象保存文件的过程并提供了一个直观的 API。
代码实现
1、先引入这两个库可以通过 npm 安装也可以直接使用 CDN 引入还可以下载到本地项目引用。
!-- 使用 npm 安装 --
!-- npm install html-docx-js file-saver --!-- 或者直接使用 CDN 引入 --
script srchttps://cdn.jsdelivr.net/npm/html-docx-js/dist/html-docx.js/script
script srchttps://cdn.jsdelivr.net/npm/file-saver/dist/FileSaver.min.js/script!-- 或者访问上面CDN链接保存到本地放在项目里引用 --
script src./js/html-docx.js/script
script src./js/FileSaver.min.js/script2、创建一个导出方法该方法接受 HTML 内容作为参数并将其导出为 Word 文档。
const exportHtmlToDocx (htmlContent, fileName exported-document.docx) {// convertImagesToBase64(htmlContent);// 将HTML元素转换为字符串并提取图片const regularImages Array.from(htmlContent.querySelectorAll(img));const imagePromises regularImages.map(imgElement {const canvas document.createElement(canvas);const ctx canvas.getContext(2d);const imgObj new Image();imgObj.crossOrigin Anonymous; // 如果图片需要跨域访问imgObj.src imgElement.src;return new Promise((resolve, reject) {imgObj.onload () {//canvas 图片缩小到固定大小//如果页面上img设置了stylewidth:50px;height:50px已页面设置的为主如果没有设置就是默认图片的大小canvas.width imgElement.width || imgObj.width;// 想要的canvas固定宽度canvas.height imgElement.height || imgObj.height;// 想要的canvas固定高度//计算缩放比例var ratio Math.min(canvas.width / imgObj.width, canvas.height / imgObj.height);var scaledWidth imgObj.width * ratio;var scaledHeight imgObj.height * ratio;var offsetX (canvas.width - scaledWidth) / 2;var offsetY (canvas.height - scaledHeight) / 2;// 绘制缩小后的图片ctx.drawImage(imgObj, 0, 0, imgObj.width, imgObj.height, offsetX, offsetY, scaledWidth, scaledHeight);// 将缩小后的图片导出为PNGresolve(canvas.toDataURL(image/png));};imgObj.onerror reject;});});Promise.all(imagePromises).then(dataUris {dataUris.forEach((dataUri, i) {regularImages[i].src dataUri;});// 构建包含 HTML 内容的完整 HTML 文档字符串const fullHtmlContent !DOCTYPE htmlhtmlheadmeta http-equivContent-Type contenttext/html; charsetUTF-8/headbody${htmlContent.outerHTML}/body/html;// 使用 html-docx-js 将 HTML 转换为 Word 文档的 Blob 对象const converted htmlDocx.asBlob(fullHtmlContent);// 使用 file-saver 保存 Blob 对象为 Word 文档文件saveAs(converted, fileName);});
};
3、在应用中调用上述导出方法并传入想导出的 HTML 内容。
注意html结构需要包含style样式引入css文件样式不生效的
//html页面上
a classfs-14 text-g ng-clickvm.daochu()导出/a
div classrow idresumeAllInfodiv classrow stylecolor:#205081;font-size:16px;border-bottom:1px solid #eee;margin-bottom: 15px;padding-bottom: 5px;基本信息/divdiv classrow stylevertical-align: middlespan stylefloat: left;margin-right:20pximg stylewidth:50px;height:50px ng-src{{ vm.resume.portraitUrl }} rw-default-img img-typeportrait alt头像/spanspanspan stylefont-size:16px;margin-right:20px{{ vm.resume.name }}/spanspan stylemargin-right:10px{{ vm.resume.phone }}/spanspan ng-ifvm.resume.wechat stylemargin-right:10pxspan stylemargin-right:10px|/span{{ vm.resume.wechat }}/spanspan ng-ifvm.resume.qq stylemargin-right:10pxspan stylemargin-right:10px|/span{{ vm.resume.qq }}/spanspan ng-ifvm.resume.email stylemargin-right:10pxspan stylemargin-right:10px|/span{{ vm.resume.email }}/span/span/div
/divscript……vm.daochudaochu;function daochu() {// 获取要导出的HTML字符串const htmlElement document.getElementById(resumeAllInfo);//调用导出方法exportHtmlToDocx(htmlElement, my-exported-document.docx);}
/script 文章转载自: http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.bpmth.cn.gov.cn.bpmth.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.lfpzs.cn.gov.cn.lfpzs.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.nhzxr.cn.gov.cn.nhzxr.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.langlaitech.cn.gov.cn.langlaitech.cn http://www.morning.bnmrp.cn.gov.cn.bnmrp.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.wcgfy.cn.gov.cn.wcgfy.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.xxfxxf.cn.gov.cn.xxfxxf.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.c7500.cn.gov.cn.c7500.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.xltdh.cn.gov.cn.xltdh.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.kwrzg.cn.gov.cn.kwrzg.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.kzdgz.cn.gov.cn.kzdgz.cn http://www.morning.xbxks.cn.gov.cn.xbxks.cn http://www.morning.jrhcp.cn.gov.cn.jrhcp.cn http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.tzpqc.cn.gov.cn.tzpqc.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn