Skip to content

Instantly share code, notes, and snippets.

@vagmi
Created November 18, 2008 10:29
Show Gist options
  • Save vagmi/26096 to your computer and use it in GitHub Desktop.
Save vagmi/26096 to your computer and use it in GitHub Desktop.
import re
from reportlab.platypus import Image, SimpleDocTemplate, Spacer,Flowable
from reportlab.lib.units import inch
import dircache
import Image as PILImage
import os.path
months = ["January", "Feburary", "March","April","May","June","July","August","September","October","November","December"]
class Bookmark(Flowable):
def __init__(self,title,key,level):
self.title=title
self.key=key
self.level=level
Flowable.__init__(self)
def wrap(self, availWidth, availHeight):
return (0,0)
def draw(self):
self.canv.showOutline()
self.canv.bookmarkPage(self.key)
self.canv.addOutlineEntry(self.title,self.key,self.level, 0)
base_dir='c:\\work\\toons\\dilbert\\'
files=filter(lambda x: x.endswith('gif'),dircache.listdir(base_dir))
doc=SimpleDocTemplate('dilbert.pdf')
oldyear=0
oldmonth=0
contents=[]
contents.append(Bookmark("Contents","Contents",0))
for f in files:
m=re.match(r'(\d\d\d\d)\-(\d\d)\-(\d\d)\.gif',f)
if(m==None):
continue
m=m.groups()
if(oldyear!=int(m[0]) or oldmonth!=int(m[1])):
contents.append( Bookmark("" + months[int(m[1])-1] + " " + str(int(m[0])),"" + months[int(m[1])-1] + " " + str(int(m[0])),1))
oldyear=int(m[0])
oldmonth=int(m[1])
#print os.path.join(base_dir,f)
fname=os.path.join(base_dir,f)
size=PILImage.open(fname).size
#print size
contents.append(Spacer(1,inch))
contents.append(Image(fname,doc.width,size[1]*(doc.width/size[0])))
print 'building...'
doc.build(contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment