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 1 commit
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
  •  
  •  
  •  
Prev Previous commit
Next Next commit
Ghostscript sample added
  • Loading branch information
krharsh17 committed Sep 11, 2019
commit d07776547ed80ff97b8478dbb228d78e956f6713
72 changes: 0 additions & 72 deletions dialogflow/functions/index.js

This file was deleted.

27 changes: 0 additions & 27 deletions dialogflow/functions/package.json

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
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`" "$@"
12 changes: 12 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/gsdj
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=deskjet -r300 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
12 changes: 12 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/gsdj500
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=djet500 -r300 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
12 changes: 12 additions & 0 deletions ghostscript-nodejs/functions/lambda-ghostscript/bin/gslj
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=laserjet -r300 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
Loading
Oops, something went wrong.