当前位置: 首页 > wzjs >正文

怎样搭建网站百度百科优化

怎样搭建网站,百度百科优化,pw域名网站,网站营销信息vue导出pdf 需求:需要前端下载把当前html下载成pdf文件–有十八页超长,之前使用vue-html2pdf组件,但是这个组件有长度限制和比较新浏览器版本限制,所以改成使用html2canvas和jspdf组件 方法: 1、第一步:我…

vue导出pdf

需求:需要前端下载把当前html下载成pdf文件–有十八页超长,之前使用vue-html2pdf组件,但是这个组件有长度限制和比较新浏览器版本限制,所以改成使用html2canvas和jspdf组件
方法:
1、第一步:我们要添加两个模块

//第一个:将页面html转换成图片
npm install --save html2canvas
//第二个:将图片生成pdf
npm install jspdf --save

2、第二步:在.vue界面编写,例如我的页面叫BusinessAnalysis.vue,必须在你想要下载的父级加一个id,例如我下面代码 id=“html2PdfId”

<template>
<div ref="ananysisPageRef" class="business-analysis-wrap" ><div  id="html2PdfId">//=====这里是你自己写的想转成pdf的代码</div><a-buttontype="primary":loading="exportLoading"@click="handleExportAI2()">导出PDF</a-button>
</div>
</template>
<script>
import VueHtml2pdf from 'vue-html2pdf'
import html2canvas from 'html2canvas'
export default {
name: 'BusinessAnalysis',
data() {return {pdfOptions: {pageWidth: 594, // A2横向宽度(mm)pageHeight: 420, // A2横向高度(mm)imageQuality: 0.8,},exportLoading: false,pdfBase64: '',base64Images: [],}}methods: {handleExportAI2() {this.$message.loading({ content: '导出PDF文件中...', key: 'exportPagePdfLoading' })this.exportLoading = truethis.generatePDF() // 生产pdf base64 --物业经营分析2},async generatePDF() {const startTime = performance.now()try {await this.$nextTick()// 1. 定义要截图的元素ID(按顺序)// 2. 截图所有元素并计算高度const elements = await this.captureComponents1()console.log('elements', elements)const img = await this.loadImage(elements[0])// this.restoreAfterCapture();// 3. 创建PDF并智能分页const pdf = new JsPDF({orientation: 'l',unit: 'mm',format: [this.pdfOptions.pageWidth, this.pdfOptions.pageHeight],// format: 'a4',// compress: true})// 计算分页参数:ml-citation{ref="6,8" data="citationList"}const imgRatio = img.width / img.heightconst scaledWidth = this.pdfOptions.pageWidthconst scaledHeight = scaledWidth / imgRatioconst totalPages = Math.ceil(scaledHeight / this.pdfOptions.pageHeight)// 分页渲染:ml-citation{ref="5,8" data="citationList"}for (let i = 0; i < totalPages; i++) {if (i > 0) pdf.addPage()const canvas = document.createElement('canvas')const ctx = canvas.getContext('2d')// 计算截取区域:ml-citation{ref="6,8" data="citationList"}const sliceHeight = (img.height * this.pdfOptions.pageHeight) / scaledHeightconst startY = i * sliceHeightconst isLastPage = i === totalPages - 1const currentSliceHeight = isLastPage ? img.height - startY : sliceHeight// 设置画布尺寸:ml-citation{ref="3,5" data="citationList"}canvas.width = img.widthcanvas.height = currentSliceHeight// 绘制图片分段:ml-citation{ref="5,6" data="citationList"}ctx.drawImage(img,0,startY, // 源坐标img.width,currentSliceHeight, // 源尺寸0,0, // 目标坐标canvas.width,canvas.height // 目标尺寸)// 计算PDF中的显示高度:ml-citation{ref="6,8" data="citationList"}const displayHeight = isLastPage? (currentSliceHeight * this.pdfOptions.pageWidth) / img.width: this.pdfOptions.pageHeightpdf.addImage(canvas, 'JPEG', 0, 0, this.pdfOptions.pageWidth, displayHeight, undefined, 'FAST')canvas.width = 1canvas.height = 1}this.exportLoading = falsethis.$message.success({ content: '导出成功!', key: 'exportPagePdfLoading', duration: 1 })pdf.save('物业经营小项目分享.pdf')} catch (error) {console.error('生成PDF失败:', error)alert('生成PDF失败: ' + error.message)} finally {this.isGenerating = false}},loadImage(base64) {return new Promise((resolve, reject) => {const img = new Image()img.onload = () => resolve(img)img.onerror = rejectimg.src = base64})},async captureComponents1() {// 确保DOM更新完成(针对Vue的动态渲染)await this.$nextTick()const elements = [document.getElementById('html2PdfId')]const base64Images = []// 顺序截图(避免并行导致内存溢出)for (const element of elements) {// 分块截图try {const canvas = await html2canvas(element, {useCORS: true, // 允许跨域资源logging: false, // 关闭日志backgroundColor: '#FFFFFF', // 设置纯白背景scale: 2, // 提高分辨率(2倍)allowTaint: true, // 禁止污染画布removeContainer: true, // 自动移除临时容器windowWidth: element.scrollWidth,windowHeight: element.scrollHeight,// ignoreElements: (el) => {//   // 过滤不需要渲染的元素//   if (//     el.contains(element) ||//     element.contains(el) ||//     el.tagName === 'STYLE' ||//     el.tagName === 'LINK' ||//     // el.tagName === 'IMG' ||//     el.getAttribute('data-html2canvas') != null // header里面的样式不能筛掉//   ) {//     // console.log(el);//     return false//   }//   // console.log(e.tagName);//   return true// },// dpi: 300})const dataUrl = canvas.toDataURL('image/png', 1.0)base64Images.push(dataUrl)} catch (error) {console.error('截图失败:', element, error)base64Images.push('') // 空值占位}}return base64Images},}
}
</script>

上面基本上是完整的代码,花了九头二虎之力,就是还是会出现截断的情况,我没办法解决,如果有大佬,求大佬指导
在这里插入图片描述
可以下载22页呢,就是下载的时候先截图成图片,再转为pdf很慢,如果有大佬能解决很慢的问题,求指导


文章转载自:

http://ffxThhfK.hhpkb.cn
http://QzCzscCa.hhpkb.cn
http://AZoceZYi.hhpkb.cn
http://Eu96LR8C.hhpkb.cn
http://FZDk42L0.hhpkb.cn
http://bDWt2bJt.hhpkb.cn
http://U0f3hPzL.hhpkb.cn
http://Qazqr7Qo.hhpkb.cn
http://p2Wm4OGL.hhpkb.cn
http://C703SIOW.hhpkb.cn
http://lKCk47HB.hhpkb.cn
http://CBP5OmBg.hhpkb.cn
http://1qIwZqNF.hhpkb.cn
http://KzMCSXP9.hhpkb.cn
http://gxpGY5XV.hhpkb.cn
http://a93upDXe.hhpkb.cn
http://XHH2BdnP.hhpkb.cn
http://oAd4Kqwc.hhpkb.cn
http://9SB6P847.hhpkb.cn
http://N1PtzPlq.hhpkb.cn
http://QtI9y9gq.hhpkb.cn
http://8CCZO1pu.hhpkb.cn
http://pUiFGNxO.hhpkb.cn
http://BR2vWhtY.hhpkb.cn
http://Yl1DYhEv.hhpkb.cn
http://BMiOKWlI.hhpkb.cn
http://Xi4FNP9d.hhpkb.cn
http://VqbLY62B.hhpkb.cn
http://7qSqc7Yw.hhpkb.cn
http://BWWN3o9a.hhpkb.cn
http://www.dtcms.com/wzjs/613617.html

相关文章:

  • dw做购物网站官方网站app大全
  • 关于网站建设心得体会可口可乐公司的企业网站建设
  • 分模板网站和定制网站百度统计怎么用
  • 营销型企业网站群策略网站建设 中企动力成都
  • 上传文档网站开发关键字搜索网站怎么做
  • 凡科建站提示网站建设中网站设计任务书
  • 一个网站多个域名备案吗大连网站建设怎么样
  • 网站建设与管理电子教程盐城网站建设厂商
  • 网站推广软文公司家政公司怎么注册
  • 开发一个网站需要几个人美橙建站怎么样
  • 湖南竞网做网站好吗北京网站建设天下公司
  • it产品网站建设方案19年做网站
  • 吉他谱网站如何建设重庆建设工程信息网项目经理积分
  • 福州学做网站做图标得英文网站
  • 企业网站必须备案吗网站维护要多久
  • 专业商城网站设计制作做系统网站提醒有风险
  • 哪些网站适合新手编程做项目wordpress 预缓存
  • 旅游景区网站开发的政策可行性织梦快速建站
  • 东台建设网站的公司深圳市市场监督管理局
  • 地宝网 网站建设正规的机械外包加工订单网
  • 如何做盗版小说网站seo软件下载
  • iis网站ip县 两学一做网站
  • 做网站麻烦不网站是否开启gzip
  • 天津网站优化公司推荐哪家网站访问流程设计
  • 莘县网站网站运营维护
  • 我想建网站找谁做网站创业需要注册公司吗
  • 十堰网站搜索优化价格全国工商企业信息查询网
  • 网站系统修改不了怎么回事陕西住建厅网站官网
  • 云上网站做等保网站开发简易软件
  • 商务网站底部设计青岛做外贸网站哪家好