Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include addJS() patch in trunk #39

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ Example:
page5.mediaBox.getUpperRight_y() / 2
)
output.addPage(page5)


# add some Javascript to launch the print window on opening this PDF
output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

# print how many pages input1 has:
print "document1.pdf has %s pages." % input1.getNumPages())

Expand Down
30 changes: 29 additions & 1 deletion pyPdf/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,43 @@ def __init__(self):
})
self._info = self._addObject(info)


# js object
js = DictionaryObject()
js.update({
NameObject("/Type"): NameObject("/Action"),
NameObject("/S"): NameObject("/Javascript"),
NameObject("/JS"): NameObject(u"(app.alert({cMsg: 'Hello from PDF JavaScript', cTitle: 'Testing PDF JavaScript', nIcon: 3});)")
})

# root object
root = DictionaryObject()
root.update({
NameObject("/Type"): NameObject("/Catalog"),
NameObject("/Pages"): self._pages,
# NameObject("/OpenAction"): self._addObject(js),
})
self._root = self._addObject(root)
# self._root = self._addObject(root)
self._root = None
self.root = root

def _addObject(self, obj):
self._objects.append(obj)
return IndirectObject(len(self._objects), 0, self)

def addJS(self, str):
js = DictionaryObject()
js.update({
NameObject("/Type"): NameObject("/Action"),
NameObject("/S"): NameObject("/JavaScript"),
# NameObject("/JS"): NameObject(u"(app.alert({cMsg: 'Hello from PDF JavaScript', cTitle: 'Testing PDF JavaScript', nIcon: 3});)")
NameObject("/JS"): NameObject("(" + str + ")"),
})

self.root.update( {
NameObject("/OpenAction"): self._addObject(js)
} )

def getObject(self, ido):
if ido.pdf != self:
raise ValueError("pdf must be self")
Expand Down Expand Up @@ -240,6 +265,9 @@ def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True):
def write(self, stream):
import struct

if(not self._root):
self._root = self._addObject(self.root)

externalReferenceMap = {}

# PDF objects sometimes have circular references to their /Page objects
Expand Down