业务联系单pdf导出

This commit is contained in:
hongchao 2025-11-07 16:25:46 +08:00
parent 0e4b980e49
commit dde99b5a4c
2 changed files with 144 additions and 1 deletions

View File

@ -275,6 +275,7 @@
</div>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="print"> </el-button>
<el-button type="success" icon="el-icon-download" @click="downloadPDF">下载PDF</el-button>
<el-button @click="dialogVisible = false"> </el-button>
</span>
</el-dialog>
@ -554,6 +555,77 @@ export default {
console.error('生成单据错误:', error)
})
},
// PDF
async downloadPDF() {
try {
this.$modal.loading('正在生成PDF请稍候...')
// DOM
const element = document.getElementById('print-content')
if (!element) {
this.$modal.closeLoading()
this.$message.error('未找到要导出的内容')
return
}
// 使html2canvasDOMcanvas
const canvas = await html2canvas(element, {
scale: 2, //
useCORS: true, //
allowTaint: true,
logging: false, //
backgroundColor: '#ffffff'
})
// PDF
const pdf = new jsPDF('p', 'mm', 'a4')
// canvas
const imgWidth = 210 // A4(mm)
const pageHeight = 297 // A4(mm)
const imgHeight = (canvas.height * imgWidth) / canvas.width
let heightLeft = imgHeight
let position = 0
// canvas
const imgData = canvas.toDataURL('image/jpeg', 1.0)
//
if (heightLeft > pageHeight) {
//
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight)
heightLeft -= pageHeight
//
while (heightLeft > 0) {
position = heightLeft - imgHeight
pdf.addPage()
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight)
heightLeft -= pageHeight
}
} else {
//
pdf.addImage(imgData, 'JPEG', 0, 0, imgWidth, imgHeight)
}
// PDF
const fileName = `业务联系单_${this.dialogForm.code || '未命名'}_${new Date().getTime()}.pdf`
// PDF
pdf.save(fileName)
this.$modal.closeLoading()
this.$message.success('PDF下载成功')
} catch (error) {
console.error('生成PDF失败:', error)
this.$modal.closeLoading()
this.$message.error('生成PDF失败请稍后重试')
}
},
//
async getDialogContent(row) {
console.log('🚀 ~ getDialogContent ~ row:', row.taskId)

View File

@ -250,7 +250,7 @@
</div>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="print"> </el-button>
<!-- <el-button type="primary" @click="">查看PDF</el-button> -->
<el-button type="success" icon="el-icon-download" @click="downloadPDF">下载PDF</el-button>
<el-button @click="dialogVisible = false"> </el-button>
</span>
</el-dialog>
@ -272,6 +272,8 @@ import {
import { getLeaseTask, getCodePDF } from '@/api/business/index'
import vueEasyPrint from "vue-easy-print";
import printJS from 'print-js';
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';
export default {
name: "Home",
@ -525,6 +527,75 @@ export default {
})
},
// PDF
async downloadPDF() {
try {
this.$modal.loading('正在生成PDF请稍候...')
// DOM
const element = document.getElementById('print-content1')
if (!element) {
this.$modal.closeLoading()
this.$message.error('未找到要导出的内容')
return
}
// 使html2canvasDOMcanvas
const canvas = await html2canvas(element, {
scale: 2, //
useCORS: true, //
allowTaint: true,
logging: false, //
backgroundColor: '#ffffff'
})
// PDF
const pdf = new jsPDF('p', 'mm', 'a4')
// canvas
const imgWidth = 210 // A4(mm)
const pageHeight = 297 // A4(mm)
const imgHeight = (canvas.height * imgWidth) / canvas.width
let heightLeft = imgHeight
let position = 0
// canvas
const imgData = canvas.toDataURL('image/jpeg', 1.0)
//
if (heightLeft > pageHeight) {
//
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight)
heightLeft -= pageHeight
//
while (heightLeft > 0) {
position = heightLeft - imgHeight
pdf.addPage()
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight)
heightLeft -= pageHeight
}
} else {
//
pdf.addImage(imgData, 'JPEG', 0, 0, imgWidth, imgHeight)
}
// PDF
const fileName = `业务联系单_${this.dialogForm.code || '未命名'}_${new Date().getTime()}.pdf`
// PDF
pdf.save(fileName)
this.$modal.closeLoading()
this.$message.success('PDF下载成功')
} catch (error) {
console.error('生成PDF失败:', error)
this.$modal.closeLoading()
this.$message.error('生成PDF失败请稍后重试')
}
},
},
};