Guest User

feliam

a guest
Jan 12th, 2010
642
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ####################################################################
  2. ## felipe.andres.manzano@gmail.com  http://feliam.wordpress.com/  ##
  3. ## twitter.com/feliam        http://www.linkedin.com/in/fmanzano  ##
  4. ####################################################################
  5. from miniPDF import *
  6. import zlib,sys,md5
  7.  
  8. def gotoE(name, next=None):
  9.     #action
  10.     action = PDFDict()
  11.     action.add('S',PDFName('GoToE'))
  12.     action.add('NewWindow',PDFBool(False))
  13.     action.add('T',PDFDict({'N': name, 'R': PDFName('C'), 'NewWindow': PDFBool(False)}))
  14.     if next:
  15.         action.add('Next', next)
  16.     return action
  17.  
  18.  
  19. def javascript(js):
  20.     actionJS = PDFDict()
  21.     actionJS.add('S', PDFName('JavaScript'))
  22.     actionJS.add('JS', PDFString( js ))
  23.     return actionJS
  24.  
  25. class FlateDecode:
  26.     name = PDFName('FlateDecode')
  27.     def __init__(self):
  28.         pass
  29.     def encode(stream):
  30.         return zlib.compress(stream)
  31.     def decode(stream):
  32.         return zlib.decompress(stream)
  33.  
  34.  
  35. def _zipEmbeddeFile(f,minimal=False):
  36.     fileStr = file(f).read()
  37.     ef = PDFStream(fileStr)
  38.     if not minimal:
  39.         ef.add('Type', PDFName('EmbeddedFile'))
  40.         ef.add('Subtype',PDFName('application#2Fpdf'))
  41.         ef.add('Params',PDFDict({'Size': PDFNum(len(fileStr)),
  42.                                  'CheckSum': PDFOctalString(md5.new(fileStr).digest())}) )
  43.         ef.add('DL', ' %d '%len(fileStr))
  44.     #ef.appendFilter(FlateDecode())
  45.     return ef
  46.  
  47.  
  48. def hidePDF(filename):
  49.     #The PDF document
  50.     doc= PDFDoc()
  51.     #pages
  52.     pages = PDFDict()
  53.     pages.add('Type', PDFName('Pages'))
  54.     doc.add(pages)
  55.     #catalog
  56.     catalog = PDFDict()
  57.     catalog.add('Type', PDFName('Catalog'))
  58.     catalog.add('Pages', PDFRef(pages))
  59.     doc.add(catalog)
  60.     doc.setRoot(catalog)
  61.  
  62.     #empty contents for a dummy page
  63.     contents = PDFStream('')
  64.     doc.add(contents)
  65.  
  66.     #The pdf page
  67.     page = PDFDict()
  68.     page.add('Type', PDFName('Page'))
  69.     page.add('Parent', PDFRef(pages))
  70.     page.add('Contents', PDFRef(contents))
  71.     doc.add(page)
  72.  
  73.     #link the page to the pages list
  74.     pages.add('Kids',PDFArray([PDFRef(page)]))
  75.     pages.add('Count', PDFNum(1))
  76.  
  77.     #embedded file stream
  78.     embedded = _zipEmbeddeFile(filename)
  79.     doc.add(embedded)
  80.  
  81.     #embedded list
  82.     embeddedlst = PDFDict()
  83.     embeddedlst.add('F',PDFRef(embedded))
  84.  
  85.     #fileSpec
  86.     filespec = PDFDict()
  87.     filespec.add('Type',PDFName('Filespec'))
  88.     filespec.add('F',PDFString('file.pdf'))
  89.     filespec.add('EF', embeddedlst)
  90.     doc.add(filespec)
  91.  
  92.     #NamesToFiles
  93.     namesToFiles = PDFDict()
  94.     namesToFiles.add('Names', PDFArray([PDFHexString('attach'.encode('utf-16')),PDFRef(filespec)]  ))
  95.  
  96.     #Global Names dictionary
  97.     names = PDFDict()
  98.     doc.add(names)
  99.     catalog.add('Names', PDFRef(names))
  100.     names.add('EmbeddedFiles',namesToFiles)
  101.  
  102.     #open action to page
  103.     action = gotoE(PDFHexString('attach'.encode('utf-16')))
  104.     doc.add(action)
  105.     page.add('AA',PDFDict({'O': PDFRef(action)}))
  106.  
  107.     print doc
  108.  
  109. hidePDF(sys.argv[1])
  110.  
  111.  
  112.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×