|
| 1 | +""" |
| 2 | +svg2pdf.py - Utility to convert SVG images to PDF format. |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | +#import argparse |
| 7 | +from pathlib import Path |
| 8 | +from svglib.svglib import svg2rlg |
| 9 | +from reportlab.graphics import renderPDF |
| 10 | + |
| 11 | +#---------------------------------- |
| 12 | +# Convert an SVG file to PDF format |
| 13 | +#---------------------------------- |
| 14 | +def convert_svg_to_pdf(input_file: str, output_dir: str = None) -> str: |
| 15 | + input_path = Path(input_file) |
| 16 | + if not input_path.exists(): |
| 17 | + print(f"Error: File not found: {input_file}") |
| 18 | + return None |
| 19 | + |
| 20 | + # If input is not an SVG file, skip it |
| 21 | + if input_path.suffix.lower() != '.svg': |
| 22 | + print(f"Skipping non-SVG file: {input_file}") |
| 23 | + return None |
| 24 | + |
| 25 | + # Determine output path (same name, but in output_dir if specified) |
| 26 | + if output_dir: |
| 27 | + output_path = Path(output_dir) / input_path.with_suffix('.pdf').name |
| 28 | + else: |
| 29 | + output_path = input_path.with_suffix('.pdf') |
| 30 | + |
| 31 | + try: |
| 32 | + # Create output directory if needed |
| 33 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 34 | + |
| 35 | + # SVG → PDF conversion |
| 36 | + drawing = svg2rlg(str(input_path)) |
| 37 | + renderPDF.drawToFile(drawing, str(output_path)) |
| 38 | + |
| 39 | + print(f"Conversion completed: {input_path} → {output_path}") |
| 40 | + return str(output_path) |
| 41 | + except Exception as e: |
| 42 | + print(f"Error during conversion: {e}") |
| 43 | + return None |
| 44 | + |
| 45 | +#-------------------------------------------------- |
| 46 | +#Convert all SVG files in a directory to PDF format |
| 47 | +#-------------------------------------------------- |
| 48 | +def convert_directory(input_dir: str, output_dir: str = None) -> int: |
| 49 | + input_path = Path(input_dir) |
| 50 | + if not input_path.is_dir(): |
| 51 | + print(f"Error: Directory not found: {input_dir}") |
| 52 | + return 0 |
| 53 | + |
| 54 | + # Find all SVG files in the directory |
| 55 | + svg_files = list(input_path.glob("*.svg")) |
| 56 | + |
| 57 | + if not svg_files: |
| 58 | + print(f"No SVG files found in {input_dir}") |
| 59 | + return 0 |
| 60 | + |
| 61 | + successful = 0 |
| 62 | + for svg_file in svg_files: |
| 63 | + if convert_svg_to_pdf(str(svg_file), output_dir): |
| 64 | + successful += 1 |
| 65 | + |
| 66 | + return successful |
| 67 | + |
| 68 | +#----------------- |
| 69 | +#Prompt usage info |
| 70 | +#----------------- |
| 71 | +def show_usage(): |
| 72 | + """Display usage information.""" |
| 73 | + print("Usage: svg2pdf.py [options] <svg_file(s)>") |
| 74 | + print(" svg2pdf.py -d <input_directory> [options]") |
| 75 | + print("\nOptions:") |
| 76 | + print(" -h, --help Show this help message and exit") |
| 77 | + print(" -d, --dir <directory> Process input as directory (convert all SVG files in it)") |
| 78 | + print(" -o, --output <directory> Specify output directory for PDF files") |
| 79 | + print("\nExamples:") |
| 80 | + print(" svg2pdf.py image.svg # Convert single SVG file") |
| 81 | + print(" svg2pdf.py image1.svg image2.svg # Convert multiple SVG files") |
| 82 | + print(" svg2pdf.py -d svgs/ # Convert all SVGs in directory") |
| 83 | + print(" svg2pdf.py image.svg -o pdfs/ # Convert file and save to specific directory") |
| 84 | + print(" svg2pdf.py -d svgs/ -o pdfs/ # Convert all SVGs in svgs/ and save to pdfs/") |
| 85 | + |
| 86 | +#----------- |
| 87 | +# Main func |
| 88 | +#----------- |
| 89 | +def main(): |
| 90 | + |
| 91 | + args = sys.argv[1:] |
| 92 | + |
| 93 | + if not args: |
| 94 | + show_usage() |
| 95 | + return |
| 96 | + |
| 97 | + if '-h' in args or '--help' in args: |
| 98 | + show_usage() |
| 99 | + return |
| 100 | + |
| 101 | + input_dir = None |
| 102 | + output_dir = None |
| 103 | + input_files = [] |
| 104 | + dir_mode = False |
| 105 | + |
| 106 | + # Process args |
| 107 | + i = 0 |
| 108 | + while i < len(args): |
| 109 | + if args[i] == '-d' or args[i] == '--dir': |
| 110 | + dir_mode = True |
| 111 | + if i + 1 < len(args) and not args[i+1].startswith('-'): |
| 112 | + input_dir = args[i+1] |
| 113 | + i += 2 |
| 114 | + else: |
| 115 | + print("Error: No directory specified after -d/--dir flag") |
| 116 | + show_usage() |
| 117 | + return |
| 118 | + elif args[i] == '-o' or args[i] == '--output': |
| 119 | + if i + 1 < len(args) and not args[i+1].startswith('-'): |
| 120 | + output_dir = args[i+1] |
| 121 | + i += 2 |
| 122 | + else: |
| 123 | + print("Error: No directory specified after -o/--output flag") |
| 124 | + show_usage() |
| 125 | + return |
| 126 | + elif args[i].startswith('-'): |
| 127 | + print(f"Error: Unknown option {args[i]}") |
| 128 | + show_usage() |
| 129 | + return |
| 130 | + else: |
| 131 | + input_files.append(args[i]) |
| 132 | + i += 1 |
| 133 | + |
| 134 | + # Execute in directory mode |
| 135 | + if dir_mode: |
| 136 | + if not input_dir: |
| 137 | + print("Error: No input directory specified") |
| 138 | + show_usage() |
| 139 | + return |
| 140 | + |
| 141 | + if input_files: |
| 142 | + print("Warning: Additional arguments ignored in directory mode") |
| 143 | + |
| 144 | + num_converted = convert_directory(input_dir, output_dir) |
| 145 | + print(f"Successfully converted {num_converted} SVG files") |
| 146 | + return |
| 147 | + |
| 148 | + # Otherwise, process individual files |
| 149 | + if not input_files: |
| 150 | + print("Error: No input files specified") |
| 151 | + show_usage() |
| 152 | + return |
| 153 | + |
| 154 | + successful = 0 |
| 155 | + for input_file in input_files: |
| 156 | + if convert_svg_to_pdf(input_file, output_dir): |
| 157 | + successful += 1 |
| 158 | + |
| 159 | + print(f"Successfully converted {successful} out of {len(input_files)} files") |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + main() |
| 164 | + |
0 commit comments