Commit 9c3466ac authored by 朱超's avatar 朱超

明日报表更新

parent 14e0abfd
...@@ -16,8 +16,10 @@ ...@@ -16,8 +16,10 @@
"echarts": "^5.5.0", "echarts": "^5.5.0",
"element-plus": "^2.7.5", "element-plus": "^2.7.5",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
"html2canvas": "^1.4.1",
"jquery": "^3.7.1", "jquery": "^3.7.1",
"jsencrypt": "^3.3.2", "jsencrypt": "^3.3.2",
"jspdf": "^2.5.1",
"less": "^4.2.0", "less": "^4.2.0",
"mitt": "^3.0.1", "mitt": "^3.0.1",
"node-sass": "^9.0.0", "node-sass": "^9.0.0",
......
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
var noTableHeight = 0 //table外的元素高度
export const htmlPdf = (title, html, fileList, type) => {// type传有效值pdf则为横版
if (fileList) {
const pageHeight = Math.floor(277 * html.scrollWidth / 190) + 20 //计算pdf高度
for (let i = 0; i < fileList.length; i++) { //循环获取的元素
const multiple = Math.ceil((fileList[i].offsetTop + fileList[i].offsetHeight) / pageHeight) //元素的高度
if (isSplit(fileList, i, multiple * pageHeight)) { //计算是否超出一页
var _H = '' //向pdf插入空白块的内容高度
if (fileList[i].localName !== 'tr') { //判断是不是表格里的内容
_H = multiple * pageHeight - (fileList[i].offsetTop + fileList[i].offsetHeight)
} else {
_H = multiple * pageHeight - (fileList[i].offsetTop + fileList[i].offsetHeight + noTableHeight) + 20
}
var newNode = getFooterElement(_H) //向pdf插入空白块的内容
const divParent = fileList[i].parentNode // 获取该div的父节点
const next = fileList[i].nextSibling // 获取div的下一个兄弟节点
// 判断兄弟节点是否存在
if (next) {
// 存在则将新节点插入到div的下一个兄弟节点之前,即div之后
divParent.insertBefore(newNode, next)
} else {
// 否则向节点添加最后一个子节点
divParent.appendChild(newNode)
}
}
}
}
html2Canvas(html, {
allowTaint: false,
taintTest: false,
logging: false,
useCORS: true,
dpi: window.devicePixelRatio * 1,
scale: 1 // 按比例增加分辨率
}).then(canvas => {
var pdf = new JsPDF('p', 'mm', 'a4') // A4纸,纵向
var ctx = canvas.getContext('2d')
var a4w = type ? 277 : 190; var a4h = type ? 190 : 277 // A4大小,210mm x 297mm,四边各保留10mm的边距,显示区域190x277
var imgHeight = Math.floor(a4h * canvas.width / a4w) // 按A4显示比例换算一页图像的像素高度
var renderedHeight = 0
while (renderedHeight < canvas.height) {
var page = document.createElement('canvas')
page.width = canvas.width
page.height = Math.min(imgHeight, canvas.height - renderedHeight)// 可能内容不足一页
// 用getImageData剪裁指定区域,并画到前面创建的canvas对象中
page.getContext('2d').putImageData(ctx.getImageData(0, renderedHeight, canvas.width, Math.min(imgHeight, canvas.height - renderedHeight)), 0, 0)
pdf.addImage(page.toDataURL('image/jpeg', 1.0), 'JPEG', 10, 10, a4w, Math.min(a4h, a4w * page.height / page.width)) // 添加图像到页面,保留10mm边距
renderedHeight += imgHeight
if (renderedHeight < canvas.height) {
pdf.addPage()// 如果后面还有内容,添加一个空页
}
// delete page;
}
// 保存文件
pdf.save(title + '.pdf')
})
}
// pdf截断需要一个空白位置来补充
const getFooterElement = (remainingHeight, fillingHeight = 0) => {
const newNode = document.createElement('div')
newNode.style.background = '#ffffff'
newNode.style.width = 'calc(100% + 8px)'
newNode.style.marginLeft = '-4px'
newNode.style.marginBottom = '0px'
newNode.classList.add('divRemove')
newNode.style.height = (remainingHeight + fillingHeight) + 'px'
return newNode
}
const isSplit = (nodes, index, pageHeight) => {
// 判断是不是tr 如果不是高度存起来
// 表格里的内容要特殊处理
// tr.offsetTop 是tr到table表格的高度
// 所以计算高速时候要把表格外的高度加起来
// 生成的pdf没有表格了这里可以不做处理 直接计算就行
if (nodes[index].localName !== 'tr') { //判断元素是不是tr
noTableHeight += nodes[index].clientHeight
}
if (nodes[index].localName !== 'tr') {
return nodes[index].offsetTop + nodes[index].offsetHeight < pageHeight && nodes[index + 1] && nodes[index + 1].offsetTop + nodes[index + 1].offsetHeight > pageHeight
} else {
return nodes[index].offsetTop + nodes[index].offsetHeight + noTableHeight < pageHeight && nodes[index + 1] && nodes[index + 1].offsetTop + nodes[index + 1].offsetHeight + noTableHeight > pageHeight
}
}
\ No newline at end of file
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
style="min-width: 70px" style="min-width: 70px"
@click="postHeatForecastFun" @click="postHeatForecastFun"
class="printHidden" class="printHidden"
v-show="printHidden"
>历史查询</el-button >历史查询</el-button
> >
</th> </th>
...@@ -53,6 +54,7 @@ ...@@ -53,6 +54,7 @@
type="primary" type="primary"
style="min-width: 70px" style="min-width: 70px"
class="printHidden" class="printHidden"
v-show="printHidden"
@click="postPrintHeatForecastFun" @click="postPrintHeatForecastFun"
>打印查询</el-button >打印查询</el-button
> >
...@@ -243,7 +245,7 @@ ...@@ -243,7 +245,7 @@
</tr> </tr>
</table> </table>
<div> <div>
<div class="btngrounp printHidden"> <div class="btngrounp printHidden" v-show="printHidden">
<el-button <el-button
type="primary" type="primary"
v-print="printObj" v-print="printObj"
...@@ -265,6 +267,7 @@ ...@@ -265,6 +267,7 @@
<el-button <el-button
type="primary" type="primary"
style="margin: 15px 0 0 10px" style="margin: 15px 0 0 10px"
@click="handleExport"
>导出PDF</el-button >导出PDF</el-button
> >
<router-link to="/RealAnalysis" <router-link to="/RealAnalysis"
...@@ -460,6 +463,7 @@ import { saveAs } from "file-saver"; ...@@ -460,6 +463,7 @@ import { saveAs } from "file-saver";
import XLSX from "xlsx-js-style"; import XLSX from "xlsx-js-style";
import { postHeatForecast, postPrintHeatForecast } from "../../api/report"; import { postHeatForecast, postPrintHeatForecast } from "../../api/report";
import print from "vue3-print-nb"; import print from "vue3-print-nb";
import { htmlPdf } from "../../utils/toPDF";
const temp = ref([]); //24小时室外预测值 const temp = ref([]); //24小时室外预测值
const weather = ref([]); //当日气象信息 const weather = ref([]); //当日气象信息
const predTemp = ref([]); //24小时室外体感预测值 const predTemp = ref([]); //24小时室外体感预测值
...@@ -469,6 +473,7 @@ const enertyInfo = reactive({ ...@@ -469,6 +473,7 @@ const enertyInfo = reactive({
printTime: null, printTime: null,
queryTime: "2024-1-1 09:00:00", queryTime: "2024-1-1 09:00:00",
}); });
const printHidden = ref(true);
const vPrint = print; const vPrint = print;
const printObj = { const printObj = {
id: "printBlock", id: "printBlock",
...@@ -476,6 +481,16 @@ const printObj = { ...@@ -476,6 +481,16 @@ const printObj = {
extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>', extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
zIndex: 20002, zIndex: 20002,
}; };
const handleExport = (name) => {
printHidden.value = false;
setTimeout(function () {
var fileName = "明日预测报表";
const fileList = document.getElementsByClassName("contentBlockInn");
htmlPdf(fileName, document.querySelector("#printBlock"), fileList);
ElMessage.success("导出成功!请稍后。。。");
printHidden.value = true;
}, 100);
};
function exportTableToExcel() { function exportTableToExcel() {
const workbook = XLSX.utils.book_new(); const workbook = XLSX.utils.book_new();
var ws = XLSX.utils.table_to_sheet(document.getElementById("exlBlock")); var ws = XLSX.utils.table_to_sheet(document.getElementById("exlBlock"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment