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

Add high-level API for adding document JavaScripts #643

Merged
merged 8 commits into from
Oct 31, 2020

Conversation

brodo
Copy link
Contributor

@brodo brodo commented Oct 22, 2020

This PR adds an API for adding JavaScript to a document. It is based on this issue).

Copy link
Owner

@Hopding Hopding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @brodo!

Thank you very much for working on this. I'm sure many users will find this to be a useful addition to the library. I've just requested a few changes.

Please also update some of the integration and unit tests to exercise this new functionality (I'd recommend taking a look at the CONTRIBUTING.md if you haven't had a chance already).

src/api/PDFDocument.ts Outdated Show resolved Hide resolved
* @param script The JavaScript to execute.
* @param name The name of the script. Must be unique per document.
*/
addJavascript(script: string, name: string) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we alter the implementation of this method a bit? There are some existing patterns in the codebase that should be followed. Please see https://github.com/Hopding/pdf-lib/blob/master/src/api/PDFEmbeddedFile.ts#L48. File embedding is similar to JavaScript embedding in some respects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken the code you suggested and adopted it. It's way better this way...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll try to review it again tomorrow evening.

src/api/PDFDocument.ts Outdated Show resolved Hide resolved
@brodo
Copy link
Contributor Author

brodo commented Oct 23, 2020

I also added unit tests and integration tests for node. As pretty much no PDF viewer besides Acrobat supports this, I think the other integration tests do not make much sense.

@Hopding
Copy link
Owner

Hopding commented Oct 25, 2020

@brodo Thanks for adding the test! It's true that Acrobat is pretty much the only reader that supports JavaScript in PDFs. However, the tests for Deno, web, and React Native will still need to be updated to exercise this feature. This is because (a) The tests are meant to be exact duplicates between environments (to the greatest extent possible), this makes them much easier to update and maintain. And (b) we need to ensure that the implementation of addJavascript works in all environments, regardless of which PDF readers are used to open the resulting PDF. Unfortunately, it's easy to mistakenly implement a feature that only works in Node, or only the browser, etc...

@brodo
Copy link
Contributor Author

brodo commented Oct 26, 2020

Should be done now!

@Hopding Hopding changed the base branch from master to AddJavaScript October 31, 2020 16:59
Copy link
Owner

@Hopding Hopding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @brodo! I'll merge this into master shortly and it will go out in the next release of pdf-lib.

@Hopding Hopding merged commit b53708a into Hopding:AddJavaScript Oct 31, 2020
Hopding added a commit that referenced this pull request Oct 31, 2020
* Add high-level API for adding document JavaScripts (#643)

* Add high-level API for adding document JavaScripts

* Add example to `addJavascript` documentation and switch parameter order.

* Add check for existing catalog entries in `addJavascript`

* Add unit tests for `addJavascript`

* Add Node.js integration test for `addJavascript`

* Add integration test for `addJavascript` in web

* Add integration test for `addJavascript` in react native

* Add integration test for `addJavascript` in deno

* Cleanup PDFDocument.addJavaScript

* Revert scratchpad

Co-authored-by: Julian Dax <[email protected]>
@Hopding
Copy link
Owner

Hopding commented Nov 15, 2020

This feature was just released in version 1.12.0: https://pdf-lib.js.org/docs/api/classes/pdfdocument#addjavascript

Hopding added a commit that referenced this pull request Aug 30, 2021
* Add high-level API for adding document JavaScripts (#643)

* Add high-level API for adding document JavaScripts

* Add example to `addJavascript` documentation and switch parameter order.

* Add check for existing catalog entries in `addJavascript`

* Add unit tests for `addJavascript`

* Add Node.js integration test for `addJavascript`

* Add integration test for `addJavascript` in web

* Add integration test for `addJavascript` in react native

* Add integration test for `addJavascript` in deno

* Cleanup PDFDocument.addJavaScript

* Revert scratchpad

Co-authored-by: Julian Dax <[email protected]>
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