| | |
| | | }); |
| | | }); |
| | | |
| | | return result.join('\n\n'); // 用两个换行符分隔段落 |
| | | return result.join('\n'); // 用两个换行符分隔段落 |
| | | } |
| | | |
| | | function convertTreeToHtml(data) { |
| | |
| | | } |
| | | 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; |
| | | const targetWidth = 550; |
| | | const scale = targetWidth / width; |
| | | let targetHeight = height * scale; |
| | | targetHeight = Math.max(100, Math.min(800, targetHeight)); |
| | | return [targetWidth, Math.round(targetHeight)]; |
| | | }, |
| | | }; |
| | | |
| | |
| | | // 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); |
| | |
| | | throw error; |
| | | } |
| | | }); |
| | | } |
| | | } |