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

Embed JS #477

Closed
btecu opened this issue Jun 9, 2020 · 1 comment
Closed

Embed JS #477

btecu opened this issue Jun 9, 2020 · 1 comment

Comments

@btecu
Copy link
Contributor

btecu commented Jun 9, 2020

Is there a way to embed Javascript into a PDF using pdf-lib?

@Hopding
Copy link
Owner

Hopding commented Sep 20, 2020

Hello @btecu!

You certainly can. However, pdf-lib doesn't have any special APIs for this, so you'll have to use the lower-level APIs. Here's a basic example demonstrating how to create a JavaScript action dictionary with pdf-lib:

import { PDFDocument, PDFHexString } from 'pdf-lib';

const pdfDoc = await PDFDocument.create()

const jsActionDict = pdfDoc.context.obj({
  Type: 'Action',
  S: 'JavaScript',
  JS: PDFHexString.fromText(`
    // Insert the JavaScript you want to embed here...
  `),
})

// Now use the `jsActionDict`...

See section 12.6.4.16 JavaScript Actions of the PDF spec for more details about JavaScript action dictionaries and how to use them.

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

No branches or pull requests

2 participants