utils module provides essential utility functions for the manga downloader, including filename sanitization, asynchronous image downloading, and PDF generation.
clean_filename()
Sanitizes strings to create valid file or directory names compatible with Windows and Linux.Parameters
The raw title string to be sanitized.
Returns
A safe filename string with HTML tags stripped and reserved characters removed. Returns
"untitled" if the input is empty or results in an empty string after sanitization.Behavior
- Strips HTML tags using regex pattern
<[^>]+> - Removes reserved characters:
\ / * ? : " < > | - Trims leading and trailing whitespace
- Returns
"untitled"as fallback for empty inputs
Usage example
download_image()
Downloads a single image asynchronously and saves it to a local directory with proper ordering.Parameters
The active asynchronous HTTP session for making requests.
The direct image URL to download.
The local directory path to save the image (typically a temporary folder).
The page order index used for filename generation. Ensures correct sorting in PDF creation.
Callback function to send error messages and logs to the frontend.
HTTP headers to bypass bot protections (Cloudflare, User-Agent, Referer, etc.).
Returns
The absolute path to the downloaded image file if successful, otherwise
None.Behavior
- Automatically detects file extension from URL (
.jpg,.png,.webp,.jpeg,.avif) - Generates zero-padded filenames (e.g.,
001.jpg,042.png) - Downloads with provided headers to bypass protections
- Logs errors through callback on failure
Usage example
create_pdf()
Compiles a list of image paths into a single PDF file usingimg2pdf (preferred) or Pillow (fallback).
Parameters
List of absolute file paths to images to be compiled into the PDF.
Absolute path where the generated PDF should be saved.
Callback function to emit log messages about the PDF generation process.
Returns
True if PDF generation succeeded, False if it failed.Behavior
- Image conversion: Automatically converts RGBA, LA, and transparent images to RGB/JPEG
- Primary method: Uses
img2pdflibrary for lossless PDF generation - Fallback method: Uses Pillow if
img2pdffails or is unavailable - Error handling: Logs detailed error messages through callback
- Path logging: Displays relative path from PDF folder for cleaner logs
Usage example
finalize_pdf_flow()
Orchestrates the final steps of PDF creation: generates the PDF, optionally opens it, and cleans up temporary files.Parameters
List of downloaded image file paths to compile.
Filename for the output PDF (not a full path, just the filename).
Callback function to emit status messages.
Optional path to temporary directory to delete after PDF creation.
Whether to automatically open the PDF file and its folder after creation (Windows only).
Returns
This function does not return a value.Behavior
- Creates PDF folder if it doesn’t exist
- Generates PDF using
create_pdf() - Opens PDF folder and file if
open_result=True(Windows only viaos.startfile()) - Deletes temporary directory if provided
- Logs completion status
Usage example
download_and_make_pdf()
Main orchestration function that handles the complete workflow: downloads images in batches, creates PDF, and cleans up.Parameters
List of image URLs to download and compile into PDF.
Output filename (or full path if
is_path=True).HTTP headers for image download requests.
Callback function for log messages.
Function that returns
True if the user cancelled the operation.Optional callback for progress updates with
(current, total) parameters.If
True, treats output_name as a full path instead of just a filename.Whether to automatically open the PDF after creation.
Returns
This function does not return a value.Behavior
- Temp folder setup: Creates/cleans temporary download folder
- Batch downloading: Downloads images in chunks defined by
BATCH_SIZE - Cancellation check: Checks for user cancellation between batches
- Progress updates: Calls progress callback after each batch
- PDF creation: Compiles downloaded images into PDF
- Cleanup: Removes temporary folder
- Completion logging: Emits
[DONE] Finished.message