### Install dependencies via npm Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Install the project dependencies after updating package.json. ```console npm install ``` -------------------------------- ### Configure installation options Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Use custom flags during installation to specify library paths or filenames. ```console npm install ghostscript4js --GS4JS_HOME="C:/gs/bin" ``` ```console npm install ghostscript4js --GS4JS_LIB="libgs.so" ``` ```console npm install ghostscript4js --GS4JS_DLL="gsdll64.dll" ``` -------------------------------- ### version() - Get Ghostscript Version Information Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Retrieves detailed information about the installed Ghostscript version, including product name, copyright, revision number, and date. Useful for compatibility checks and debugging. ```APIDOC ## version() ### Description Returns an object containing detailed information about the installed Ghostscript version, including product name, copyright notice, revision number, and revision date. This method is useful for conditional logic based on Ghostscript capabilities or for logging and debugging purposes. ### Method `version()` ### Parameters None ### Response #### Success Response (Object) - **product** (string) - The name of the Ghostscript product. - **copyright** (string) - The copyright notice for the Ghostscript version. - **revision** (number) - The revision number of the Ghostscript installation. - **revisiondate** (number) - The revision date of the Ghostscript installation (YYYYMMDD format). ### Response Example ```json { "product": "GPL Ghostscript", "copyright": "Copyright (C) 2016 Artifex Software, Inc. All rights reserved.", "revision": 950, "revisiondate": 20191001 } ``` ``` -------------------------------- ### Install Ghostscript on Arch Linux Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Install the required Ghostscript package on Arch Linux. ```bash pacman -S ghostscript ``` -------------------------------- ### Install Ghostscript on Alpine Linux Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Install the required Ghostscript package on Alpine Linux. ```bash apk add ghostscript ``` -------------------------------- ### Example Usage of Conversion Functions Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Demonstrates the asynchronous execution of various document conversion functions, including error handling. ```javascript (async () => { try { await pdfToJpeg('document.pdf', 'document.jpg'); await compressPdf('large.pdf', 'compressed.pdf'); await extractPages('book.pdf', 'chapter1.pdf', 1, 10); console.log('All conversions completed'); } catch (err) { console.error('Conversion failed:', err.message); } })(); ``` -------------------------------- ### Install ghostscript4js via CLI Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Install the package directly using the npm command line. ```console npm install ghostscript4js --save ``` -------------------------------- ### Install Ghostscript on macOS Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Use Homebrew to install Ghostscript on macOS systems. ```bash brew install ghostscript ``` -------------------------------- ### Install Ghostscript on Debian Systems Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Install the required Ghostscript packages on Debian-based Linux distributions. ```bash apt-get install ghostscript libgs-dev ``` -------------------------------- ### version() Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Retrieves information about the installed Ghostscript library version. ```APIDOC ## version() ### Description Returns an object containing information about the Ghostscript library version installed on the system. ### Response - **product** (string) - Name of the product - **copyright** (string) - Copyright information - **revision** (number) - Revision number - **revisiondate** (number) - Revision date ### Response Example { "product": "GPL Ghostscript", "copyright": "Copyright (C) 2016 Artifex Software, Inc. All rights reserved.", "revision": 919, "revisiondate": 20160323 } ``` -------------------------------- ### Install Ghostscript on Red Hat or Fedora Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Install the required Ghostscript packages on Red Hat or Fedora Linux distributions. ```bash yum install ghostscript ghostscript-devel ``` -------------------------------- ### Get Ghostscript Version Information Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Retrieves detailed Ghostscript version information. Useful for conditional logic based on Ghostscript capabilities or for logging. ```javascript const gs = require('ghostscript4js'); try { const version = gs.version(); console.log(version); // Output: // { // product: 'GPL Ghostscript', // copyright: 'Copyright (C) 2016 Artifex Software, Inc. All rights reserved.', // revision: 950, // revisiondate: 20191001 // } // Take decisions based on Ghostscript version if (version.revision >= gs.MIN_SUPPORTED_REVISION) { console.log('Ghostscript version is supported'); } if (version.revision > 920) { console.log('Using newer Ghostscript features'); } } catch (err) { console.error('Failed to get Ghostscript version:', err.message); } ``` -------------------------------- ### Execute Ghostscript Command (Callback) Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Executes a Ghostscript command using a callback function for handling results. Ensure Ghostscript is installed and accessible in your system's PATH. ```javascript 'use strict' const gs = require('ghostscript4js') let cmd = '-sDEVICE=pngalpha -o my.png -sDEVICE=pngalpha -r144 my.pdf' gs.execute(cmd, function (err) { if (err) { console.log("Ooops... something wrong happened") } }) ``` -------------------------------- ### Check Ghostscript Version Compatibility Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Use MIN_SUPPORTED_REVISION and MAX_SUPPORTED_REVISION to validate compatibility with the installed Ghostscript version. Handles cases where the version is too old or newer than tested. ```javascript const gs = require('ghostscript4js'); console.log('Minimum supported revision:', gs.MIN_SUPPORTED_REVISION); // 919 console.log('Maximum supported revision:', gs.MAX_SUPPORTED_REVISION); // 950 // Check if installed Ghostscript is compatible try { const version = gs.version(); if (version.revision < gs.MIN_SUPPORTED_REVISION) { console.error(`Ghostscript ${version.revision} is too old. Minimum required: ${gs.MIN_SUPPORTED_REVISION}`); process.exit(1); } if (version.revision > gs.MAX_SUPPORTED_REVISION) { console.warn(`Ghostscript ${version.revision} is newer than tested version ${gs.MAX_SUPPORTED_REVISION}`); } console.log('Ghostscript version is compatible'); } catch (err) { console.error('Could not verify Ghostscript version:', err.message); } ``` -------------------------------- ### Execute Ghostscript Command (Promise) Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Executes a Ghostscript command using Promises for handling results. This approach is suitable for asynchronous operations. Ensure Ghostscript is installed and accessible. ```javascript 'use strict' const gs = require('ghostscript4js') let cmd = '-sDEVICE=pngalpha -o my.png -sDEVICE=pngalpha -r144 my.pdf' gs.execute(cmd) .then(() => { console.log("All is ok") }) .catch((err) => { console.log("Ooops... something wrong happened") }) ``` -------------------------------- ### Get Supported Ghostscript Revisions Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Retrieves the minimum and maximum supported Ghostscript revision numbers by the ghostscript4js module. These values indicate compatibility with specific Ghostscript versions. ```javascript 'use strict' const gs = require('ghostscript4js') console.log(gs.MIN_SUPPORTED_REVISION) console.log(gs.MAX_SUPPORTED_REVISION) ``` -------------------------------- ### Basic usage of ghostscript4js Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Initialize the module and execute a synchronous command. ```js 'use strict' const gs = require('ghostscript4js') try { // Take decision based on Ghostscript version const version = gs.version() console.log(version) gs.executeSync('-sDEVICE=pngalpha -o my.png -sDEVICE=pngalpha -r144 my.pdf') } catch (err) { // Handle error throw err } ``` -------------------------------- ### Execute synchronous commands Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Run Ghostscript commands synchronously using executeSync. ```js 'use strict' const gs = require('ghostscript4js') try { gs.executeSync('-sDEVICE=pngalpha -o my.png -sDEVICE=pngalpha -r144 my.pdf') } catch (err) { // Handle error throw err } ``` -------------------------------- ### Check Ghostscript version Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Use the version information to conditionally execute logic. ```js 'use strict' const gs = require('ghostscript4js') try { const version = gs.version() console.log(version) // Take decision based on Ghostscript version if (version.revision > 916) { // ... some stuff } else { // ... other stuff } } catch (err) { // Handle error throw err } ``` -------------------------------- ### Add ghostscript4js to package.json Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md Add the dependency to your project's package.json file. ```json "ghostscript4js": "version" ``` ```json "ghostscript4js": "*" for the latest version "ghostscript4js": "1.0.0" for the version 1.0.0 ``` -------------------------------- ### execute(cmd, callback) - Asynchronous Command Execution Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Executes Ghostscript commands asynchronously without blocking the Node.js event loop. Supports both callback and Promise-based patterns, making it suitable for web applications and services. ```APIDOC ## execute(cmd, callback) ### Description Executes Ghostscript commands asynchronously without blocking the Node.js event loop. Supports both callback and Promise-based patterns. This is the recommended method for web applications and services where responsiveness is critical. ### Method `execute(cmd, callback?)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **cmd** (string | string[]) - The Ghostscript command(s) to execute. Can be a single string or an array of strings. - **callback** (function, optional) - A callback function to be executed upon completion or error when not using Promises. ### Request Example ```javascript // Using Promises (recommended) const cmd = `-psconv -q -dNOPAUSE -sDEVICE=pngalpha -o output.png -r144 input.pdf`; await gs.execute(cmd); // Using array-based API with Promises await gs.execute([ '-psconv', '-q', '-dNOPAUSE', '-sDEVICE=pngalpha', '-o', 'my converted image.png', '-r144', 'my source document.pdf' ]); // Using callback pattern gs.execute('-sDEVICE=pngalpha -o output.png -r144 input.pdf', function(err) { if (err) { console.error('Conversion failed:', err.message); return; } console.log('PDF successfully converted to PNG'); }); ``` ### Response #### Success Response (200) When using Promises, the Promise resolves upon successful execution. When using callbacks, the `callback` function is invoked with `err` as `null`. #### Error Response Throws an error if Ghostscript execution fails. The error message typically includes the Ghostscript error code. When using Promises, the Promise rejects with the error. When using callbacks, the `callback` function is invoked with the error object. ### Response Example ```javascript // Promise-based success gs.execute(cmd).then(() => { console.log('Operation successful'); }).catch((err) => { console.error('Operation failed:', err.message); }); // Callback-based success gs.execute(cmd, (err, result) => { if (err) { console.error('Operation failed:', err.message); } else { console.log('Operation successful'); } }); ``` ``` -------------------------------- ### executeSync(cmd) - Synchronous Command Execution Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Executes Ghostscript commands synchronously. The command can be provided as a string or an array of strings. This method is suitable for CLI tools or scripts where blocking the event loop is acceptable. ```APIDOC ## executeSync(cmd) ### Description Executes Ghostscript commands synchronously, blocking the event loop until completion. The command can be passed as a string or an array of strings. This method is ideal for CLI tools or scripts where blocking behavior is acceptable. ### Method `executeSync(cmd)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **cmd** (string | string[]) - The Ghostscript command(s) to execute. Can be a single string or an array of strings for better handling of paths with spaces. ### Request Example ```json // Example using a string command "-sDEVICE=pngalpha -o output.png -r144 input.pdf" // Example using an array of strings [ "-psconv", "-q", "-dNOPAUSE", "-sDEVICE=pngalpha", "-o", "my output file.png", "-r144", "my input file.pdf" ] ``` ### Response #### Success Response (200) No specific response body is returned on success; the method completes execution. #### Error Response Throws an error if Ghostscript execution fails. The error message typically includes the Ghostscript error code. ### Error Handling Example ```javascript try { gs.executeSync('-sDEVICE=pngalpha -o output.png -r144 input.pdf'); console.log('PDF converted to PNG successfully'); } catch (err) { console.error('Conversion failed:', err.message); // Example error: "Sorry error happened executing Ghostscript command. Error code: -100" } ``` ``` -------------------------------- ### Convert PDF to Grayscale PDF Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Converts a color PDF file to a grayscale PDF file. ```javascript async function pdfToGrayscale(input, output) { await gs.execute(`-sDEVICE=pdfwrite -sColorConversionStrategy=Gray -o ${output} ${input}`); } ``` -------------------------------- ### Asynchronously Execute Ghostscript Commands Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Executes Ghostscript commands asynchronously without blocking the event loop. Recommended for web applications and services. Supports Promises and callbacks. ```javascript const gs = require('ghostscript4js'); // Using Promises (recommended for modern Node.js) async function convertPdfToPng(inputPath, outputPath, resolution = 144) { const cmd = `-psconv -q -dNOPAUSE -sDEVICE=pngalpha -o ${outputPath} -r${resolution} ${inputPath}`; try { await gs.execute(cmd); console.log(`Successfully converted ${inputPath} to ${outputPath}`); return true; } catch (err) { console.error('Conversion error:', err.message); return false; } } // Usage convertPdfToPng('document.pdf', 'document.png', 300); ``` ```javascript const gs = require('ghostscript4js'); // Using array-based API with Promises (handles spaces in filenames) async function convertWithSpaces() { try { await gs.execute([ '-psconv', '-q', '-dNOPAUSE', '-sDEVICE=pngalpha', '-o', 'my converted image.png', '-sDEVICE=pngalpha', '-r144', 'my source document.pdf' ]); console.log('Conversion complete'); } catch (err) { console.error('Failed:', err.message); } } ``` ```javascript const gs = require('ghostscript4js'); // Using callback pattern gs.execute('-sDEVICE=pngalpha -o output.png -r144 input.pdf', function(err) { if (err) { console.error('Conversion failed:', err.message); return; } console.log('PDF successfully converted to PNG'); }); ``` ```javascript const gs = require('ghostscript4js'); // Convert PostScript to PDF asynchronously gs.execute('-sDEVICE=pdfwrite -o output.pdf -f input.ps') .then(() => { console.log('PostScript to PDF conversion complete'); }) .catch((err) => { console.error('Conversion error:', err.message); }); ``` -------------------------------- ### Ghostscript version object structure Source: https://github.com/nicknaso/ghostscript4js/blob/master/README.md The structure of the object returned by the version() method. ```js { product: "GPL Ghostscript", copyright: "Copyright (C) 2016 Artifex Software, Inc. All rights reserved.", revision: 919, revisiondate: 20160323 } ``` -------------------------------- ### Synchronously Execute Ghostscript Commands Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Executes Ghostscript commands synchronously, blocking the event loop. Ideal for CLI tools or scripts where blocking is acceptable. Commands can be strings or arrays. ```javascript const gs = require('ghostscript4js'); // Convert PDF to PNG using string command try { gs.executeSync('-sDEVICE=pngalpha -o output.png -r144 input.pdf'); console.log('PDF converted to PNG successfully'); } catch (err) { console.error('Conversion failed:', err.message); // Error includes Ghostscript error code for debugging // Example: "Sorry error happened executing Ghostscript command. Error code: -100" } ``` ```javascript const gs = require('ghostscript4js'); // Convert PDF to PNG using array command (better for paths with spaces) try { gs.executeSync([ '-psconv', '-q', '-dNOPAUSE', '-sDEVICE=pngalpha', '-o', 'my output file.png', '-r144', 'my input file.pdf' ]); console.log('Conversion completed'); } catch (err) { console.error('Error:', err.message); } ``` ```javascript const gs = require('ghostscript4js'); // Convert PostScript to PDF try { gs.executeSync('-sDEVICE=pdfwrite -o output.pdf -f input.ps'); console.log('PostScript converted to PDF'); } catch (err) { console.error('PS to PDF conversion failed:', err.message); } ``` -------------------------------- ### Convert PDF to TIFF (Multi-page) Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Converts a PDF file to a multi-page TIFF image. Each page will be saved as a separate TIFF file with a sequential page number. ```javascript async function pdfToTiff(input, output) { // %d in output creates separate files for each page await gs.execute(`-sDEVICE=tiff24nc -o ${output}-%03d.tiff -r300 ${input}`); } ``` -------------------------------- ### Compress PDF Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Compresses a PDF file using specific PDF settings for smaller file size. ```javascript async function compressPdf(input, output) { await gs.execute(`-sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -o ${output} ${input}`); } ``` -------------------------------- ### Extract Specific Page Range from PDF Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Extracts a specified range of pages from a PDF file to a new PDF file. ```javascript async function extractPages(input, output, firstPage, lastPage) { await gs.execute(`-sDEVICE=pdfwrite -dFirstPage=${firstPage} -dLastPage=${lastPage} -o ${output} ${input}`); } ``` -------------------------------- ### Convert PDF to JPEG Source: https://context7.com/nicknaso/ghostscript4js/llms.txt Converts a PDF file to a JPEG image with specified quality and resolution. ```javascript async function pdfToJpeg(input, output) { await gs.execute(`-sDEVICE=jpeg -dJPEGQ=95 -o ${output} -r150 ${input}`); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.