commit 8c9c6736d1e138163fa93aaa2341a5650f8aff39 Author: Artyom Titov Date: Mon Oct 12 15:55:15 2026 +0300 Add script with autoscaling diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a136337 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pdf diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..48cd3c9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +img2pdf=0.6.3 diff --git a/script.py b/script.py new file mode 100755 index 0000000..5191a8e --- /dev/null +++ b/script.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +import argparse +from pathlib import Path + +import img2pdf + + +SUPPORTED_EXTENSIONS = {".jpg", ".jpeg", ".png"} + + +def images_to_pdf(images: list[Path], output: Path) -> None: + # A4 page dimensions in PDF points (1 point = 1/72 inch). + a4_size = (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297)) + + # Scale images proportionally to fit within A4 pages. + layout = img2pdf.get_layout_fun( + pagesize=a4_size, + fit=img2pdf.FitMode.into, + auto_orient=True, + ) + + with output.open("wb") as pdf_file: + pdf_file.write( + img2pdf.convert( + [str(image) for image in images], #type: ignore + layout_fun=layout, + ) + ) + +def main() -> None: + parser = argparse.ArgumentParser( + description="Convert JPEG/PNG images into a PDF, one image per page." + ) + + parser.add_argument( + "images", + nargs="+", + type=Path, + help="Input JPEG/PNG images in page order", + ) + + parser.add_argument( + "-o", + "--output", + type=Path, + default=Path("output.pdf"), + help="Output PDF file (default: output.pdf)", + ) + + args = parser.parse_args() + + images_to_pdf(args.images, args.output) + + print(f"Created: {args.output}") + + +if __name__ == "__main__": + main()