pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

ciju private pastebin - collaborative debugging tool What's a private pastebin?


Posted by ciju on Sun 20 Jul 22:16
report abuse | download | new post

  1. #!/usr/bin/python
  2. import copy, getopt, sys, pprint
  3.  
  4. try:
  5.     from pdf import *
  6. except:
  7.     from pyPdf import PdfFileWriter, PdfFileReader
  8.    
  9. res, cont, xobj, bbox = "/Resources", "/Contents", "/XObject", "/BBox"
  10. def page4eachXobj(self, spage):
  11.     pl = {}
  12.     objlst = self[res].getObject()[xobj]
  13.  
  14.     for key in objlst.keys():
  15.         page = copy.copy(spage)
  16.         r,x,c = [ NameObject(n) for n in (res, xobj, cont) ]
  17.         page[r] = DictionaryObject()
  18.         page[r][x] = DictionaryObject()
  19.         page[r][x][key] = dict.__getitem__(objlst,key)
  20.         try:
  21.             page.mediaBox = page[r][x][key][bbox]
  22.         except: pass
  23.         cs = DecodedStreamObject()
  24.         cs.setData("q\n"+key+" Do\nQ")
  25.         page[c] = ContentStream(cs, page.pdf)
  26.         pl[key] = page
  27.  
  28.     # return pages in the order of original /Contents description
  29.     stream = self[cont].getObject().getData().split()
  30.     return [pl[x] for x in stream if x in pl.keys()]
  31.  
  32. def copy_page(page):
  33.     p = PageObject(page)
  34.     p.update(page)
  35.     p.mediaBox = copy.copy(page.mediaBox)
  36.     return p
  37.  
  38. def make_page(lx, ly, ux, uy, page, o):
  39.     p = copy_page(page)
  40.     p.mediaBox.lowerLeft = (lx, ly)
  41.     p.mediaBox.upperRight = (ux, uy)
  42.     return p
  43.  
  44. # range l, u divided into s+1 points.
  45. def get_points(l, u, s):
  46.     xl = [l]
  47.     for i in range(1, s+1): xl.append(i*(u-l)/s)
  48.     return xl
  49.  
  50. def split_slide(page, output, y, x, lst):
  51.     minx, miny = page.mediaBox.lowerLeft
  52.     maxx, maxy = page.mediaBox.upperRight
  53.  
  54.     xl = get_points(minx, maxx, int(x)) # columns
  55.     yl = get_points(miny, maxy, int(y)) # rows
  56.  
  57.     yl.reverse()
  58.  
  59.     pl=[]
  60.     for j in range(len(yl)-1):
  61.         for i in range(len(xl)-1):
  62.             pl.append(make_page(xl[i], yl[j+1], xl[i+1], yl[j]\
  63.                                     , page, output))
  64.     for i in lst:
  65.         output.addPage(pl[int(i)-1])
  66.            
  67.  
  68. def adv_split_slide(p, o):
  69.     for i in p.page4eachXobj(p):
  70.         o.addPage(i)
  71.  
  72.  
  73. def ssplit(readf, writef, fn, *arg):
  74.     out = PdfFileWriter()
  75.     inp = PdfFileReader(file(readf, "rb"))
  76.  
  77.     for i in range(inp.getNumPages()):
  78.         page = inp.getPage(i)
  79.         fn(page, out, *arg)
  80.  
  81.     outstream = file(writef, "wb")
  82.     out.write(outstream)
  83.  
  84. def slide_split(readf, writef, x, y, lst):
  85.     ssplit(readf, writef, split_slide, x, y, lst)
  86.  
  87. def adv_slide_split(readf, writef):
  88.     ssplit(readf, writef, adv_split_slide)
  89.  
  90. if __name__ == "__main__":
  91.     opts, args = getopt.getopt(sys.argv[1:], "")
  92.  
  93.     if len(args) < 4 :
  94.         print "Usage: "+sys.argv[0]+" <in.pdf> <out.pdf> <row> <col> <opt: slide order>"
  95.         print """
  96.        Mention a slide order, if the result doesn't match the order of
  97.        sequence you want. Example, if the 1st slide is at position 3, 2nd
  98.        at 2, 3rd at 1st, and 4th at 4th, slide order "3 2 1 4" would give
  99.        the required result."""
  100.         sys.exit(0)
  101.  
  102.     try:    setattr(PageObject, 'page4eachXobj', page4eachXobj)
  103.     except: pass
  104.  
  105.     try:
  106.         # split using XObjects
  107.         adv_slide_split(args[0], args[1])
  108.         print "xobject way"
  109.     except (KeyError, AttributeError):
  110.         # split the crude way
  111.         print "mediabox way"
  112.         r, c, lst = int(args[2]), int(args[3]), args[4:]
  113.         if (len(lst) != r*c): lst = range(1, r*c+1)
  114.         slide_split(args[0], args[1], r, c, lst)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post