Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####################################################################
- ## felipe.andres.manzano@gmail.com http://feliam.wordpress.com/ ##
- ## twitter.com/feliam http://www.linkedin.com/in/fmanzano ##
- ####################################################################
- from miniPDF import *
- import zlib,sys,md5
- def gotoE(name, next=None):
- #action
- action = PDFDict()
- action.add('S',PDFName('GoToE'))
- action.add('NewWindow',PDFBool(False))
- action.add('T',PDFDict({'N': name, 'R': PDFName('C'), 'NewWindow': PDFBool(False)}))
- if next:
- action.add('Next', next)
- return action
- def javascript(js):
- actionJS = PDFDict()
- actionJS.add('S', PDFName('JavaScript'))
- actionJS.add('JS', PDFString( js ))
- return actionJS
- class FlateDecode:
- name = PDFName('FlateDecode')
- def __init__(self):
- pass
- def encode(stream):
- return zlib.compress(stream)
- def decode(stream):
- return zlib.decompress(stream)
- def _zipEmbeddeFile(f,minimal=False):
- fileStr = file(f).read()
- ef = PDFStream(fileStr)
- if not minimal:
- ef.add('Type', PDFName('EmbeddedFile'))
- ef.add('Subtype',PDFName('application#2Fpdf'))
- ef.add('Params',PDFDict({'Size': PDFNum(len(fileStr)),
- 'CheckSum': PDFOctalString(md5.new(fileStr).digest())}) )
- ef.add('DL', ' %d '%len(fileStr))
- #ef.appendFilter(FlateDecode())
- return ef
- def hidePDF(filename):
- #The PDF document
- doc= PDFDoc()
- #pages
- pages = PDFDict()
- pages.add('Type', PDFName('Pages'))
- doc.add(pages)
- #catalog
- catalog = PDFDict()
- catalog.add('Type', PDFName('Catalog'))
- catalog.add('Pages', PDFRef(pages))
- doc.add(catalog)
- doc.setRoot(catalog)
- #empty contents for a dummy page
- contents = PDFStream('')
- doc.add(contents)
- #The pdf page
- page = PDFDict()
- page.add('Type', PDFName('Page'))
- page.add('Parent', PDFRef(pages))
- page.add('Contents', PDFRef(contents))
- doc.add(page)
- #link the page to the pages list
- pages.add('Kids',PDFArray([PDFRef(page)]))
- pages.add('Count', PDFNum(1))
- #embedded file stream
- embedded = _zipEmbeddeFile(filename)
- doc.add(embedded)
- #embedded list
- embeddedlst = PDFDict()
- embeddedlst.add('F',PDFRef(embedded))
- #fileSpec
- filespec = PDFDict()
- filespec.add('Type',PDFName('Filespec'))
- filespec.add('F',PDFString('file.pdf'))
- filespec.add('EF', embeddedlst)
- doc.add(filespec)
- #NamesToFiles
- namesToFiles = PDFDict()
- namesToFiles.add('Names', PDFArray([PDFHexString('attach'.encode('utf-16')),PDFRef(filespec)] ))
- #Global Names dictionary
- names = PDFDict()
- doc.add(names)
- catalog.add('Names', PDFRef(names))
- names.add('EmbeddedFiles',namesToFiles)
- #open action to page
- action = gotoE(PDFHexString('attach'.encode('utf-16')))
- doc.add(action)
- page.add('AA',PDFDict({'O': PDFRef(action)}))
- print doc
- hidePDF(sys.argv[1])
RAW Paste Data