This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PostScript.py
144 lines (126 loc) · 5.3 KB
/
PostScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# PostScript.py
__version__ = "v20201113"
# Built-In Libraries
from PJL_Commands.PJL_PS import blank
import files
from PyPDF2.pdf import PageObject
from PyPDF2 import PdfFileWriter, PdfFileReader
import PyPDF2
import json
import os
import glob
import sys
import locale
import subprocess
import log
print = log.Print
input = log.Input
if(os.name == "posix"):
GHOSTSCRIPT_PATH = 'gs'
else:
GHOSTSCRIPT_VERSION = os. listdir('C:/Program Files/gs/')[-1]
GHOSTSCRIPT_PATH = 'C:/"Program Files"/gs/'+GHOSTSCRIPT_VERSION+'/bin/gswin64c.exe'
def ghostscript(gsCMD):
"""
Processes the requested ghostscript command through the ghostscript program.
Parameters:
gsCMD (str): The Ghostscript command to be processed.
Returns:
bool: Unused output.
"""
output = subprocess.Popen(gsCMD, stdout=subprocess.PIPE, shell=True)
(out, err) = output.communicate() # pylint: disable=unused-variable
out = str(out).replace("b'GPL Ghostscript 9.27 (2019-04-04)\\nCopyright (C) 2018 Artifex Software, Inc. All rights reserved.\\nThis software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:\\nsee the file COPYING for details.\\n", "")
out = out.split("\\n")
for line in out:
log.logger.debug(line)
return 1
def ticket_conversion(PATH):
"""
Converts the Tickets from PDF TO PS
Parameters:
PATH (str): The Path of the PDF Ticket to be Converted.
Returns:
void: Unused output
"""
# https://stackoverflow.com/questions/39574096/how-to-delete-pages-from-pdf-file-using-python
pdf = PyPDF2.PdfFileReader(PATH, "rb")
output = PyPDF2.PdfFileWriter()
if (int(pdf.getNumPages())) > 1:
output.addPage(pdf.getPage(0))
with open(PATH, 'wb') as f:
output.write(f)
# Processes the Conversion
gsCMD = "".join([GHOSTSCRIPT_PATH, ' -dNOPAUSE -dBATCH -sDEVICE=ps2write -sPAPERSIZE=letter -dFIXEDMEDIA -dPDFFitPage -sOutputFile="',
PATH, '.ps" "', PATH, '" "', PATH, '" -c quit'])
ghostscript(gsCMD)
def postscript_conversion(order):
"""
Converts order PDF files to PS
During Email download, pdf files are converted to postscript.
Parameters:
order (object) : The object containing all the information for the current order.
Returns:
bool: Unused output
"""
F = "".join([order.OD,
"/", order.NAME, "/PostScript"])
try:
# Creates the Directory for Output
if not os.path.exists(F):
os.makedirs(F)
print("".join(["Successfully created the directory ", F]))
except OSError:
print("".join(["Creation of the directory failed ", F]))
for i in range(len(order.FILE_NAMES)):
# Processes the Conversion
gsCMD = "".join([GHOSTSCRIPT_PATH, ' -dNOPAUSE -dBATCH -sDEVICE=ps2write -sPAPERSIZE=letter -dFIXEDMEDIA -dPDFFitPage -sOutputFile="', order.OD, '"/"' +
order.NAME, '"/PostScript/"', order.FILE_NAMES[i], '.ps" "', order.OD, '"/"', order.NAME, '"/"', order.FILE_NAMES[i], '" -c quit'])
ghostscript(gsCMD)
return True
def file_merge(order, DUPLEX_STATE):
"""
If needed, merges multiple files together
Some orders get all files merged into one big file.
If duplex, blank pages are added to the end of all odd number documents.
Parameters:
order (object): The object containing all the information for the current order.
DUPLEX_STATE (str) : Whether the order is duplex or not.
Returns:
bool: Unused output
"""
with open('Blank.ps', 'wb') as f:
f.write(blank)
FILES_path = ''
if DUPLEX_STATE == 2: # Adds blanks for doublesided uncollated printing
for i in range(len(order.FILE_NAMES)):
try:
f = open(
'/'.join([order.OD, order.NAME, order.FILE_NAMES[i]]), "rb")
pdf = PyPDF2.PdfFileReader(f)
pdf = pdf.getNumPages()
f.close()
except:
log.logger.exception("")
pdf = order.PAGE_COUNTS
if (int(pdf) % 2) != 0: # If odd number pages, add blank page
print("Adding Blank Page!")
output = "".join(
['"', order.OD, '/', order.NAME, '/PostScript/', order.FILE_NAMES[i], '.ps"'])
src = "".join(['"', order.OD, '/',
order.NAME, '/', order.FILE_NAMES[i], '"'])
gsCMD = "".join(
[GHOSTSCRIPT_PATH, ' -dNOPAUSE -dBATCH -sDEVICE=ps2write -sPAPERSIZE=letter -dFIXEDMEDIA -dPDFFitPage -sOutputFile=', output, ' ', src, ' Blank.ps -c quit'])
ghostscript(gsCMD)
# Merges order.FILE_NAMES for Uncollated Printing with SlipSheets
for FILE in order.FILE_NAMES:
FILES_path = "".join([FILES_path, '"', order.OD,
'/', order.NAME, '/PostScript/', FILE, '.ps" '])
print("These Files are being MERGED!!")
output = "".join(
[order.OD, '/', order.NAME, '/', order.NAME, '.ps'])
gsCMD = "".join(
[GHOSTSCRIPT_PATH, ' -dNOPAUSE -dBATCH -sDEVICE=ps2write -sPAPERSIZE=letter -dFIXEDMEDIA -dPDFFitPage -sOutputFile="', output, '" ', FILES_path, ' -c quit'])
# Processes the Conversion
ghostscript(gsCMD)
return True