-
-
Notifications
You must be signed in to change notification settings - Fork 673
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PDFDocument.addJavaScript method (#654)
* 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]>
- Loading branch information
Showing
9 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import Embeddable from 'src/api/Embeddable'; | ||
import PDFDocument from 'src/api/PDFDocument'; | ||
import JavaScriptEmbedder from 'src/core/embedders/JavaScriptEmbedder'; | ||
import { PDFName, PDFArray, PDFDict, PDFHexString, PDFRef } from 'src/core'; | ||
|
||
/** | ||
* Represents JavaScript that has been embedded in a [[PDFDocument]]. | ||
*/ | ||
export default class PDFJavaScript implements Embeddable { | ||
/** | ||
* > **NOTE:** You probably don't want to call this method directly. Instead, | ||
* > consider using the [[PDFDocument.addJavaScript]] method, which will | ||
* create instances of [[PDFJavaScript]] for you. | ||
* | ||
* Create an instance of [[PDFJavaScript]] from an existing ref and script | ||
* | ||
* @param ref The unique reference for this script. | ||
* @param doc The document to which the script will belong. | ||
* @param embedder The embedder that will be used to embed the script. | ||
*/ | ||
static of = (ref: PDFRef, doc: PDFDocument, embedder: JavaScriptEmbedder) => | ||
new PDFJavaScript(ref, doc, embedder); | ||
|
||
/** The unique reference assigned to this embedded script within the document. */ | ||
readonly ref: PDFRef; | ||
|
||
/** The document to which this embedded script belongs. */ | ||
readonly doc: PDFDocument; | ||
|
||
private alreadyEmbedded = false; | ||
private readonly embedder: JavaScriptEmbedder; | ||
|
||
private constructor( | ||
ref: PDFRef, | ||
doc: PDFDocument, | ||
embedder: JavaScriptEmbedder, | ||
) { | ||
this.ref = ref; | ||
this.doc = doc; | ||
this.embedder = embedder; | ||
} | ||
|
||
/** | ||
* > **NOTE:** You probably don't need to call this method directly. The | ||
* > [[PDFDocument.save]] and [[PDFDocument.saveAsBase64]] methods will | ||
* > automatically ensure all JavaScripts get embedded. | ||
* | ||
* Embed this JavaScript in its document. | ||
* | ||
* @returns Resolves when the embedding is complete. | ||
*/ | ||
async embed(): Promise<void> { | ||
if (!this.alreadyEmbedded) { | ||
const { catalog, context } = this.doc; | ||
|
||
const ref = await this.embedder.embedIntoContext( | ||
this.doc.context, | ||
this.ref, | ||
); | ||
|
||
if (!catalog.has(PDFName.of('Names'))) { | ||
catalog.set(PDFName.of('Names'), context.obj({})); | ||
} | ||
const Names = catalog.lookup(PDFName.of('Names'), PDFDict); | ||
|
||
if (!Names.has(PDFName.of('JavaScript'))) { | ||
Names.set(PDFName.of('JavaScript'), context.obj({})); | ||
} | ||
const Javascript = Names.lookup(PDFName.of('JavaScript'), PDFDict); | ||
|
||
if (!Javascript.has(PDFName.of('Names'))) { | ||
Javascript.set(PDFName.of('Names'), context.obj([])); | ||
} | ||
const JSNames = Javascript.lookup(PDFName.of('Names'), PDFArray); | ||
|
||
JSNames.push(PDFHexString.fromText(this.embedder.scriptName)); | ||
JSNames.push(ref); | ||
|
||
this.alreadyEmbedded = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import PDFHexString from 'src/core/objects/PDFHexString'; | ||
import PDFContext from 'src/core/PDFContext'; | ||
import PDFRef from 'src/core/objects/PDFRef'; | ||
|
||
class JavaScriptEmbedder { | ||
static for(script: string, scriptName: string) { | ||
return new JavaScriptEmbedder(script, scriptName); | ||
} | ||
|
||
private readonly script: string; | ||
readonly scriptName: string; | ||
|
||
private constructor(script: string, scriptName: string) { | ||
this.script = script; | ||
this.scriptName = scriptName; | ||
} | ||
|
||
async embedIntoContext(context: PDFContext, ref?: PDFRef): Promise<PDFRef> { | ||
const jsActionDict = context.obj({ | ||
Type: 'Action', | ||
S: 'JavaScript', | ||
JS: PDFHexString.fromText(this.script), | ||
}); | ||
|
||
if (ref) { | ||
context.assign(ref, jsActionDict); | ||
return ref; | ||
} else { | ||
return context.register(jsActionDict); | ||
} | ||
} | ||
} | ||
|
||
export default JavaScriptEmbedder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters