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
Javascript code added via addJS gets executed twice #482
Comments
here is the solution to your case. I had the same issue and after digging in the code of pdf.py file in PyPDF2 specifically the addJS() method on the PdfWriter class I found out that they are using """
This is the fix to not having the global JavaScript being added twice
instead of calling the original writer.addJS() method I overrode
the addJS method.
"""
def addJS(writer: PdfFileWriter, javascript, jsName):
"""
Add Javascript which will launch upon opening this PDF.
:param str javascript: Your Javascript.
>>> output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# Example: This will launch the print window when the PDF is opened.
"""
js = DictionaryObject()
js.update(
{
NameObject("/Type"): NameObject("/Action"),
NameObject("/S"): NameObject("/JavaScript"),
NameObject("/JS"): NameObject("(%s)" % javascript),
}
)
js_indirect_object = writer._addObject(js)
# We need a name for parameterized javascript in the pdf file, but it can be anything.
"""
Here is what i have changed
This change is not really necessary but instead of having
a long uuid string it is better to give a meaningful name
"""
# js_string_name = str(uuid.uuid4())
js_string_name = jsName
js_name_tree = DictionaryObject()
js_name_tree.update(
{
NameObject("/JavaScript"): DictionaryObject(
{
NameObject("/Names"): ArrayObject(
[createStringObject(js_string_name), js_indirect_object]
)
}
)
}
)
writer._addObject(js_name_tree)
"""
Here is what i have changed
I only replaced /OpenAction with /JavaScript
This will prevent having 2 global document entries
The /OpenAction creates a Doc Open on top of Document-Level which is unnecessary in our case
"""
writer._root_object.update(
{
# NameObject("/OenAction"): js_indirect_object,
NameObject("/JavaScript"): js_indirect_object,
NameObject("/Names"): js_name_tree,
}
)
return writer and her is how you call it from your python code
This will solve your problem as i have tested it and it is 100% working. |
Hi @youssef-jaber, there is a form of adding more than one javascript file.? When I try to use the .addJS() twice for some reason only keep the last one, I think that at some point it makes a new object with the last js code. |
Hi kenca23 |
|
fixes py-pdf#1425 and py-pdf#482 Also Introduces a new API opening in order to open a doc on a page or destination
Fixed by #1439 |
When using
addJS
with PyPDF 1.26.0 on Python 3.6.1 to add Javascript logic to a PDF, this Javascript gets executed twice when opening the PDF instead of once. When using the following sample code, the message box gets shown twice when opening the file in Adobe Reader XI on Windows 7 instead of only once as expected:Sample usage:
The text was updated successfully, but these errors were encountered: