预览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 {
width: 80px;
height: 40px;
background: #313131;
border: 1px solid rgb(148, 148, 148);
background-color: #597194;
border: 1px solid #597194;
font-family: 'Noto Sans SC';
font-weight: 400;
font-style: normal;
font-size: 14px;
color: rgb(148, 148, 148);
color: rgba(255, 255, 255, 0.6);
}
.formreturnCancel:hover {
background: #1e2c49;
color: #266fff;
border: 1px solid #266fff;
background-color: #14aaff;
color: #fff;
border: 1px solid #14aaff;
}
.formsaveCancel {
width: 80px;
height: 40px;
background: #266fff;
background:#0b94d5;
border:1px solid #0b94d5;
font-family: 'Noto Sans SC';
font-weight: 400;
font-style: normal;
font-size: 14px;
color: #FFFFFF;
}
.formsaveCancel:hover{
background:#14aaff;
border:1px solid #14aaff;
}
</style>
<style>
.DiagnosticMessage-container .el-form-item__label{

View File

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

View File

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

View File

@ -1,5 +1,5 @@
<template>
<div class="PopUpOnlyReport-container">
<div class="PopUpOnlyReport-container" id="popup-report-root">
<div class="PopUpOnlyReport-container-body" id="pdf-content">
<div style="height: 100%; padding:0 90px; box-sizing: border-box;">
<div class="PopUpOnlyReport-container-bodytitle">体态测量报告单</div>
@ -131,8 +131,8 @@
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { getBackendUrl } from '@/services/api.js'
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';
// import html2canvas from 'html2canvas';
// import jsPDF from 'jspdf';
const emit = defineEmits([ 'closePopUpOnlyReport' ]);
const props = defineProps({
selectedPatient: {
@ -187,6 +187,7 @@ onMounted(() => {
}
}
}
setTimeout(() => {
generatePDF()
}, 500);
@ -206,66 +207,127 @@ function getFormattedTime() {
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
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();
try {
const root = document.getElementById('popup-report-root')
if (!root) {
ElMessage.error('报告容器未找到')
return
}
//
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;
if (!window.electronAPI) {
console.error('electronAPI 未定义')
return
}
const pdfBuffer = await window.electronAPI.generateReportPdf({
selector: '#popup-report-root',
pageSize: 'A4',
landscape: true, //
printBackground: true
})
// PDF
pdf.addImage(imgData, 'JPEG', 0, 0, imgWidth, imgHeight);
const blob = new Blob([pdfBuffer], { type: 'application/pdf' })
const form = new FormData()
// 使ID
const filename = `${props.detectionInfo.id || 'report'}.pdf`
form.append('file', blob, filename)
//
position += pageContentHeight;
currentPage++;
// detectionInfo.id
if (props.detectionInfo.id) {
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>
<style scoped>

View File

@ -184,6 +184,11 @@
<div class="PopUpReport-border2">{{ detectionInfo.treatment_info }}</div>
<div class="PopUpReport-title2">备注</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>
@ -678,4 +683,15 @@ function getFormattedTime() {
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>

View File

@ -1,8 +1,17 @@
<template>
<div class="viewPDF-container">
<div class="pdf-viewer">
<div class="pdf-container" ref="pdfContainer"></div>
<div class="viewPDF-header">
<div style="display: flex; align-items: center;">
</div>
<img src="@/assets/archive/close.png" alt="" style="cursor: pointer;" @click="handleCancel">
</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>
</template>
@ -12,12 +21,23 @@ import { patientAPI, detectionAPI,historyAPI,getBackendUrl } from '@/services/ap
import { ref, onMounted } from 'vue'
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
// worker
pdfjsLib.GlobalWorkerOptions.workerSrc = '/pdf.worker.min.js'
const BACKEND_URL = getBackendUrl()
const pdfCanvas = ref(null)
function handleCancel(){
emit('closeViewPDF',false)
}
// const renderPDF = async (url) => {
// try {
// // PDF
@ -106,7 +126,7 @@ const renderPage = async (pdf, pageNum, scale) => {
}
onMounted(() => {
// PDF URL
renderPDF(`${BACKEND_URL}/202512070001/20251209141628/report_142802459.pdf`) //
// renderPDF(`${BACKEND_URL}/202512070001/20251209141628/report_142802459.pdf`) //
})
</script>
@ -139,4 +159,12 @@ canvas {
border-radius: 8px;
overflow: auto;
}
.viewPDF-header{
height: 60px;
display: flex;
justify-content: flex-end;
align-items: center;
padding-right: 20px;
}
</style>