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

ENH: Add PdfWriter.open_destination property #1431

Merged
merged 11 commits into from Nov 25, 2022
Merged

Conversation

pubpub-zz
Copy link
Collaborator

@pubpub-zz pubpub-zz commented Nov 13, 2022

(fix JS executed twice extracted in another PR)
add capability to define an opening destination for the PDF

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
@codecov
Copy link

codecov bot commented Nov 13, 2022

Codecov Report

Base: 94.25% // Head: 94.31% // Increases project coverage by +0.06% 🎉

Coverage data is based on head (ccf9346) compared to base (6f73cb1).
Patch coverage: 96.42% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1431      +/-   ##
==========================================
+ Coverage   94.25%   94.31%   +0.06%     
==========================================
  Files          28       28              
  Lines        5096     5171      +75     
  Branches      973      980       +7     
==========================================
+ Hits         4803     4877      +74     
  Misses        177      177              
- Partials      116      117       +1     
Impacted Files Coverage Δ
PyPDF2/_writer.py 91.53% <96.42%> (+0.22%) ⬆️
PyPDF2/generic/_base.py 100.00% <0.00%> (ø)
PyPDF2/generic/_utils.py 100.00% <0.00%> (ø)
PyPDF2/filters.py 97.29% <0.00%> (+0.01%) ⬆️
PyPDF2/_reader.py 89.61% <0.00%> (+0.03%) ⬆️
PyPDF2/_encryption.py 92.66% <0.00%> (+0.04%) ⬆️
PyPDF2/generic/_data_structures.py 95.11% <0.00%> (+0.04%) ⬆️
PyPDF2/_page.py 92.22% <0.00%> (+0.05%) ⬆️
PyPDF2/xmp.py 92.10% <0.00%> (+0.97%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@pubpub-zz pubpub-zz changed the title Prevent Multiple JS execution at open and add Opening Destination FIX : Prevent Multiple JS execution at open and add Opening Destination Nov 13, 2022
@MartinThoma
Copy link
Member

@pubpub-zz Is the new opening property related in any way to the issues you mentioned?

@MartinThoma
Copy link
Member

In general I prefer PRs which deal with a single issue. This makes it way easier for me to merge a PR as I only have to check one thing at a time.

@pubpub-zz
Copy link
Collaborator Author

pubpub-zz commented Nov 19, 2022

In general I prefer PRs which deal with a single issue. This makes it way easier for me to merge a PR as I only have to check one thing at a time.

I've introduced the opening at the same time as the change was removing access to AutoOpen field
I can split if you prefer

@MartinThoma
Copy link
Member

I think splitting would be nice 🙏

I'm uncertain about the naming opening, but the JS-fixes are very clearly something I want to merge

@pubpub-zz
Copy link
Collaborator Author

I think splitting would be nice 🙏

I'm uncertain about the naming opening, but the JS-fixes are very clearly something I want to merge

any ideas another name for opening ?

@MartinThoma
Copy link
Member

MartinThoma commented Nov 19, 2022

If you want to make OpenAction accessible, I would just do that and call it open_action. But there is more going on in the code.

According to TABLE 3.25 Entries in the catalog dictionary the OpenAction entry is either an array or a dictionary:

(Optional; PDF 1.1) A value specifying a destination to be displayed or
an action to be performed when the document is opened. The value is
either an array defining a destination (see Section 8.2.1, “Destinations”)
or an action dictionary representing an action (Section 8.5, “Actions”). If
this entry is absent, the document should be opened to the top of the
first page at the default magnification factor.

Why is the isinstance(oa, (str, bytes)) helpful?

I'm sorry if I ask dumb questions; I know way less about PDF than you 😅

@pubpub-zz pubpub-zz changed the title FIX : Prevent Multiple JS execution at open and add Opening Destination ENH : add PDF Opening Destination property Nov 20, 2022
@pubpub-zz
Copy link
Collaborator Author

If you want to make OpenAction accessible, I would just do that and call it open_action. But there is more going on in the code.

I dislike the idea of calling it open_action as actually the JS actions are now executed from other Names/JavaScript.
OpenAction will now only deal with destinations to define the opening of the pdf

According to TABLE 3.25 Entries in the catalog dictionary the OpenAction entry is either an array or a dictionary:

(Optional; PDF 1.1) A value specifying a destination to be displayed or
an action to be performed when the document is opened. The value is
either an array defining a destination (see Section 8.2.1, “Destinations”)
or an action dictionary representing an action (Section 8.5, “Actions”). If
this entry is absent, the document should be opened to the top of the
first page at the default magnification factor.

Why is the isinstance(oa, (str, bytes)) helpful?

You can define named destinations. Even if not in the PDF standard it works (I was also doubtfull about it😉). For the getter I've prefered to keep the name instead of the actual destination for users to not believe that something has been modified

I've extracted the fix on in another PR

@pubpub-zz
Copy link
Collaborator Author

Waiting for #1439 to be merged before updating this one

@pubpub-zz
Copy link
Collaborator Author

@MartinThoma
looks ready

@MartinThoma
Copy link
Member

@MasterOdin What do you think about newly added opening property?

PyPDF2/_writer.py Outdated Show resolved Hide resolved
tests/test_javascript.py Outdated Show resolved Hide resolved
PyPDF2/_writer.py Outdated Show resolved Hide resolved
PyPDF2/_writer.py Outdated Show resolved Hide resolved
@MasterOdin
Copy link
Member

Personally, I feel like calling it open_action would be better as opening doesn't feel right as a noun saying "PDF Writer opening". I could see calling it maybe opening_action as in "PDF Writer opening action" vs open_action.

I do think it's fine to add this property, even if it's not really one that's often used given it is in the PDF spec (see Table 28 in 7.7.2).

@pubpub-zz
Copy link
Collaborator Author

Personally, I feel like calling it open_action would be better as opening doesn't feel right as a noun saying "PDF Writer opening". I could see calling it maybe opening_action as in "PDF Writer opening action" vs open_action.

I do think it's fine to add this property, even if it's not really one that's often used given it is in the PDF spec (see Table 28 in 7.7.2).

The point is that this property is not dealing with any action/script. What about open_destination ?

pubpub-zz and others added 2 commits November 22, 2022 07:23
Co-authored-by: Matthew Peveler <matt.peveler@gmail.com>
Co-authored-by: Matthew Peveler <matt.peveler@gmail.com>
@MasterOdin
Copy link
Member

MasterOdin commented Nov 23, 2022

open_destination makes sense for what it does. Really just want the open or opening applied as an adjective to a word that describes what it does.

@pubpub-zz
Copy link
Collaborator Author

@MartinThoma
it should be good now for you

@MartinThoma
Copy link
Member

Thank you @pubpub-zz for putting that much work again in this PR ❤️

And thank you @MasterOdin for the thorough review 🤗

@MartinThoma MartinThoma merged commit fdd1058 into py-pdf:main Nov 25, 2022
@MartinThoma MartinThoma changed the title ENH : add PDF Opening Destination property ENH: Add PdfWriter.open_destination property Nov 25, 2022
@pubpub-zz pubpub-zz deleted the iss1425 branch November 25, 2022 17:25
MartinThoma added a commit that referenced this pull request Dec 10, 2022
New Features (ENH):
-  Add support to extract gray scale images (#1460)
-  Add 'threads' property to PdfWriter (#1458)
-  Add 'open_destination' property to PdfWriter (#1431)
-  Make PdfReader.get_object accept integer arguments (#1459)

Bug Fixes (BUG):
-  Scale PDF annotations (#1479)

Robustness (ROB):
-  Padding issue with AES encryption (#1469)
-  Accept empty object as null objects (#1477)

Documentation (DOC):
-  Add module documentation the PaperSize class (#1447)

Maintenance (MAINT):
-  Use 'page_number' instead of 'pagenum' (#1365)
-  Add List of pages to PageRangeSpec (#1456)

Testing (TST):
-  Cleanup temporary files (#1454)
-  Mark test_tounicode_is_identity as external (#1449)
-  Use Ubuntu 20.04 for running CI test suite (#1452)

[Full Changelog](2.11.2...2.12.0)
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.

None yet

3 participants