Skip to content

ENH: Add page-level actions #3382

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

ENH: Add page-level actions #3382

wants to merge 10 commits into from

Conversation

j-t-1
Copy link
Contributor

@j-t-1 j-t-1 commented Jul 16, 2025

Sets entries in a page object’s additional-actions dictionary.

@j-t-1
Copy link
Contributor Author

j-t-1 commented Jul 16, 2025

Ideally want to be able to add open and close actions. To avoid a future deprecation, can we add this as _add_js for now?

@stefan6419846
Copy link
Collaborator

To avoid a future deprecation, can we add this as _add_js for now?

I do not understand what you are referring to. Could you please elaborate?

@j-t-1
Copy link
Contributor Author

j-t-1 commented Jul 16, 2025

I could have described better...

def add_js(self, javascript: str, /, *, open_action: bool = True) -> None:

Currently adds either open or close page action. To be able to add both will require modifying the function signature. Or maybe change it so that if given an open action keeps any close action and vice versa.

@stefan6419846
Copy link
Collaborator

We shouldn't have public API that is internal as it might break in the future.

Do you have an actual use case for implementing this? Or is this just because the implementation allows this? I have to be honest that I personally never actively used Javascript in PDF files.

@j-t-1
Copy link
Contributor Author

j-t-1 commented Jul 16, 2025

Let me know if you have a good way to modify this, or the functionality requirements needed.

Currently use the catalog OpenAction but the JavaScript at page-level is more flexible to only run the JavaScript once opening or closing a page.

Will also do a new PR for these tests, as could be in same function?

def test_add_js(pdf_file_writer):
pdf_file_writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
assert (
"/Names" in pdf_file_writer._root_object
), "add_js should add a name catalog in the root object."
assert (
"/JavaScript" in pdf_file_writer._root_object["/Names"]
), "add_js should add a JavaScript name tree under the name catalog."
def test_added_js(pdf_file_writer):
def get_javascript_name() -> Any:
assert "/Names" in pdf_file_writer._root_object
assert "/JavaScript" in pdf_file_writer._root_object["/Names"]
assert "/Names" in pdf_file_writer._root_object["/Names"]["/JavaScript"]
return pdf_file_writer._root_object["/Names"]["/JavaScript"]["/Names"][
-2
] # return -2 in order to get the latest javascript
pdf_file_writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
first_js = get_javascript_name()
pdf_file_writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
second_js = get_javascript_name()
assert (
first_js != second_js
), "add_js should add to the previous script in the catalog."

@stefan6419846
Copy link
Collaborator

Let me know if you have a good way to modify this, or the functionality requirements needed.

I am not completely sure if I can follow you here, but I would just let the user pass the NameObject and document the basics.

Will also do a new PR for these tests, as could be in same function?

Sorry, but I am rather confused what you want to say/ask with this.

@j-t-1
Copy link
Contributor Author

j-t-1 commented Jul 16, 2025

That a good way forward. So the function will take a NameObject called say action_type, which will take either NameObject("/O") or NameObject("/C"). This will replace open_action.

For the catalog OpenAction tests, test_added_js and test_add_js could be merged as they are doing similar things.

Copy link

codecov bot commented Jul 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.94%. Comparing base (0b64266) to head (884de23).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3382   +/-   ##
=======================================
  Coverage   96.94%   96.94%           
=======================================
  Files          55       55           
  Lines        9333     9341    +8     
  Branches     1708     1709    +1     
=======================================
+ Hits         9048     9056    +8     
  Misses        170      170           
  Partials      115      115           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants