New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@privyid/ghoulscript

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@privyid/ghoulscript

  • 0.1.0-alpha.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
223
increased by2130%
Maintainers
0
Weekly downloads
 
Created
Source

@privyid/ghoulscript

Compress, merge, split, and render pages to image using Ghostscript

Installation

yarn add @privyid/ghoulscript

How to Use

Using on Browser (Vite)

import { optimizePDF } from '@privyid/ghoulscript'

const input = document.querySelector('input[type="file"]')

input.addEventListener('change', async () => {
  if (input.files) {
    const file      = input.files[0]
    const output    = await optimizePDF(file)
    const outputURL = URL.createObjectURL(new Blob([output], { type: 'application/pdf' }))

    window.open(outputURL, '_blank')
  }
})

Using on NodeJS

import fs from 'node:fs/promises'
import { resolve } from 'node:path'
import { optimizePDF } from '@privyid/ghoulscript'

const buffer = await fs.readFile(resolve(__dirname, './sample.pdf'))
const output = await optimizePDF(buffer)

await fs.writeFile(resolve(__dirname, './sample.compressed.pdf'), output)

Utilities

optimizePDF (file: Buffer, options?: CompressOptions)

Compress and optimize PDF for Web Viewer.

import { optimizePDF } from '@privyid/ghoulscript'

const buffer = await fs.readFile(resolve(__dirname, './sample.pdf'))
const output = await optimizePDF(buffer, { password: '******' })

await fs.writeFile(resolve(__dirname, './sample.compressed.pdf'), output)
CompressOptions
OptionsTypeDefaultDescription
passwordString-Document protection password
pdfSettingsStringscreenPreset setting, valid value is screen, ebook, printer, prepress, default
fastWebViewBooleantrueEnable Fast Web View (Linearization)
compatibilityLevelString1.4Compability version
colorConversionStrategyStringRGBColor conversion strategy, valid value is RGB, CMYK
noTransparencyBooleantrueRemove transparency
keepPasswordBooleantrueKeep document password if document have a password protection
userPasswordString-Set User Password to document
ownerPasswordString-Set Owner Password to document
colorImageResolutionNumber300Color image resolution
grayImageResolutionNumber300Gray image resolution
monoImageResolutionNumber300Mono image resolution
argsString[]-Additional arguments

combinePDF (files: Buffer[], options?: CompressOptions)

Combine multiple PDF files into single PDF

import { combinePDF } from '@privyid/ghoulscript'

const bufferA = await fs.readFile(resolve(__dirname, './sample-1.pdf'))
const bufferB = await fs.readFile(resolve(__dirname, './sample-2.pdf'))
const output  = await combinePDF([bufferA, bufferB])

await fs.writeFile(resolve(__dirname, './sample.combine.pdf'), output)

splitPdf (file: Buffer, pageList: PageList[], options?: CompressOptions)

Split single PDF into multiple files

import { splitPdf } from '@privyid/ghoulscript'

const buffer  = await fs.readFile(resolve(__dirname, './sample.pdf'))
const outputs = await splitPdf(buffer, ['1-5', '5-12'])

await fs.writeFile(resolve(__dirname, './sample.part1.pdf'), outputs[0])
await fs.writeFile(resolve(__dirname, './sample.part2.pdf'), outputs[1])

addPassword (file: Buffer, userPassword: string, ownerPassword?: string)

Set new User Password and Owner Password

import { addPassword } from '@privyid/ghoulscript'

const buffer = await fs.readFile(resolve(__dirname, './sample.pdf'))
const output = await addPassword(buffer, '123456', '112233')

await fs.writeFile(resolve(__dirname, './sample.protected.pdf'), output)

It's equal to compressPDF's userPassword and ownerPassword options

removePassword (file: Buffer, oldPassword: string)

Remove existing encrypted PDF

import { removePassword } from '@privyid/ghoulscript'

const buffer = await fs.readFile(resolve(__dirname, './sample.protected.pdf'))
const output = await removePassword(buffer, '123456')

await fs.writeFile(resolve(__dirname, './sample.unprotected.pdf'), output)

It's equal to compressPDF's keepPassword: false

renderPageAsImage (file: Buffer, page: number = 1, options?: RenderOptions)

Convert specific page to image

import { renderPageAsImage } from '@privyid/ghoulscript'

const buffer = await fs.readFile(resolve(__dirname, './sample.pdf'))
const output = await renderPageAsImage(buffer, 5, { format: 'jpg' })

await fs.writeFile(resolve(__dirname, './sample.jpg'), output)
RenderOptions
OptionsTypeDefaultDescription
resolutionNumber96Render resolution
textAlphaBitsNumber4Text alpha bits, valid value is 1-4
graphicsAlphaBitsNumber4Graphic alpha bits, valid value is 1-4
formatStringjpgRender format, valid value is jpg or png
argsString[]-Additional arguments

getInfo (file: Buffer, options?: { password: string })

Extract pages info

import { getInfo } from '@privyid/ghoulscript'

const buffer = await fs.readFile(resolve(__dirname, './sample.pdf'))
const info   = await getInfo(buffer)

console.log(info)
/*
{
  numPages: 5,
  pages: [
    {
      page: 1,
      width: 612,
      height: 792,
    },
    {
      page: 2,
      width: 612,
      height: 792,
    },
    {
      page: 3,
      width: 612,
      height: 792,
    },
  ]
}
*/

isRequirePassword (file: Buffer)

Check document is require password or not to open.

import { isRequirePassword } from '@privyid/ghoulscript'

const bufferA = await fs.readFile(resolve(__dirname, './sample.pdf'))
const bufferB = await fs.readFile(resolve(__dirname, './sample.protected.pdf'))

console.log(await isRequirePassword(bufferA)) // false
console.log(await isRequirePassword(bufferB)) // true

License

AGPL-3.0

FAQs

Package last updated on 18 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc