diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index a554570..76fe59e 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -155,14 +155,39 @@ async def md_str_to_html(md_content, output_html): """ - # 异步写入 HTML 文件 - async with aiofiles.open(output_html, 'w', encoding='utf-8') as f: - await f.write('
') - await f.write('') # 确保 UTF-8 编码 - await f.write(css) - await f.write('') - await f.write(html_content) - await f.write('') + # 构建完整的 HTML 内容 + full_html = f''' + + + {css} + + + {html_content} + + ''' + + # 使用普通的文件写入,确保文件完全写入 + try: + with open(output_html, 'w', encoding='utf-8') as f: + f.write(full_html) + f.flush() # 强制刷新缓冲区 + os.fsync(f.fileno()) # 确保写入磁盘 + except Exception as e: + logger.error(f"写入HTML文件失败: {e}") + raise + + # 验证文件是否成功写入 + try: + with open(output_html, 'r', encoding='utf-8') as f: + content = f.read() + if not content: + raise ValueError("HTML文件写入后为空") + except Exception as e: + logger.error(f"验证HTML文件失败: {e}") + raise + + # 添加小延时确保文件系统同步 + await asyncio.sleep(0.5) def check_chromium_installed(path):