祖安之光
8 天以前 89cb82acdcb3fa13ec3119bfc9ddcb781d684412
src/views/build/conpanyFunctionConsult/digitalFileDep/manageType/qualityManual/components/exportDoc.js
@@ -271,12 +271,111 @@
    }
    return bytes.buffer;
}
function getDimensionsFromBase64Sync(base64Str) {
    try {
        // 去除 data:image/...;base64, 前缀
        const base64Data = base64Str.replace(/^data:image\/\w+;base64,/, '');
        // 将base64转换为二进制
        const binaryString = atob(base64Data);
        const bytes = new Uint8Array(binaryString.length);
        for (let i = 0; i < binaryString.length; i++) {
            bytes[i] = binaryString.charCodeAt(i);
        }
        return parseImageDimensions(bytes);
    } catch (error) {
        console.warn('解析图片尺寸失败:', error);
        return { width: 550, height: 400 };
    }
}
function parseImageDimensions(bytes) {
    if (bytes.length < 8) {
        return { width: 550, height: 400 };
    }
    // 检查 PNG 格式 (89 50 4E 47 0D 0A 1A 0A)
    if (bytes[0] === 0x89 && bytes[1] === 0x50 &&
        bytes[2] === 0x4E && bytes[3] === 0x47) {
        return parsePNGDimensions(bytes);
    }
    // 检查 JPEG 格式 (FF D8)
    if (bytes[0] === 0xFF && bytes[1] === 0xD8) {
        return parseJPEGDimensions(bytes);
    }
    return { width: 550, height: 400 };
}
// 解析 PNG 尺寸
function parsePNGDimensions(bytes) {
    // PNG 的宽高在 IHDR chunk 中(偏移 16-24 字节)
    if (bytes.length >= 24) {
        const view = new DataView(bytes.buffer);
        const width = view.getUint32(16, false);  // 大端序
        const height = view.getUint32(20, false);
        return { width, height };
    }
    return { width: 550, height: 400 };
}
// 解析 JPEG 尺寸
function parseJPEGDimensions(bytes) {
    let i = 2; // 跳过 FFD8
    while (i < bytes.length - 1) {
        // JPEG 标记开始
        if (bytes[i] === 0xFF) {
            const marker = bytes[i + 1];
            // SOF0, SOF1, SOF2 (Start of Frame markers)
            if ((marker >= 0xC0 && marker <= 0xC3) ||
                (marker >= 0xC5 && marker <= 0xC7) ||
                (marker >= 0xC9 && marker <= 0xCB) ||
                (marker >= 0xCD && marker <= 0xCF)) {
                if (i + 7 < bytes.length) {
                    const height = (bytes[i + 5] << 8) | bytes[i + 6];
                    const width = (bytes[i + 7] << 8) | bytes[i + 8];
                    return { width, height };
                }
                break;
            }
            // 跳过当前段
            const length = (bytes[i + 2] << 8) | bytes[i + 3];
            i += length + 2;
        } else {
            i++;
        }
    }
    return { width: 550, height: 400 };
}
const imageOptions = {
    getImage(tagValue) {
        return base64Parser(tagValue);
    },
    getSize(img, tagValue, tagName, context) {
        return [600, 600];
        const dimensions = getDimensionsFromBase64Sync(tagValue);
        const { width, height } = dimensions;
        if(tagName == 'sign1' || tagName == 'sign2' || tagName == 'sign3' || tagName == 'sign4'){
            const targetWidth = 160;
            const scale = targetWidth / width;
            let targetHeight = height * scale;
            targetHeight = Math.max(100, Math.min(400, targetHeight));
            return [targetWidth, Math.round(targetHeight)];
        }else{
            const targetWidth = 550;
            const scale = targetWidth / width;
            let targetHeight = height * scale;
            targetHeight = Math.max(100, Math.min(800, targetHeight));
            return [targetWidth, Math.round(targetHeight)];
        }
    },
};
@@ -309,11 +408,27 @@
    //     data.departmentsTable = processTableHtml(tableHtml);
    // }
    // 生成表格XML
    // if (data.clauses && data.duties) {
    //     data.tableXML = generateTableXML(data.clauses, data.duties);
    // }
    if (data.productServiceImages && Array.isArray(data.productServiceImages)) {
        // 确保是纯 base64 字符串数组
        data.productServiceImageArray = data.productServiceImages.map(item =>
            typeof item === 'object' ? item.image : item
        ).filter(img => img && typeof img === 'string');
        // 为前10张图片创建单独的图片变量
        data.productServiceImageArray.slice(0, 10).forEach((img, index) => {
            data[`productServiceImage${index + 1}`] = img;
        });
        // 创建带元数据的对象数组
        data.productServiceImageObjects = data.productServiceImageArray.map((img, index) => ({
            image: img,  // 这个字段会作为图片插入
            index: index + 1,
            description: `产品和服务实现过程图 ${index + 1}`
        }));
        data.productServiceCount = data.productServiceImageArray.length;
        data.hasProductServiceImages = data.productServiceImageArray.length > 0;
    }
    // 处理富文本字段(如果有)
    if (data.summaries && typeof data.summaries === 'string') {
        data.summaries = processRichText(data.summaries);
@@ -321,7 +436,18 @@
    if (data.policies && typeof data.policies === 'string') {
        data.policies = processRichText(data.policies);
    }
    if (data.proclaim1 && typeof data.proclaim1 === 'string') {
        data.proclaim1 = processRichText(data.proclaim1);
    }if (data.proclaim2 && typeof data.proclaim2 === 'string') {
        data.proclaim2 = processRichText(data.proclaim2);
    }if (data.proclaim3 && typeof data.proclaim3 === 'string') {
        data.proclaim3 = processRichText(data.proclaim3);
    }if (data.proclaim4 && typeof data.proclaim4 === 'string') {
        data.proclaim4 = processRichText(data.proclaim4);
    }
    // 处理树形结构数据(如果有)
    if (data.deptList && Array.isArray(data.deptList)) {
        data.departmentsHtml = processRichText(convertTreeToHtml(data.deptList));
@@ -329,6 +455,22 @@
    if (data.orgChart && typeof data.orgChart !== 'string') {
        console.warn("orgChart 不是字符串,可能被意外转换:", data.orgChart);
        delete data.orgChart; // 避免传递无效数据
    }
    if (data.sign1 && typeof data.sign1 !== 'string') {
        console.warn("sign1 不是字符串,可能被意外转换:", data.sign1);
        delete data.sign1; // 避免传递无效数据
    }
    if (data.sign2 && typeof data.sign2 !== 'string') {
        console.warn("sign1 不是字符串,可能被意外转换:", data.sign2);
        delete data.sign2; // 避免传递无效数据
    }
    if (data.sign3 && typeof data.sign3 !== 'string') {
        console.warn("sign1 不是字符串,可能被意外转换:", data.sign3);
        delete data.sign3; // 避免传递无效数据
    }
    if (data.sign4 && typeof data.sign4 !== 'string') {
        console.warn("sign1 不是字符串,可能被意外转换:", data.sign4);
        delete data.sign4; // 避免传递无效数据
    }
    loadFile(templatePath, function (error, content) {
@@ -369,4 +511,4 @@
            throw error;
        }
    });
}
}