预览pdf(未完成)

This commit is contained in:
limengnan 2025-12-09 18:18:22 +08:00
parent 68936a6ab3
commit 0b1af5bc78
6 changed files with 206 additions and 76 deletions

View File

@ -141,31 +141,36 @@ async function handleDiagnosticInfo(status) {
.formreturnCancel { .formreturnCancel {
width: 80px; width: 80px;
height: 40px; height: 40px;
background: #313131; background-color: #597194;
border: 1px solid rgb(148, 148, 148); border: 1px solid #597194;
font-family: 'Noto Sans SC'; font-family: 'Noto Sans SC';
font-weight: 400; font-weight: 400;
font-style: normal; font-style: normal;
font-size: 14px; font-size: 14px;
color: rgb(148, 148, 148); color: rgba(255, 255, 255, 0.6);
} }
.formreturnCancel:hover { .formreturnCancel:hover {
background: #1e2c49; background-color: #14aaff;
color: #266fff; color: #fff;
border: 1px solid #266fff; border: 1px solid #14aaff;
} }
.formsaveCancel { .formsaveCancel {
width: 80px; width: 80px;
height: 40px; height: 40px;
background: #266fff; background:#0b94d5;
border:1px solid #0b94d5;
font-family: 'Noto Sans SC'; font-family: 'Noto Sans SC';
font-weight: 400; font-weight: 400;
font-style: normal; font-style: normal;
font-size: 14px; font-size: 14px;
color: #FFFFFF; color: #FFFFFF;
} }
.formsaveCancel:hover{
background:#14aaff;
border:1px solid #14aaff;
}
</style> </style>
<style> <style>
.DiagnosticMessage-container .el-form-item__label{ .DiagnosticMessage-container .el-form-item__label{

View File

@ -210,7 +210,7 @@
:selectedPatient="selectedPatient" :selectedPatient="selectedPatient"
:detectionInfo="detectionInfo" :detectionInfo="detectionInfo"
:selectId="rawData.id" :selectId="rawData.id"
@closePopUpReport="closePopUpReport" @closePopUpOnlyReport="closePopUpOnlyReport"
/> />
</div> </div>
</template> </template>
@ -344,7 +344,15 @@ function closeSelectData(is,val){
} }
function closePopUpReport() { function closePopUpReport() {
isPopUpReport.value = false isPopUpReport.value = false
emit("closeGenerateReport",true)
} }
function closePopUpOnlyReport() {
isPopUpOnlyReport.value = false
emit("closeGenerateReport",true)
}
</script> </script>
<style scoped> <style scoped>

View File

@ -303,7 +303,7 @@
</div> </div>
</div> </div>
</div> </div>
<!-- <ViewPDF /> --> <ViewPDF v-if="isViewPDF" :pdfUrl="pdfUrl" @closeViewPDF="closeViewPDF"/>
</div> </div>
</template> </template>
@ -341,6 +341,8 @@ const props = defineProps({
} }
}) })
const isTip = ref(false) // 退 const isTip = ref(false) // 退
const isViewPDF = ref(false) // PDF
const pdfUrl = ref('') // PDF
const tipValue = ref('') const tipValue = ref('')
const delType = ref('') const delType = ref('')
const detectionId = ref('') // ID const detectionId = ref('') // ID
@ -380,6 +382,9 @@ function getNo(order) {
return order return order
} }
} }
function closeViewPDF(){
isViewPDF.value = false
}
function clickImg(item,index){ function clickImg(item,index){
debugger debugger
selectIndex.value = index selectIndex.value = index
@ -562,10 +567,13 @@ function deleteClick(row){
isTip.value = true isTip.value = true
} }
function fileClick(row){ function fileClick(row){
ElMessage.success({ pdfUrl.value = BACKEND_URL + '/' + row.detection_report
message: '预览文件', debugger
duration: 3000 isViewPDF.value = true
}); // ElMessage.success({
// message: '',
// duration: 3000
// });
} }
function selectedChange(val){ function selectedChange(val){
@ -695,7 +703,10 @@ function generateReport(row,index){ // 打开生成报告页面
detectionId.value = row.id detectionId.value = row.id
isGenerateReport.value = true isGenerateReport.value = true
} }
function closeGenerateReport(){ // function closeGenerateReport(e){ //
if(e == true){
sessionsInit()
}
isGenerateReport.value = false isGenerateReport.value = false
} }

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="PopUpOnlyReport-container"> <div class="PopUpOnlyReport-container" id="popup-report-root">
<div class="PopUpOnlyReport-container-body" id="pdf-content"> <div class="PopUpOnlyReport-container-body" id="pdf-content">
<div style="height: 100%; padding:0 90px; box-sizing: border-box;"> <div style="height: 100%; padding:0 90px; box-sizing: border-box;">
<div class="PopUpOnlyReport-container-bodytitle">体态测量报告单</div> <div class="PopUpOnlyReport-container-bodytitle">体态测量报告单</div>
@ -131,8 +131,8 @@
<script setup> <script setup>
import { ref, onMounted, onUnmounted } from 'vue' import { ref, onMounted, onUnmounted } from 'vue'
import { getBackendUrl } from '@/services/api.js' import { getBackendUrl } from '@/services/api.js'
import html2canvas from 'html2canvas'; // import html2canvas from 'html2canvas';
import jsPDF from 'jspdf'; // import jsPDF from 'jspdf';
const emit = defineEmits([ 'closePopUpOnlyReport' ]); const emit = defineEmits([ 'closePopUpOnlyReport' ]);
const props = defineProps({ const props = defineProps({
selectedPatient: { selectedPatient: {
@ -187,6 +187,7 @@ onMounted(() => {
} }
} }
} }
setTimeout(() => { setTimeout(() => {
generatePDF() generatePDF()
}, 500); }, 500);
@ -206,66 +207,127 @@ function getFormattedTime() {
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
} }
const generatePDF = async () => { const generatePDF = async () => {
const element = document.getElementById('pdf-content'); try {
const root = document.getElementById('popup-report-root')
// PDF if (!root) {
const pdf = new jsPDF('p', 'mm', 'a4'); ElMessage.error('报告容器未找到')
const pageWidth = pdf.internal.pageSize.getWidth(); return
const pageHeight = pdf.internal.pageSize.getHeight();
//
const scale = 2; //
const elementWidth = element.offsetWidth;
const elementHeight = element.scrollHeight;
const widthRatio = pageWidth / (elementWidth / scale);
//
const pageContentHeight = (pageHeight / widthRatio) * scale;
let position = 0;
let currentPage = 1;
while (position < elementHeight) {
//
if (currentPage > 1) {
pdf.addPage();
} }
// if (!window.electronAPI) {
const canvas = await html2canvas(element, { console.error('electronAPI 未定义')
scale, return
useCORS: true, }
windowHeight: pageContentHeight,
height: pageContentHeight, const pdfBuffer = await window.electronAPI.generateReportPdf({
y: position, selector: '#popup-report-root',
x: 0, pageSize: 'A4',
scrollY: -window.scrollY // landscape: true, //
}); printBackground: true
})
//
const imgData = canvas.toDataURL('image/jpeg', 1.0);
const imgWidth = pageWidth;
const imgHeight = (canvas.height * imgWidth) / canvas.width;
// PDF const blob = new Blob([pdfBuffer], { type: 'application/pdf' })
pdf.addImage(imgData, 'JPEG', 0, 0, imgWidth, imgHeight); const form = new FormData()
// 使ID
const filename = `${props.detectionInfo.id || 'report'}.pdf`
form.append('file', blob, filename)
// // detectionInfo.id
position += pageContentHeight; if (props.detectionInfo.id) {
currentPage++; const res = await fetch(`${BACKEND_URL}/api/reports/${props.detectionInfo.id}/upload`, {
method: 'POST',
body: form
})
if (!res.ok) {
throw new Error(`HTTP ${res.status}: ${res.statusText}`)
}
const json = await res.json()
if (json.success) {
ElMessage.success('报告生成并上传成功')
emit('closePopUpOnlyReport', true)
} else {
throw new Error(json.error || '上传失败')
}
} else {
//
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
emit('closePopUpOnlyReport', true)
}
} catch (e) {
console.error('报告生成异常:', e)
ElMessage.error(`报告生成失败:${e.message}`)
} }
// PDF
const pdfBlob = pdf.output('blob');
const url = URL.createObjectURL(pdfBlob);
const a = document.createElement('a');
a.href = url;
a.download = '体态测量报告单.pdf';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
emit("closePopUpOnlyReport",false)
}; };
// const generatePDF = async () => {
// const element = document.getElementById('pdf-content');
// // PDF
// const pdf = new jsPDF('p', 'mm', 'a4');
// const pageWidth = pdf.internal.pageSize.getWidth();
// const pageHeight = pdf.internal.pageSize.getHeight();
// //
// const scale = 2; //
// const elementWidth = element.offsetWidth;
// const elementHeight = element.scrollHeight;
// const widthRatio = pageWidth / (elementWidth / scale);
// //
// const pageContentHeight = (pageHeight / widthRatio) * scale;
// let position = 0;
// let currentPage = 1;
// while (position < elementHeight) {
// //
// if (currentPage > 1) {
// pdf.addPage();
// }
// //
// const canvas = await html2canvas(element, {
// scale,
// useCORS: true,
// windowHeight: pageContentHeight,
// height: pageContentHeight,
// y: position,
// x: 0,
// scrollY: -window.scrollY //
// });
// //
// const imgData = canvas.toDataURL('image/jpeg', 1.0);
// const imgWidth = pageWidth;
// const imgHeight = (canvas.height * imgWidth) / canvas.width;
// // PDF
// pdf.addImage(imgData, 'JPEG', 0, 0, imgWidth, imgHeight);
// //
// position += pageContentHeight;
// currentPage++;
// }
// // PDF
// const pdfBlob = pdf.output('blob');
// const url = URL.createObjectURL(pdfBlob);
// const a = document.createElement('a');
// a.href = url;
// a.download = '.pdf';
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a);
// URL.revokeObjectURL(url);
// emit("closePopUpOnlyReport",false)
// };
</script> </script>
<style scoped> <style scoped>

View File

@ -184,6 +184,11 @@
<div class="PopUpReport-border2">{{ detectionInfo.treatment_info }}</div> <div class="PopUpReport-border2">{{ detectionInfo.treatment_info }}</div>
<div class="PopUpReport-title2">备注</div> <div class="PopUpReport-title2">备注</div>
<div class="PopUpReport-border3">{{ detectionInfo.suggestion_info }}</div> <div class="PopUpReport-border3">{{ detectionInfo.suggestion_info }}</div>
<div class="PopUpReport-footer">
<div style="margin-right: 80px;">检测时间{{ detectionInfo.created_at }}</div>
<div style="margin-right: 80px;">报告时间{{ getFormattedTime() }}</div>
<div>检测医生{{ detectionInfo.creator_name }}</div>
</div>
</div> </div>
@ -678,4 +683,15 @@ function getFormattedTime() {
display: none !important; display: none !important;
} }
} }
.PopUpOnlyReport-footer{
margin-top: 40px;
padding-top: 40px;
border-top: 1px solid #333;
display: flex;
font-weight: 700;
font-style: normal;
color: rgb(40, 40, 40);
font-size: 18px;
}
</style> </style>

View File

@ -1,8 +1,17 @@
<template> <template>
<div class="viewPDF-container"> <div class="viewPDF-container">
<div class="pdf-viewer"> <div class="viewPDF-header">
<div class="pdf-container" ref="pdfContainer"></div> <div style="display: flex; align-items: center;">
</div>
<img src="@/assets/archive/close.png" alt="" style="cursor: pointer;" @click="handleCancel">
</div> </div>
<div style="padding: 0 20px;height: calc(100vh - 80px);">
<iframe :src="pdfUrl" width="100%" height="100%" frameborder="0"></iframe>
</div>
<!-- <div class="pdf-viewer">
<div class="pdf-container" ref="pdfContainer"></div>
</div> -->
</div> </div>
</template> </template>
@ -12,12 +21,23 @@ import { patientAPI, detectionAPI,historyAPI,getBackendUrl } from '@/services/ap
import { ref, onMounted } from 'vue' import { ref, onMounted } from 'vue'
import * as pdfjsLib from 'pdfjs-dist' import * as pdfjsLib from 'pdfjs-dist'
const emit = defineEmits([ 'closeViewPDF' ]);
const props = defineProps({
pdfUrl: {
required: false,
type: String,
default: ''
}
})
const pdfUrl = ref(props.pdfUrl)
const pdfContainer = ref(null) // canvas const pdfContainer = ref(null) // canvas
// worker // worker
pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js' pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js'
const BACKEND_URL = getBackendUrl() const BACKEND_URL = getBackendUrl()
const pdfCanvas = ref(null) const pdfCanvas = ref(null)
function handleCancel(){
emit('closeViewPDF',false)
}
// const renderPDF = async (url) => { // const renderPDF = async (url) => {
// try { // try {
// // PDF // // PDF
@ -106,7 +126,7 @@ const renderPage = async (pdf, pageNum, scale) => {
} }
onMounted(() => { onMounted(() => {
// PDF URL // PDF URL
renderPDF(`${BACKEND_URL}/202512070001/20251209141628/report_142802459.pdf`) // // renderPDF(`${BACKEND_URL}/202512070001/20251209141628/report_142802459.pdf`) //
}) })
</script> </script>
@ -139,4 +159,12 @@ canvas {
border-radius: 8px; border-radius: 8px;
overflow: auto; overflow: auto;
} }
.viewPDF-header{
height: 60px;
display: flex;
justify-content: flex-end;
align-items: center;
padding-right: 20px;
}
</style> </style>