Advertisement
Guest User

feliam

a guest
Jan 11th, 2010
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.75 KB | None | 0 0
  1. ##########################################################################
  2. ####   Felipe Andres Manzano * felipe.andres.manzano@gmail.com        ####
  3. ##########################################################################
  4. '''
  5. A mini PDF library for constructing very basic PDF files.
  6. '''
  7.  
  8. import struct
  9.  
  10. #For constructing a minimal pdf file
  11. ## PDF REference 3rd edition:: 3.2 Objects
  12. class PDFObject:
  13.     def __init__(self):
  14.         self.n=None
  15.         self.v=None
  16.     def __str__(self):
  17.         raise "Fail"
  18.  
  19. ## PDF REference 3rd edition:: 3.2.1 Booleans Objects
  20. class PDFBool(PDFObject):
  21.     def __init__(self,s):
  22.         PDFObject.__init__(self)
  23.         self.s=s
  24.     def __str__(self):
  25.         if self.s:
  26.             return "true"
  27.         return "false"
  28.  
  29. ## PDF REference 3rd edition:: 3.2.2 Numeric Objects
  30. class PDFNum(PDFObject):
  31.     def __init__(self,s):
  32.         PDFObject.__init__(self)
  33.         self.s=s
  34.     def __str__(self):
  35.         return "%s"%self.s
  36.  
  37. ## PDF REference 3rd edition:: 3.2.3 String Objects
  38. class PDFString(PDFObject):
  39.     def __init__(self,s):
  40.         PDFObject.__init__(self)
  41.         self.s=s
  42.     def __str__(self):
  43.         return "(%s)"%self.s
  44.  
  45. ## PDF REference 3rd edition:: 3.2.3 String Objects / Hexadecimal Strings
  46. class PDFHexString(PDFObject):
  47.     def __init__(self,s):
  48.         PDFObject.__init__(self)
  49.         self.s=s
  50.     def __str__(self):
  51.         return "<" + "".join(["%02x"%ord(c) for c in self.s]) + ">"
  52.  
  53. ## A convenient type of literal Strings
  54. class PDFOctalString(PDFObject):
  55.     def __init__(self,s):
  56.         PDFObject.__init__(self)
  57.         self.s="".join(["\\%03o"%ord(c) for c in s])
  58.     def __str__(self):
  59.         return "(%s)"%self.s
  60.  
  61. ## PDF REference 3rd edition:: 3.2.4 Name Objects
  62. class PDFName(PDFObject):
  63.     def __init__(self,s):
  64.         PDFObject.__init__(self)
  65.         self.s=s
  66.     def __str__(self):
  67.         return "/%s"%self.s
  68.  
  69. ## PDF REference 3rd edition:: 3.2.5 Array Objects
  70. class PDFArray(PDFObject):
  71.     def __init__(self,s):
  72.         PDFObject.__init__(self)
  73.         self.s=s
  74.     def __str__(self):
  75.         return "[%s]"%(" ".join([ o.__str__() for o in self.s]))
  76.  
  77. ## PDF REference 3rd edition:: 3.2.6 Dictionary Objects
  78. class PDFDict(PDFObject):
  79.     def __init__(self, d={}):
  80.         PDFObject.__init__(self)
  81.         self.dict = {}
  82.         for k in d:
  83.             self.dict[k]=d[k]
  84.     def add(self,name,obj):
  85.         self.dict[name] = obj
  86.     def __str__(self):
  87.         s="<<"
  88.         for name in self.dict:
  89.             s+="%s %s "%(PDFName(name),self.dict[name])
  90.         s+=">>"
  91.         return s
  92.  
  93. ## PDF REference 3rd edition:: 3.2.7 Stream Objects
  94. class PDFStream(PDFDict):
  95.     def __init__(self,stream=""):
  96.         PDFDict.__init__(self)
  97.         self.stream=stream
  98.         self.filtered=self.stream
  99.         self.filters = []
  100.     def appendFilter(self, filter):
  101.         self.filters.append(filter)
  102.         self._applyFilters() #yeah every time .. so what!
  103.     def _applyFilters(self):
  104.         self.filtered = self.stream
  105.         for f in self.filters:
  106.                 self.filtered = f.encode(self.filtered)
  107.         self.add('Length', len(self.filtered))
  108.         if len(self.filters)>0:
  109.             self.add('Filter', PDFArray([f.name for f in self.filters]))
  110.         #Add Filter parameters ?
  111.     def __str__(self):
  112.         self._applyFilters() #yeah every time .. so what!
  113.         s=""
  114.         s+=PDFDict.__str__(self)
  115.         s+="\nstream\n"
  116.         s+=self.filtered
  117.         s+="\nendstream"
  118.         return s
  119.  
  120. ## PDF REference 3rd edition:: 3.2.8 Null Object
  121. class PDFNull(PDFObject):
  122.     def __init__(self):
  123.         PDFObject.__init__(self)
  124.  
  125.     def __str__(self):
  126.         return "null"
  127.  
  128. ## PDF REference 3rd edition:: 3.2.9 Indirect Objects
  129. class PDFRef(PDFObject):
  130.     def __init__(self,obj):
  131.         PDFObject.__init__(self)
  132.         self.obj=[obj]
  133.     def __str__(self):
  134.         return "%d %d R"%(self.obj[0].n,self.obj[0].v)
  135.  
  136. ## PDF REference 3rd edition:: 3.3 Filters
  137. ## Example Filter...
  138. class FlateDecode:
  139.     name = PDFName('FlateDecode')
  140.     def __init__(self):
  141.         pass
  142.     def encode(self,stream):
  143.         return zlib.compress(stream)
  144.     def decode(self,stream):
  145.         return zlib.decompress(stream)
  146.  
  147. ## PDF REference 3rd edition:: 3.4 File Structure
  148. ## Simplest file structure...
  149. class PDFDoc():
  150.     def __init__(self,obfuscate=0):
  151.         self.objs=[]
  152.         self.info=None
  153.         self.root=None
  154.     def setRoot(self,root):
  155.         self.root=root
  156.     def setInfo(self,info):
  157.         self.info=info
  158.     def _add(self,obj):
  159.         if obj.v!=None or obj.n!=None:
  160.             raise "Already added!!!"
  161.         obj.v=0
  162.         obj.n=1+len(self.objs)
  163.         self.objs.append(obj)
  164.     def add(self,obj):
  165.         if type(obj) != type([]):
  166.             self._add(obj);        
  167.         else:
  168.             for o in obj:  
  169.                 self._add(o)
  170.     def _header(self):
  171.         return "%PDF-1.3\n%\xE7\xF3\xCF\xD3\n"
  172.     def __str__(self):
  173.         doc1 = self._header()
  174.         xref = {}
  175.         for obj in self.objs:
  176.             xref[obj.n] = len(doc1)
  177.             doc1+="%d %d obj\n"%(obj.n,obj.v)
  178.             doc1+=obj.__str__()
  179.             doc1+="\nendobj\n"
  180.         posxref=len(doc1)
  181.         doc1+="xref\n"
  182.         doc1+="0 %d\n"%(len(self.objs)+1)
  183.         doc1+="0000000000 65535 f \n"
  184.         for xr in xref.keys():
  185.             doc1+= "%010d %05d n \n"%(xref[xr],0)
  186.         doc1+="trailer\n"
  187.         trailer =  PDFDict()
  188.         trailer.add("Size",len(self.objs)+1)
  189.         trailer.add("Root",PDFRef(self.root))
  190.         if self.info:  
  191.             trailer.add("Info",PDFRef(self.info))
  192.         doc1+=trailer.__str__()
  193.         doc1+="\nstartxref\n%d\n"%posxref
  194.         doc1+="%%EOF"
  195.         return doc1
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement