修改文件预览功能的配置,版本1

main
于阔 9 months ago
parent aec7eb71e6
commit 2c5b80e641

@ -21,13 +21,103 @@ const fetchDocFile = async ()=>{
// APIDOC // APIDOC
const response = await queryFileFlowRepTemplateApi(fileData); const response = await queryFileFlowRepTemplateApi(fileData);
console.log(response,"response文件流信息"); console.log(response,"response文件流信息");
const { data } = response; const { data, headers } = response;
const arrayBuffer = new File([data], 'document.docx', { type: 'docx' }).arrayBuffer(); if(headers['status'] == 500){// WARNERROR
console.log(arrayBuffer,"arrayBuffer"); const code = headers['error']?headers['error'].substring(headers['error'].length-4):'1003';
const result = mammoth.convertToHtml({ arrayBuffer }); console.log(code)
switch(code){
case "1001":
case "1002":
ElMessage.error(downLoadErrorMsg[code]);
break;
default:
ElMessage.error(downLoadErrorMsg['1003']);
break;
}
return;
}
const arrayBuffer = await new File([data], 'document.docx', { type: data.type }).arrayBuffer();
if (!(arrayBuffer instanceof ArrayBuffer)) {
throw new Error('Invalid arrayBuffer type');
}
// styleMap
const styleMap = `
p[style-name='Heading 1'] => h1:fresh
p[style-name='Heading 2'] => h2:fresh
p[style-name='Heading 3'] => h3:fresh
table => table
tr => tr
td => td
p[style-name='Normal'] => p
p[style-name='List Paragraph'] => ul > li:fresh
header => div.header
footer => div.footer
`;
let result;
try {
result = await mammoth.convertToHtml({
arrayBuffer,
styleMap,
includeDefaultStyleMap: true,
extractImages: {
//
getAltText: (image) => {
return image.contentId; // alt
},
getImageUrl: (image) => {
// base64 URL
const contentType = image.contentType;
const base64Data = Buffer.from(image.data).toString('base64');
return `data:${contentType};base64,${base64Data}`;
}
},
transformDocument: (document) => {
//
const headers = document.children.filter(child => child.type === 'header');
const footers = document.children.filter(child => child.type === 'footer');
//
headers.forEach(header => {
document.children.push({
type: 'custom',
name: 'div',
attributes: { class: 'header' },
children: header.children
});
});
footers.forEach(footer => {
document.children.push({
type: 'custom',
name: 'div',
attributes: { class: 'footer' },
children: footer.children
});
});
return document;
}
});
} catch (convertError) {
console.error('Error converting DOC file to HTML:', convertError);
throw convertError;
}
console.log(result,"result信息"); console.log(result,"result信息");
// 线CSS
const cssStyles = `
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
}
</style>
`;
// HTMLhtmlContent // HTMLhtmlContent
htmlContent.value = result.value; // result.value HTML htmlContent.value = `${cssStyles}${result.value}`; // result.value HTML
// 使 result.messages // 使 result.messages
} catch (error) { } catch (error) {
console.error('Error fetching or converting DOC file:', error); console.error('Error fetching or converting DOC file:', error);
@ -38,13 +128,25 @@ fetchDocFile();
<template> <template>
<div> <div class="wrap">
<!-- <input type="file" @change="handleChange" /> -->
<!-- <VueOfficeDocx :src="docxSrc" :rendered="rendered" /> --> <!-- <VueOfficeDocx :src="docxSrc" :rendered="rendered" /> -->
<div v-html="htmlContent"></div> <div v-html="htmlContent" class="htmlContent"></div>
</div> </div>
</template> </template>
<style scoped></style> <style scoped>
.htmlContent {
height: 100vh;
overflow: auto;
background-color: #fff;
padding:50px;
}
table{
border-collapse: collapse;
}
td{
border:1px solid #999;
}
</style>

Loading…
Cancel
Save