(
files: list[str],
output: str = "",
pages: Optional[list[int]] = None,
lang_in: str = "",
lang_out: str = "",
service: str = "",
thread: int = 0,
vfont: str = "",
vchar: str = "",
callback: object = None,
compatible: bool = False,
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
envs: Dict = None,
prompt: Template = None,
skip_subset_fonts: bool = False,
ignore_cache: bool = False,
**kwarg: Any,
)
| 300 | |
| 301 | |
| 302 | def translate( |
| 303 | files: list[str], |
| 304 | output: str = "", |
| 305 | pages: Optional[list[int]] = None, |
| 306 | lang_in: str = "", |
| 307 | lang_out: str = "", |
| 308 | service: str = "", |
| 309 | thread: int = 0, |
| 310 | vfont: str = "", |
| 311 | vchar: str = "", |
| 312 | callback: object = None, |
| 313 | compatible: bool = False, |
| 314 | cancellation_event: asyncio.Event = None, |
| 315 | model: OnnxModel = None, |
| 316 | envs: Dict = None, |
| 317 | prompt: Template = None, |
| 318 | skip_subset_fonts: bool = False, |
| 319 | ignore_cache: bool = False, |
| 320 | **kwarg: Any, |
| 321 | ): |
| 322 | if not files: |
| 323 | raise PDFValueError("No files to process.") |
| 324 | |
| 325 | missing_files = check_files(files) |
| 326 | |
| 327 | if missing_files: |
| 328 | print("The following files do not exist:", file=sys.stderr) |
| 329 | for file in missing_files: |
| 330 | print(f" {file}", file=sys.stderr) |
| 331 | raise PDFValueError("Some files do not exist.") |
| 332 | |
| 333 | result_files = [] |
| 334 | |
| 335 | for file in files: |
| 336 | if type(file) is str and ( |
| 337 | file.startswith("http://") or file.startswith("https://") |
| 338 | ): |
| 339 | print("Online files detected, downloading...") |
| 340 | try: |
| 341 | r = requests.get(file, allow_redirects=True) |
| 342 | if r.status_code == 200: |
| 343 | with tempfile.NamedTemporaryFile( |
| 344 | suffix=".pdf", delete=False |
| 345 | ) as tmp_file: |
| 346 | print(f"Writing the file: {file}...") |
| 347 | tmp_file.write(r.content) |
| 348 | file = tmp_file.name |
| 349 | else: |
| 350 | r.raise_for_status() |
| 351 | except Exception as e: |
| 352 | raise PDFValueError( |
| 353 | f"Errors occur in downloading the PDF file. Please check the link(s).\nError:\n{e}" |
| 354 | ) |
| 355 | filename = os.path.splitext(os.path.basename(file))[0] |
| 356 | |
| 357 | # If the commandline has specified converting to PDF/A format |
| 358 | # --compatible / -cp |
| 359 | if compatible: |
no test coverage detected