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

DOC: Add JavaScript example #2746

Merged
merged 5 commits into from
Jul 10, 2024
Merged
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
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ You can contribute to `pypdf on GitHub <https:/py-pdf/pypdf>`_.
user/encryption-decryption
user/merging-pdfs
user/cropping-and-transforming
user/add-watermark
user/reading-pdf-annotations
user/adding-pdf-annotations
user/add-watermark
user/add-javascript
user/viewer-preferences
user/forms
user/streaming-data
Expand Down
21 changes: 21 additions & 0 deletions docs/user/add-javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Adding JavaScript to a PDF

PDF readers vary in the extent they support JavaScript, with some not supporting it at all.

Adobe has documentation on its support here:
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html

## Launch print window on opening

```python
from pypdf import PdfWriter

writer = PdfWriter(clone_from="example.pdf")

# Add JavaScript to launch the print window on opening this PDF.
writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

# Write to pypdf-output.pdf.
with open("pypdf-output.pdf", "wb") as fp:
writer.write(fp)
```
6 changes: 0 additions & 6 deletions docs/user/cropping-and-transforming.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ page3.mediabox.upper_right = (
)
writer.add_page(page3)

# Add some JavaScript to launch the print window on opening this PDF.
# The password dialog may prevent the print dialog from being shown,
# comment the encryption lines, if that's the case, to try this out.
# https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/index.html
writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

# Write to pypdf-output.pdf.
with open("pypdf-output.pdf", "wb") as fp:
writer.write(fp)
Expand Down