import os
import sqlite3
import tempfile

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch

styles = getSampleStyleSheet ()
h1 = styles["h1"]
h2 = styles["h2"]
normal = styles["Normal"]

db = sqlite3.connect ("books.db")
reviews = {}
for date_added, title, synopsis in db.execute ("SELECT date_added, title, synopsis FROM whatsnew"):
  if date_added not in reviews:
    reviews[date_added] = []
  reviews[date_added].append ((title, synopsis))

pdf_file_name = tempfile.mktemp (".pdf")

doc = SimpleDocTemplate (pdf_file_name)

story = []
for date in reviews.keys ():
  story.append (Paragraph (str (date), h1))
  for title, synopsis in reviews[date]:
    print title
    story.append (Paragraph (title, h1))
    story.append (Paragraph (synopsis, normal))

doc.build (story)

os.startfile (pdf_file_name)