jsPDF之html生成PDF文件并下载demo

写在前面: 为了不浪费你们的时间,预先说明:由于时间关系本次使用jquery2x.js+html2canvas +html2canvas.js+ jsPdf.debug.js 组成写在elementUI中,简单做一下文档总结

文件下载地址:https://download.csdn.net/download/qq_27751965/19476359

重点:需要导出节点的内容不能滚动,高度需auto

Html:

<!--点击按钮-->
<div  style="position: fixed;right: 40px;top:40%;z-index: 4;cursor: pointer;display: none" id="exportToPdfBtn">
      <el-button type="primary" plain size="mini" id="exportToPdf" ><i class="iconfont iconxiazai" style="font-size: 12px">导出为PDF</i></el-button>
</div>

<!--导出文本容器-->
<div id="pdfDom" >
	<div id='part1'>...</div>
	<div id='part2'>...</div>
	<div id='part3'>...</div>
	.......
</div>

Css:

/**
**主要说明容器得高度不能固定,要么会只打印它高度显示得部分
**/
#pdfDom {
    padding: 15px;
    background-color: #ffffff;
    overflow-y: auto;
    height:auto;
  }

Js:

var that = this;
var downPdf = document.getElementById("exportToPdf");
downPdf.onclick = function() {
   // 进行dom截图 必须让dom更新完再调用
   Vue.nextTick(()=>{
       var targetDom = $("#pdfDom");
       
     // 设置 前三个部分各占一个页面  页面高度  =  当前宽度 + 30(页面15padding) /  552.28去掉canvas20padding  * 打印高度
     $('#part1').css('height', (targetDom.width() + 30) / 552.28 * 841.89)
     $('#part2').css('height', (targetDom.width() + 30) / 552.28 * 841.89)
     $('#part3').css('height', (targetDom.width() + 30) / 552.28 * 841.89)
     
       $('#wellCon').scrollTop(0);
       var copyDom = targetDom.clone();

       copyDom.attr('id','content');
       // copyDom.css('display','none');
       copyDom.width(targetDom.width() + "px");
       copyDom.height(targetDom.height() + "px");
       $('body').append(copyDom)
       html2canvas(targetDom, {
           allowTaint: true,
           useCORS: true,
           // background:"#fff",
           // dpi: 192,//导出pdf清晰度
           // scale:2,
       }).then((canvas)=>{
           var contentWidth = canvas.width;
           var contentHeight = canvas.height;

           //一页pdf显示html页面生成的canvas高度;
           var pageHeight = contentWidth / 592.28 * 841.89;
           //未生成pdf的html页面高度
           var leftHeight = contentHeight;
           //页面偏移
           var position = 0;
           //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高
           var imgWidth = 555.28;
           var imgHeight = 555.28/contentWidth * contentHeight;

           var pageData = canvas.toDataURL('image/jpeg', 1.0);
           // 取消生成状态
           this.isLoading = false
           var pdf = new jsPDF('', 'pt', 'a4');

           //有两个高度需要区分,一个是html页面的实际高度,和生成pdf的页面高度(841.89)
           //当内容未超过pdf一页显示的范围,无需分页
           if (leftHeight < pageHeight) {
               imgWidth = 555.28;
               imgHeight = 555.28/contentWidth * contentHeight;
               pdf.addImage(pageData, 'JPEG', 20, 20, imgWidth, imgHeight );
           } else {
               while(leftHeight > 0) {
                   leftHeight -= pageHeight;
                   pdf.addImage(pageData, 'JPEG', 20, position, imgWidth, imgHeight)
                   position -= 841.89;
                   //避免添加空白页
                   if(leftHeight > 0) {
                       pdf.addPage();
                   }
               }
           }
           pdf.save(that.deptName + '-' + that.userInfo.user_name + '-' + that.endTime + '.pdf');
           setTimeout(() => {window.close()},100)
           // 保存pdf对象
       })
   })
}

说明:新增功能-------前三块内容设置单页显示(下载包没有,算法)

     // 设置 前三个部分各占一个页面  页面高度  =  当前宽度 + 30(页面15padding) /  552.28去掉canvas20padding  * 打印高度
     $('#part1').css('height', (targetDom.width() + 30) / 552.28 * 841.89)
     $('#part2').css('height', (targetDom.width() + 30) / 552.28 * 841.89)
     $('#part3').css('height', (targetDom.width() + 30) / 552.28 * 841.89)
     //.......
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要前端生成PDF文件并提供下载,需要使用特定的库或工具来实现。以下是一种实现方法: 1. 安装相应的库或工具:可以使用jsPDF库来生成PDF文件,它是使用纯JavaScript实现的前端PDF生成库。可以通过npm安装jsPDF库。 2. 创建HTML页面:在HTML页面中添加一个按钮,用于触发生成PDF文件的操作。 3. 编写JavaScript代码:使用jsPDF库来生成PDF文件并提供下载。在按钮的点击事件处理程序中,首先实例化一个jsPDF对象,并设置相关的参数,例如页面大小、文本内容等。然后使用jsPDF提供的方法,将HTML内容转换为PDF文件。最后调用jsPDF提供的save方法,将生成PDF文件保存到本地。 以下是一个简单示例代码: ```html <!DOCTYPE html> <html> <head> <title>生成PDF下载</title> </head> <body> <button onclick="generatePDF()">生成PDF文件</button> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js"></script> <script> function generatePDF() { var doc = new jsPDF(); doc.text('Hello World!', 10, 10); doc.save('sample.pdf'); } </script> </body> </html> ``` 在上述示例中,点击"生成PDF文件"按钮将会生成一个包含"Hello World!"文本的PDF文件,并自动下载到本地。要使用该示例,只需将HTML代码复制到一个HTML文件中,并在浏览器中打开该文件。 请注意,生成PDF文件的功能可能受到不同浏览器的限制。有些浏览器可能会在自动下载文件之前提示用户是否允许下载。另外,如果需要更复杂的布局或包含图像等更复杂的内容,可能需要进一步学习和调整jsPDF库的使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.T's Blog

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值