Skip to content

Commit d077765

Browse files
committed
Ghostscript sample added
1 parent b699699 commit d077765

File tree

332 files changed

+440313
-99
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

332 files changed

+440313
-99
lines changed

dialogflow/functions/index.js

-72
This file was deleted.

dialogflow/functions/package.json

-27
This file was deleted.
File renamed without changes.
File renamed without changes.

ghostscript-nodejs/functions/index.js

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
'use strict';
17+
18+
const functions = require('firebase-functions');
19+
const { Storage } = require('@google-cloud/storage');
20+
const gcs = new Storage();
21+
const spawn = require('child-process-promise').spawn;
22+
const path = require('path');
23+
const os = require('os');
24+
const fs = require('fs');
25+
var gs = require('gs');
26+
27+
28+
//This function triggers whenever any pdf is uploaded to the firebase storage
29+
//and attempts to generate
30+
31+
exports.makePreviews = functions.storage.object().onFinalize(async (object, event) => {
32+
33+
//Checking for pdf files
34+
if (!object.name.endsWith('.pdf')) return false;
35+
36+
const filePath = object.name;
37+
38+
//slicing name and path
39+
const splitFileName = object.name.split(".");
40+
const fileID = splitFileName[0].split("/")[1];
41+
42+
console.log("File ID -", fileID);
43+
44+
//creating temporary path strings for gcp file system
45+
const fileName = path.basename(filePath);
46+
const tempFilePath = path.join(os.tmpdir(), fileName);
47+
48+
const newName1 = path.basename(filePath, '.pdf') + '01.jpeg';
49+
const tempNewPath1 = path.join(os.tmpdir(), newName1);
50+
51+
const newName2 = path.basename(filePath, '.pdf') + '02.jpeg';
52+
const tempNewPath2 = path.join(os.tmpdir(), newName2);
53+
54+
55+
//downloading file from firebase storage
56+
const bucket = gcs.bucket(object.bucket);
57+
58+
return bucket.file(filePath).download({
59+
destination: tempFilePath
60+
}).then(async () => {
61+
console.log('PDF downloaded locally to', tempFilePath);
62+
63+
//generating two preview JPEGS
64+
await new Promise((resolve, reject) => {
65+
gs()
66+
.batch()
67+
.option('-dFirstPage=1')
68+
.option('-dLastPage=1')
69+
.nopause()
70+
.res(90)
71+
.executablePath('gs')
72+
.device('jpeg')
73+
.output(tempNewPath1)
74+
.input(tempFilePath)
75+
.exec((err, stdout, stderr) => {
76+
if (!err) {
77+
console.log('gs executed w/o error');
78+
console.log('stdout', stdout);
79+
console.log('stderr', stderr);
80+
resolve();
81+
} else {
82+
console.log('gs error:', err);
83+
reject(err);
84+
}
85+
});
86+
});
87+
88+
return new Promise((resolve, reject) => {
89+
gs()
90+
.batch()
91+
.option('-dFirstPage=2')
92+
.option('-dLastPage=2')
93+
.nopause()
94+
.res(90)
95+
.executablePath('gs')
96+
.device('jpeg')
97+
.output(tempNewPath2)
98+
.input(tempFilePath)
99+
.exec((err, stdout, stderr) => {
100+
if (!err) {
101+
console.log('gs executed w/o error');
102+
console.log('stdout', stdout);
103+
console.log('stderr', stderr);
104+
resolve();
105+
} else {
106+
console.log('gs error:', err);
107+
reject(err);
108+
}
109+
});
110+
});
111+
112+
}).then(async () => {
113+
console.log('PNG created at', tempNewPath1 + 'and' + tempNewPath2);
114+
115+
//uploading the files back to firebase storage
116+
await bucket.upload(tempNewPath1, {
117+
destination: '/files/' + fileID + '/' + fileID + '-01.jpeg'
118+
});
119+
120+
return bucket.upload(tempNewPath2, {
121+
destination: '/files/' + fileID + '/' + fileID + '-02.jpeg'
122+
});
123+
124+
125+
126+
}).then(() => {
127+
//once the files have been uploaded delete the local temporary
128+
//files to free up disk space.
129+
fs.unlinkSync(tempNewPath1);
130+
fs.unlinkSync(tempNewPath2);
131+
return fs.unlinkSync(tempFilePath);
132+
}).catch((err) => {
133+
console.log('exception:', err);
134+
return err;
135+
});
136+
137+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# lambda-ghostscript
2+
3+
This repository contains a compiled version of Ghostscript which works with Amazon Web Services(AWS) Lambda.
4+
5+
These binaries are based on Ghostscript 9.20.
6+
7+
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.
8+
9+
You should include the files inside your project and use a Ghostscript Node js package to use it.
10+
11+
This is an npm package to call Ghostscript functions:
12+
13+
https://github.com/sina-masnadi/node-gs
14+
15+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/sh
2+
# Convert DVI to PDF.
3+
#
4+
# Please contact Andrew Ford <[email protected]> with any questions
5+
# about this file.
6+
#
7+
# Based on ps2pdf
8+
9+
# This definition is changed on install to match the
10+
# executable name set in the makefile
11+
GS_EXECUTABLE=gs
12+
13+
14+
OPTIONS=""
15+
DVIPSOPTIONS=""
16+
while true
17+
do
18+
case "$1" in
19+
-R*) DVIPSOPTIONS="$DVIPSOPTIONS $1";;
20+
-?*) OPTIONS="$OPTIONS $1" ;;
21+
*) break ;;
22+
esac
23+
shift
24+
done
25+
26+
if [ $# -lt 1 -o $# -gt 2 ]; then
27+
echo "Usage: `basename \"$0\"` [options...] input.dvi [output.pdf]" 1>&2
28+
exit 1
29+
fi
30+
31+
infile=$1;
32+
33+
if [ $# -eq 1 ]
34+
then
35+
case "${infile}" in
36+
*.dvi) base=`basename "${infile}" .dvi` ;;
37+
*) base=`basename "${infile}"` ;;
38+
esac
39+
outfile="${base}".pdf
40+
else
41+
outfile=$2
42+
fi
43+
44+
# We have to include the options twice because -I only takes effect if it
45+
# appears before other options.
46+
exec dvips -Ppdf $DVIPSOPTIONS -q -f "$infile" | $GS_EXECUTABLE $OPTIONS -q -P- -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr -sOutputFile="$outfile" $OPTIONS -c .setpdfwrite -
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
# "Distill" Encapsulated PostScript.
3+
4+
# This definition is changed on install to match the
5+
# executable name set in the makefile
6+
GS_EXECUTABLE=gs
7+
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
8+
if test ! -x "$gs"; then
9+
gs="$GS_EXECUTABLE"
10+
fi
11+
GS_EXECUTABLE="$gs"
12+
13+
OPTIONS="-dDEVICEWIDTH=250000 -dDEVICEHEIGHT=250000"
14+
while true
15+
do
16+
case "$1" in
17+
-?*) OPTIONS="$OPTIONS $1" ;;
18+
*) break ;;
19+
esac
20+
shift
21+
done
22+
23+
if [ $# -ne 2 ]; then
24+
echo "Usage: `basename \"$0\"` ...switches... input.eps output.eps" 1>&2
25+
exit 1
26+
fi
27+
28+
exec "$GS_EXECUTABLE" -q -sDEVICE=eps2write -sstdout=%stderr "-sOutputFile=$2" -dNOPAUSE -dBATCH -P- -dSAFER $OPTIONS "$1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# This definition is changed on install to match the
4+
# executable name set in the makefile
5+
GS_EXECUTABLE=gs
6+
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
7+
if test ! -x "$gs"; then
8+
gs="$GS_EXECUTABLE"
9+
fi
10+
GS_EXECUTABLE="$gs"
11+
12+
exec "$GS_EXECUTABLE" -q -P- -dSAFER -dNODISPLAY -dWRITESYSTEMDICT -- font2c.ps "$@"
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# This definition is changed on install to match the
4+
# executable name set in the makefile
5+
GS_EXECUTABLE=gs
6+
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
7+
if test ! -x "$gs"; then
8+
gs="$GS_EXECUTABLE"
9+
fi
10+
GS_EXECUTABLE="$gs"
11+
12+
exec "$GS_EXECUTABLE" -q -sDEVICE=bj10e -r180 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# This definition is changed on install to match the
4+
# executable name set in the makefile
5+
GS_EXECUTABLE=gs
6+
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
7+
if test ! -x "$gs"; then
8+
gs="$GS_EXECUTABLE"
9+
fi
10+
GS_EXECUTABLE="$gs"
11+
12+
exec "$GS_EXECUTABLE" -q -sDEVICE=deskjet -r300 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# This definition is changed on install to match the
4+
# executable name set in the makefile
5+
GS_EXECUTABLE=gs
6+
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
7+
if test ! -x "$gs"; then
8+
gs="$GS_EXECUTABLE"
9+
fi
10+
GS_EXECUTABLE="$gs"
11+
12+
exec "$GS_EXECUTABLE" -q -sDEVICE=djet500 -r300 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# This definition is changed on install to match the
4+
# executable name set in the makefile
5+
GS_EXECUTABLE=gs
6+
gs="`dirname \"$0\"`/$GS_EXECUTABLE"
7+
if test ! -x "$gs"; then
8+
gs="$GS_EXECUTABLE"
9+
fi
10+
GS_EXECUTABLE="$gs"
11+
12+
exec "$GS_EXECUTABLE" -q -sDEVICE=laserjet -r300 -P- -dSAFER -dNOPAUSE -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"

0 commit comments

Comments
 (0)