#!/usr/bin/env python3 import subprocess import os # Test PDF to TIFF-F conversion pdf_path = "/Users/accusys/telfax/test/testfax.pdf" tiff_path = "/Users/accusys/telfax/test/testfax_converted.tif" print("=== Testing PDF → TIFF-F Conversion ===") # Run ghostscript result = subprocess.run([ "gs", "-dBATCH", "-dNOPAUSE", "-q", "-sDEVICE=tiffg3", "-r204x196", # Fine resolution "-sOutputFile", tiff_path, pdf_path ], capture_output=True, text=True) if result.returncode != 0: print(f"Ghostscript failed: {result.stderr}") exit(1) # Check output if os.path.exists(tiff_path): size = os.path.getsize(tiff_path) print(f"✓ TIFF-F created: {tiff_path}") print(f" Size: {size} bytes") # Get TIFF info import struct with open(tiff_path, 'rb') as f: header = f.read(8) if header[:2] == b'II': print(" Byte order: Little-endian") elif header[:2] == b'MM': print(" Byte order: Big-endian") # Read IFD offset if header[:2] == b'II': ifd_offset = struct.unpack('I', header[4:8])[0] f.seek(ifd_offset) num_entries = struct.unpack('H', f.read(2))[0] print(f" IFD entries: {num_entries}") # Read key tags for i in range(num_entries): tag = struct.unpack('H', f.read(2))[0] type_ = struct.unpack('H', f.read(2))[0] count = struct.unpack('I', f.read(4))[0] value_offset = f.read(4) # Tags of interest: 256=width, 257=height, 259=compression, 262=photometric if tag == 256: # Width val = struct.unpack('I', value_offset)[0] print(f" Width: {val} pixels") elif tag == 257: # Height val = struct.unpack('I', value_offset)[0] print(f" Height: {val} lines") elif tag == 259: # Compression val = struct.unpack('H', value_offset[:2])[0] comp_names = {1: "None", 3: "Group 3 MH", 4: "Group 4 MMR"} print(f" Compression: {comp_names.get(val, val)}") elif tag == 262: # PhotometricInterpretation val = struct.unpack('H', value_offset[:2])[0] photo_names = {0: "WhiteIsZero", 1: "BlackIsZero"} print(f" Photometric: {photo_names.get(val, val)}") else: print("✗ TIFF-F not created") exit(1) print("\n=== Conversion Test Passed ===")