uniapp 页面转换成pdf

uniapp 页面转换成pdf。
“html2canvas”: “^1.4.1”,
“jspdf”: “^2.5.1”,

		const content = document.querySelector('#content').innerHTML; // 需要转换为PDF的DOM元素
			  const innerHtml = `
			          <head>
			        <title>Print</title>
			    
			      </head>
			      <body>
			        ${content}
			      </body>
			      `
		html2canvas(innerHtml, {
		        allowTaint: true
		      }).then(function (canvas) {
		        let contentWidth = canvas.width
		        let contentHeight = canvas.height
		        let pageHeight = contentWidth / 592.28 * 841.89
		        let leftHeight = contentHeight
		        let position = 0
		        let imgWidth = 595.28
		        let imgHeight = 592.28 / contentWidth * contentHeight
		        let pageData = canvas.toDataURL('image/jpeg', 1.0)
				let PDF = new jsPDF('', 'pt', 'a4')
		        if (leftHeight < pageHeight) {
		          PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
		        } else {
		          while (leftHeight > 0) {
		            PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
		            leftHeight -= pageHeight
		            position -= 841.89
		            if (leftHeight > 0) {
		              PDF.addPage()
		            }
		          }
		        }
				//console.error(PDF.output('blob'), 'HHHH')
		        PDF.save('output.pdf')
		})
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp中,你可以使用uni.uploadFile方法来上传多个PDF文件。你需要将每个文件都转换为FormData对象,然后使用uni.uploadFile方法进行上传。 以下是一个示例代码: ```javascript // 在页面中选择多个文件 uni.chooseMessageFile({ count: 5, // 最多选择5个文件 type: 'file', // 文件类型为任意文件 success: function(res) { // 选择文件成功 const tempFiles = res.tempFiles; // 创建一个FormData对象 const formData = new FormData(); // 循环添加每个文件到FormData对象中 for (let i = 0; i < tempFiles.length; i++) { formData.append('file', tempFiles[i].path); } // 发起上传请求 uni.uploadFile({ url: 'http://example.com/upload', // 上传接口地址 filePath: tempFiles[i].path, // 要上传的文件路径 name: 'file', // 文件对应的key值,这个根据你的实际需求来修改 formData: formData, // formData对象 success: function(res) { // 上传成功 console.log(res.data); }, fail: function(res) { // 上传失败 console.error(res); } }); }, fail: function(res) { // 选择文件失败 console.error(res); } }); ``` 这是一个基本的示例,你可以根据实际需求进行修改。在示例中,我们使用了uni.chooseMessageFile方法来选择多个文件,并将选中的文件路径添加到FormData对象中。然后,我们使用uni.uploadFile方法来上传FormData对象。 请注意,你需要替换示例代码中的上传接口地址和formData对象中的key值,以适应你的实际情况。另外,你还需要根据自己的需求进行错误处理和其他逻辑操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值