File size: 4,168 Bytes
ae6d295
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Test script for PDF generation from LaTeX.
This directly tests the PDF generation functionality without using Gradio.
"""

import os
import subprocess
import shutil

# Define paths
TEMPLATE_PATH = os.path.join("templates", "minimal_resume.tex")
OUTPUT_DIR = "outputs"
OUTPUT_TEX_PATH = os.path.join(OUTPUT_DIR, "test_resume.tex")

def add_why_hire_me_section(template_path, output_path):
    """Add a test Why Hire Me section to the resume template."""
    # The section needs to be properly placed after \begin{document}
    why_hire_me = """\\section*{Why Hire Me}
I am a highly motivated software engineer with extensive experience in web development.
My expertise in Python and JavaScript makes me an ideal candidate for this position.
"""
    
    # Read the template
    with open(template_path, "r") as f:
        content = f.read()
    
    # Replace the placeholder with the test section
    modified_content = content.replace("% WHY_HIRE_ME_SECTION", why_hire_me)
    
    # Write the modified content
    with open(output_path, "w") as f:
        f.write(modified_content)
    
    return output_path

def convert_to_pdf(tex_path):
    """Convert a LaTeX file to PDF."""
    # Get the directory and filename
    tex_dir = os.path.dirname(tex_path)
    tex_filename = os.path.basename(tex_path)
    
    # Change to the directory containing the tex file
    original_dir = os.getcwd()
    if tex_dir:  # Only change directory if tex_dir is not empty
        os.chdir(tex_dir)
    
    try:
        # Create a log file to capture output
        log_path = "pdflatex_log.txt"
        with open(log_path, 'w') as log_file:
            # Run pdflatex and capture output to the log file
            log_file.write("Running pdflatex - First pass\n")
            cmd = ['pdflatex', '-interaction=nonstopmode', tex_filename]
            log_file.write(f"Running command: {' '.join(cmd)}\n")
            result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
            log_file.write(f"Return code: {result.returncode}\n")
            log_file.write(f"STDOUT:\n{result.stdout}\n")
            log_file.write(f"STDERR:\n{result.stderr}\n\n")
        
        # Get the PDF path
        pdf_filename = tex_filename.replace('.tex', '.pdf')
        
        # Check if the PDF was actually created (in current directory)
        if os.path.exists(pdf_filename):
            pdf_path = os.path.abspath(pdf_filename)
            print(f"PDF created successfully: {pdf_path}")
            return pdf_path
        
        # Check if the PDF was created in the tex_dir
        pdf_in_texdir = os.path.join(tex_dir, pdf_filename) if tex_dir else pdf_filename
        if os.path.exists(pdf_in_texdir):
            pdf_path = os.path.abspath(pdf_in_texdir)
            print(f"PDF created successfully: {pdf_path}")
            return pdf_path
        
        print(f"PDF file not created. See log at: {os.path.abspath(log_path)}")
        return None
    except Exception as e:
        print(f"Error in PDF conversion: {str(e)}")
        return None
    finally:
        # Change back to the original directory
        if tex_dir:  # Only change back if we changed in the first place
            os.chdir(original_dir)

def main():
    """Main function to test the PDF generation."""
    print("Starting PDF generation test...")
    print(f"Using template: {os.path.abspath(TEMPLATE_PATH)}")
    
    # Make sure the output directory exists
    os.makedirs(OUTPUT_DIR, exist_ok=True)
    
    # Add the Why Hire Me section to the template
    modified_tex_path = add_why_hire_me_section(TEMPLATE_PATH, OUTPUT_TEX_PATH)
    print(f"Created modified LaTeX file: {os.path.abspath(modified_tex_path)}")
    
    # Convert to PDF
    pdf_path = convert_to_pdf(modified_tex_path)
    
    if pdf_path:
        print("PDF generation test completed successfully!")
        # Copy the PDF to the current directory for easier viewing
        shutil.copy(pdf_path, "test_resume.pdf")
        print(f"PDF copied to: {os.path.abspath('test_resume.pdf')}")
    else:
        print("PDF generation test failed.")

if __name__ == "__main__":
    main()