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

Ghostscript nodejs client use sample added #616

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions ghostscript-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Use Ghostscript with Functions

This sample shows how to use Ghostscript with firebase functions. Ghostscript is a tool which is used to perform operations on the PDF files.

## Functions Code

See file [functions/index.js](functions/index.js) for the code.

The dependencies are listed in [functions/package.json](functions/package.json).
8 changes: 8 additions & 0 deletions ghostscript-nodejs/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public"
}
}
122 changes: 122 additions & 0 deletions ghostscript-nodejs/functions/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 2017
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",

// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",

// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",

// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",

// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",

// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",

// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,

// Warn if return statements do not either always or never specify values
"consistent-return": 1,

// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,

// Requre the use of === and !==
"eqeqeq": 2,

// Disallow the use of alert, confirm, and prompt
"no-alert": 2,

// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,

// Disallow null comparisons without type-checking operators
"no-eq-null": 2,

// Disallow the use of eval()
"no-eval": 2,

// Warn against extending native types
"no-extend-native": 1,

// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,

// Warn against unnecessary labels
"no-extra-label": 1,

// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,

// Warn against shorthand type conversions
"no-implicit-coercion": 1,

// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,

// Disallow new operators with the Function object
"no-new-func": 2,

// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,

// Disallow throwing literals as exceptions
"no-throw-literal": 2,

// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,

// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,

// Enforce return statements in getters
"getter-return": 2,

// Disallow await inside of loops
"no-await-in-loop": 2,

// Disallow comparing against -0
"no-compare-neg-zero": 2,

// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,

// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,

// Enforce return statements in callbacks of array methods
"callback-return": 2,

// Require error handling in callbacks
"handle-callback-err": 2,

// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,

// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,

// Return inside each then() to create readable and reusable Promise chains.
"promise/always-return": 2,

//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,

// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}
137 changes: 137 additions & 0 deletions ghostscript-nodejs/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const functions = require('firebase-functions');
const { Storage } = require('@google-cloud/storage');
const gcs = new Storage();
const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
var gs = require('gs');


//This function triggers whenever any pdf is uploaded to the firebase storage
//and attempts to generate

exports.makePreviews = functions.storage.object().onFinalize(async (object, event) => {

//Checking for pdf files
if (!object.name.endsWith('.pdf')) return false;

const filePath = object.name;

//slicing name and path
const splitFileName = object.name.split(".");
const fileID = splitFileName[0].split("/")[1];

console.log("File ID -", fileID);

//creating temporary path strings for gcp file system
const fileName = path.basename(filePath);
const tempFilePath = path.join(os.tmpdir(), fileName);

const newName1 = path.basename(filePath, '.pdf') + '01.jpeg';
const tempNewPath1 = path.join(os.tmpdir(), newName1);

const newName2 = path.basename(filePath, '.pdf') + '02.jpeg';
const tempNewPath2 = path.join(os.tmpdir(), newName2);


//downloading file from firebase storage
const bucket = gcs.bucket(object.bucket);

return bucket.file(filePath).download({
destination: tempFilePath
}).then(async () => {
console.log('PDF downloaded locally to', tempFilePath);

//generating two preview JPEGS
await new Promise((resolve, reject) => {
gs()
.batch()
.option('-dFirstPage=1')
.option('-dLastPage=1')
.nopause()
.res(90)
.executablePath('gs')
.device('jpeg')
.output(tempNewPath1)
.input(tempFilePath)
.exec((err, stdout, stderr) => {
if (!err) {
console.log('gs executed w/o error');
console.log('stdout', stdout);
console.log('stderr', stderr);
resolve();
} else {
console.log('gs error:', err);
reject(err);
}
});
});

return new Promise((resolve, reject) => {
gs()
.batch()
.option('-dFirstPage=2')
.option('-dLastPage=2')
.nopause()
.res(90)
.executablePath('gs')
.device('jpeg')
.output(tempNewPath2)
.input(tempFilePath)
.exec((err, stdout, stderr) => {
if (!err) {
console.log('gs executed w/o error');
console.log('stdout', stdout);
console.log('stderr', stderr);
resolve();
} else {
console.log('gs error:', err);
reject(err);
}
});
});

}).then(async () => {
console.log('PNG created at', tempNewPath1 + 'and' + tempNewPath2);

//uploading the files back to firebase storage
await bucket.upload(tempNewPath1, {
destination: '/files/' + fileID + '/' + fileID + '-01.jpeg'
});

return bucket.upload(tempNewPath2, {
destination: '/files/' + fileID + '/' + fileID + '-02.jpeg'
});



}).then(() => {
//once the files have been uploaded delete the local temporary
//files to free up disk space.
fs.unlinkSync(tempNewPath1);
fs.unlinkSync(tempNewPath2);
return fs.unlinkSync(tempFilePath);
}).catch((err) => {
console.log('exception:', err);
return err;
});

});
15 changes: 15 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# lambda-ghostscript

This repository contains a compiled version of Ghostscript which works with Amazon Web Services(AWS) Lambda.

These binaries are based on Ghostscript 9.20.

AWS Lambda is using an old version of Ghostscript which causes many issues. Lambda has the ability to use local libraries which can be packed inside the function archive, this repository can save you some time.

You should include the files inside your project and use a Ghostscript Node js package to use it.

This is an npm package to call Ghostscript functions:

https://github.com/sina-masnadi/node-gs

After copying the compiled Ghostscript files to your project and adding the npm package, you can use the executablePath('path to ghostscript') function to point the package to the compiled Ghostscript files that you added earlier.
46 changes: 46 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/dvipdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh
# Convert DVI to PDF.
#
# Please contact Andrew Ford <A.Ford@ford-mason.co.uk> with any questions
# about this file.
#
# Based on ps2pdf

# This definition is changed on install to match the
# executable name set in the makefile
GS_EXECUTABLE=gs


OPTIONS=""
DVIPSOPTIONS=""
while true
do
case "$1" in
-R*) DVIPSOPTIONS="$DVIPSOPTIONS $1";;
-?*) OPTIONS="$OPTIONS $1" ;;
*) break ;;
esac
shift
done

if [ $# -lt 1 -o $# -gt 2 ]; then
echo "Usage: `basename \"$0\"` [options...] input.dvi [output.pdf]" 1>&2
exit 1
fi

infile=$1;

if [ $# -eq 1 ]
then
case "${infile}" in
*.dvi) base=`basename "${infile}" .dvi` ;;
*) base=`basename "${infile}"` ;;
esac
outfile="${base}".pdf
else
outfile=$2
fi

# We have to include the options twice because -I only takes effect if it
# appears before other options.
exec dvips -Ppdf $DVIPSOPTIONS -q -f "$infile" | $GS_EXECUTABLE $OPTIONS -q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr -sOutputFile="$outfile" $OPTIONS -c .setpdfwrite -
28 changes: 28 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/eps2eps
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
# "Distill" Encapsulated PostScript.

# This definition is changed on install to match the
# executable name set in the makefile
GS_EXECUTABLE=gs
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
if test ! -x "$gs"; then
gs="$GS_EXECUTABLE"
fi
GS_EXECUTABLE="$gs"

OPTIONS="-dDEVICEWIDTH=250000 -dDEVICEHEIGHT=250000"
while true
do
case "$1" in
-?*) OPTIONS="$OPTIONS $1" ;;
*) break ;;
esac
shift
done

if [ $# -ne 2 ]; then
echo "Usage: `basename \"$0\"` ...switches... input.eps output.eps" 1>&2
exit 1
fi

exec "$GS_EXECUTABLE" -q -sDEVICE=eps2write -sstdout=%stderr "-sOutputFile=$2" -dNOPAUSE -dBATCH -P- -dSAFER $OPTIONS "$1"
12 changes: 12 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/font2c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# This definition is changed on install to match the
# executable name set in the makefile
GS_EXECUTABLE=gs
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
if test ! -x "$gs"; then
gs="$GS_EXECUTABLE"
fi
GS_EXECUTABLE="$gs"

exec "$GS_EXECUTABLE" -q -P- -dSAFER -dNODISPLAY -dWRITESYSTEMDICT -- font2c.ps "$@"
Binary file not shown.
12 changes: 12 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/gsbj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# This definition is changed on install to match the
# executable name set in the makefile
GS_EXECUTABLE=gs
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
if test ! -x "$gs"; then
gs="$GS_EXECUTABLE"
fi
GS_EXECUTABLE="$gs"

exec "$GS_EXECUTABLE" -q -sDEVICE=bj10e -r180 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
Loading
Oops, something went wrong.