验证PDF文件 Args: file_path: PDF文件路径 Returns: bool: 是否有效
(self, file_path: str)
| 158 | raise ConversionFailedError(f"PDF转换失败: {str(e)}") from e |
| 159 | |
| 160 | async def validate(self, file_path: str) -> bool: |
| 161 | """验证PDF文件 |
| 162 | |
| 163 | Args: |
| 164 | file_path: PDF文件路径 |
| 165 | |
| 166 | Returns: |
| 167 | bool: 是否有效 |
| 168 | """ |
| 169 | try: |
| 170 | # 检查文件是否存在 |
| 171 | if not os.path.exists(file_path): |
| 172 | logger.error(f"PDF文件不存在: {file_path}") |
| 173 | return False |
| 174 | |
| 175 | # 检查文件扩展名 |
| 176 | if not file_path.lower().endswith(".pdf"): |
| 177 | logger.error(f"不是PDF文件: {file_path}") |
| 178 | return False |
| 179 | |
| 180 | # 检查文件大小 |
| 181 | file_size = os.path.getsize(file_path) |
| 182 | if file_size == 0: |
| 183 | logger.error("PDF文件为空") |
| 184 | return False |
| 185 | |
| 186 | # 尝试打开PDF文件验证 |
| 187 | pdf_document = fitz.open(file_path) |
| 188 | page_count = pdf_document.page_count |
| 189 | pdf_document.close() |
| 190 | |
| 191 | if page_count == 0: |
| 192 | logger.error("PDF文件没有页面") |
| 193 | return False |
| 194 | |
| 195 | logger.info(f"PDF文件验证通过,共 {page_count} 页") |
| 196 | return True |
| 197 | |
| 198 | except Exception as e: |
| 199 | logger.error(f"PDF文件验证失败: {str(e)}") |
| 200 | return False |