Skip to content

Commit

Permalink
Improvements to PDF generation
Browse files Browse the repository at this point in the history
  • Loading branch information
btfranklin committed Aug 29, 2024
1 parent fce0f24 commit 0394ced
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
32 changes: 23 additions & 9 deletions src/spooklight/completion/build_pdf_from_story_files.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from colorama import Back, Fore
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, Spacer, PageBreak
from reportlab.lib.units import inch

from spooklight.settings import Settings
Expand All @@ -23,17 +23,31 @@ def build_pdf_from_story_files() -> None:
styles = getSampleStyleSheet()
story_flow = []

# Add a title page
# Add a title page with the story concept (Concept)
title_file = os.path.join(output_dir, "title.txt")
concept_file = os.path.join(output_dir, "concept.txt")

if os.path.exists(title_file):
with open(title_file, "r") as f:
title = f.read().strip()
title_style = styles["Title"]
story_flow.append(Paragraph(title, title_style))
story_flow.append(Spacer(1, 2 * inch)) # Adds some space after the title
story_flow.append(Spacer(1, 0.5 * inch)) # Adds some space after the title
else:
print(Fore.RED + f"Warning: Title file '{title_file}' not found.")

if os.path.exists(concept_file):
with open(concept_file, "r") as f:
concept = f.read().strip()
concept_heading = Paragraph("<b>Concept</b>", styles["Heading2"])
concept_text = Paragraph(concept, styles["BodyText"])
story_flow.append(concept_heading)
story_flow.append(Spacer(1, 0.2 * inch)) # Space after the heading
story_flow.append(concept_text)
story_flow.append(PageBreak()) # Start a new page after the concept
else:
print(Fore.RED + f"Warning: Concept file '{concept_file}' not found.")

# Add each step to the PDF
step = 0
while True:
Expand All @@ -43,15 +57,15 @@ def build_pdf_from_story_files() -> None:
if not os.path.exists(narrative_file) or not os.path.exists(image_file):
break

# Add image
story_flow.append(Image(image_file, width=6 * inch, height=4.5 * inch))
story_flow.append(Spacer(1, 0.5 * inch)) # Space between image and narrative

# Add narrative
with open(narrative_file, "r") as f:
narrative = f.read().strip()
story_flow.append(Paragraph(narrative, styles["BodyText"]))
story_flow.append(Spacer(1, 0.5 * inch)) # Space between narrative and image

# Add image
story_flow.append(Image(image_file, width=6 * inch, height=4.5 * inch))
story_flow.append(Spacer(1, 1 * inch)) # Space before the next step
story_flow.append(PageBreak()) # Start a new page for the next step

step += 1

Expand Down
6 changes: 5 additions & 1 deletion src/spooklight/generate_story.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def generate_story(
starting_image_description=starting_image_description,
)

# Save the story concept to a file in the output directory with a name like "concept.txt"
output_dir = Settings.get_output_directory()
with open(f"{output_dir}/concept.txt", "w") as f:
f.write(story.concept)

# Generate the first story step
generate_first_step(
llm_client=llm_client,
Expand All @@ -57,7 +62,6 @@ def generate_story(
story.title = generate_title_from_narrative(llm_client, story)

# Save the title to a file in the output directory with a name like "title.txt"
output_dir = Settings.get_output_directory()
with open(f"{output_dir}/title.txt", "w") as f:
f.write(story.title)

Expand Down

0 comments on commit 0394ced

Please sign in to comment.