diff --git a/src/components/PreViewFile/src/PreViewFile.vue b/src/components/PreViewFile/src/PreViewFile.vue
index ce165d6..81f132b 100644
--- a/src/components/PreViewFile/src/PreViewFile.vue
+++ b/src/components/PreViewFile/src/PreViewFile.vue
@@ -21,13 +21,103 @@ const fetchDocFile = async ()=>{
// 假设你有一个API可以返回DOC文件流
const response = await queryFileFlowRepTemplateApi(fileData);
console.log(response,"response文件流信息");
- const { data } = response;
- const arrayBuffer = new File([data], 'document.docx', { type: 'docx' }).arrayBuffer();
- console.log(arrayBuffer,"arrayBuffer");
- const result = mammoth.convertToHtml({ arrayBuffer });
- console.log(result,"result信息");
+ const { data, headers } = response;
+ if(headers['status'] == 500){// 说明此时下载文件有错误,需要获取WARN和ERROR的值
+ const code = headers['error']?headers['error'].substring(headers['error'].length-4):'1003';
+ 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信息");
+ // 添加边框线的CSS样式
+ const cssStyles = `
+
+ `;
// 将生成的HTML内容赋值给htmlContent
- htmlContent.value = result.value; // result.value 包含转换后的HTML
+ htmlContent.value = `${cssStyles}${result.value}`; // result.value 包含转换后的HTML
// 如果需要显示消息,可以使用 result.messages
} catch (error) {
console.error('Error fetching or converting DOC file:', error);
@@ -38,13 +128,25 @@ fetchDocFile();
-