Sparticuz / node-qpdf2

A Content Preserving transformations on PDFs wrapped around QPDF
MIT License
20 stars 16 forks source link

Use in common js #37

Closed xuakson closed 2 weeks ago

xuakson commented 11 months ago

How do i use this in common js , node js environment, i dont want to set the type to module in the packagae json file neither change file extension to mjs because all my fies in common js only, give me ideas how to use this to blend with the common js , thanks in advance

Sparticuz commented 11 months ago

@xuakson You can use version 4. Everything should still work fine

xuakson commented 11 months ago

Version 4 of what? please send the link, i'm confused, bcus i see 11.xx

Sparticuz commented 11 months ago

@xuakson https://github.com/Sparticuz/node-qpdf2/releases/tag/v4.1.0

You can still use the latest version of qpdf, you will need to use v4 of node-qpdf2 though if you need cjs.

xuakson commented 11 months ago

oh see it, thanks sir, worked , [AsyncFunction: encrypt]

xuakson commented 11 months ago

another question, can i get result in buffer?

Sparticuz commented 11 months ago

Yep, https://github.com/Sparticuz/node-qpdf2/blob/682e869f28425e12f6a4fe02354fcf94b722144e/test/encrypt-and-decrypt.ts#L65-L79

xuakson commented 11 months ago

ayee, this is typescript, dont understand, this is my codes ` ![image](https://github.com/Sparticuz/node-qpdf2/assets/23705128/e70a4031-b0b7-42c7-824a-4a952890971d)

luicfrr commented 11 months ago

To use this package on commonjs module you can use this. Here's an example:

import type nodeQpf from 'node-qpdf2'

...

const { encrypt } = await importEsmModule<typeof nodeQpf>( 'node-qpdf2' )

Hope this helps :)

xuakson commented 11 months ago

To use this package on commonjs module you can use this. Here's an example:

import type nodeQpf from 'node-qpdf2'

...

const { encrypt } = await importEsmModule<typeof nodeQpf>( 'node-qpdf2' )

Hope this helps :)

didnt work sir, i use version 4 worked, thank for helping anyway, i'm getting issue with await yet it at the top level,

luicfrr commented 11 months ago

You can only use await inside an async function. I'm using latest version without errors:

import type nodeQpf from 'node-qpdf2'

async function myFunction() {
  const { encrypt } = await importEsmModule<typeof nodeQpf>( 'node-qpdf2' )

  await encrypt({ ... })
}