44import tempfile
55import logging
66
7+
78class FileService :
89 def compress_pdf_buffer (self , pdf_bytes : bytes , quality : str ) -> bytes :
9- logging .debug (f"Compressing { len (pdf_bytes )} bytes with quality={ quality } in buffer" )
10+ logging .debug (
11+ f"Compressing { len (pdf_bytes )} bytes with quality={ quality } in buffer"
12+ )
1013
1114 gs_cmd = [
1215 "gs" ,
@@ -24,7 +27,7 @@ def compress_pdf_buffer(self, pdf_bytes: bytes, quality: str) -> bytes:
2427 "-dFILTERTEXTANNOTATIONS=true" ,
2528 "-dFILTERIMAGEANNOTATIONS=true" ,
2629 "-sOutputFile=-" ,
27- "-"
30+ "-" ,
2831 ]
2932 gs_process : subprocess .Popen = None
3033
@@ -33,12 +36,14 @@ def compress_pdf_buffer(self, pdf_bytes: bytes, quality: str) -> bytes:
3336 gs_cmd ,
3437 stdin = subprocess .PIPE ,
3538 stdout = subprocess .PIPE ,
36- stderr = subprocess .PIPE
39+ stderr = subprocess .PIPE ,
3740 )
3841
3942 gs_output , gs_err = gs_process .communicate (input = pdf_bytes , timeout = 60 )
4043 if gs_process .returncode != 0 :
41- raise RuntimeError (f"Error to compress file: { gs_err .decode ('utf-8' , 'ignore' )} " )
44+ raise RuntimeError (
45+ f"Error to compress file: { gs_err .decode ('utf-8' , 'ignore' )} "
46+ )
4247
4348 if not gs_output :
4449 raise RuntimeError ("Compression failed: no output generated" )
@@ -53,7 +58,9 @@ def compress_pdf_buffer(self, pdf_bytes: bytes, quality: str) -> bytes:
5358 raise RuntimeError (f"Unexpected error: { str (e )} " )
5459
5560 def compress_pdf_tmp (self , pdf_bytes : bytes , quality : str ) -> bytes :
56- logging .debug (f"Compressing { len (pdf_bytes )} bytes with quality={ quality } in tmp disk" )
61+ logging .debug (
62+ f"Compressing { len (pdf_bytes )} bytes with quality={ quality } in tmp disk"
63+ )
5764
5865 input_path = None
5966 output_path = None
@@ -64,10 +71,14 @@ def compress_pdf_tmp(self, pdf_bytes: bytes, quality: str) -> bytes:
6471 input_file .write (pdf_bytes )
6572 input_path = input_file .name
6673
67- with tempfile .NamedTemporaryFile (suffix = "_gs.pdf" , delete = False ) as output_file :
74+ with tempfile .NamedTemporaryFile (
75+ suffix = "_gs.pdf" , delete = False
76+ ) as output_file :
6877 output_path = output_file .name
6978
70- with tempfile .NamedTemporaryFile (suffix = "_qpdf.pdf" , delete = False ) as output_compress_file :
79+ with tempfile .NamedTemporaryFile (
80+ suffix = "_qpdf.pdf" , delete = False
81+ ) as output_compress_file :
7182 output_compress_path = output_compress_file .name
7283
7384 gs_cmd = [
@@ -86,18 +97,17 @@ def compress_pdf_tmp(self, pdf_bytes: bytes, quality: str) -> bytes:
8697 "-dFILTERTEXTANNOTATIONS=true" ,
8798 "-dFILTERIMAGEANNOTATIONS=true" ,
8899 f"-sOutputFile={ output_path } " ,
89- input_path
100+ input_path ,
90101 ]
91102
92103 gs_process = subprocess .run (
93- gs_cmd ,
94- stdout = subprocess .PIPE ,
95- stderr = subprocess .PIPE ,
96- timeout = 60
104+ gs_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE , timeout = 60
97105 )
98106
99107 if gs_process .returncode != 0 :
100- raise RuntimeError (f"Error to compress file: { gs_process .stderr .decode ('utf-8' , 'ignore' )} " )
108+ raise RuntimeError (
109+ f"Error to compress file: { gs_process .stderr .decode ('utf-8' , 'ignore' )} "
110+ )
101111
102112 qpdf_cmd = [
103113 "qpdf" ,
@@ -109,14 +119,13 @@ def compress_pdf_tmp(self, pdf_bytes: bytes, quality: str) -> bytes:
109119 output_compress_path ,
110120 ]
111121 qpdf_process = subprocess .run (
112- qpdf_cmd ,
113- stdout = subprocess .PIPE ,
114- stderr = subprocess .PIPE ,
115- timeout = 60
122+ qpdf_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE , timeout = 60
116123 )
117124
118125 if qpdf_process .returncode != 0 :
119- raise RuntimeError (f"Error to compress file: { qpdf_process .stderr .decode ('utf-8' , 'ignore' )} " )
126+ raise RuntimeError (
127+ f"Error to compress file: { qpdf_process .stderr .decode ('utf-8' , 'ignore' )} "
128+ )
120129
121130 with open (output_compress_path , "rb" ) as file :
122131 return file .read ()
@@ -132,38 +141,44 @@ def compress_pdf_tmp(self, pdf_bytes: bytes, quality: str) -> bytes:
132141 unlink (file_path )
133142 except Exception :
134143 pass
135-
144+
136145 def merge_pdf (self , bytes_list : List [bytes ]):
137146 input_paths = []
138147 output_path = None
139148
140149 try :
141150 for i , pdf_bytes in enumerate (bytes_list ):
142- with tempfile .NamedTemporaryFile (suffix = f"_{ i } .pdf" , delete = False ) as input_file :
151+ with tempfile .NamedTemporaryFile (
152+ suffix = f"_{ i } .pdf" , delete = False
153+ ) as input_file :
143154 input_file .write (pdf_bytes )
144155 input_paths .append (input_file .name )
145156
146- with tempfile .NamedTemporaryFile (suffix = "_gs.pdf" , delete = False ) as output_file :
157+ with tempfile .NamedTemporaryFile (
158+ suffix = "_gs.pdf" , delete = False
159+ ) as output_file :
147160 output_path = output_file .name
148161
149162 qpdf_cmd = [
150163 "qpdf" ,
151164 "--linearize" ,
152165 "--empty" ,
153- "--pages" , * input_paths , "--" , output_path ,
166+ "--pages" ,
167+ * input_paths ,
168+ "--" ,
169+ output_path ,
154170 "--object-streams=generate" ,
155171 "--compress-streams=y" ,
156- "--recompress-flate"
172+ "--recompress-flate" ,
157173 ]
158174 qpdf_process = subprocess .run (
159- qpdf_cmd ,
160- stdout = subprocess .PIPE ,
161- stderr = subprocess .PIPE ,
162- timeout = 60
175+ qpdf_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE , timeout = 60
163176 )
164177
165178 if qpdf_process .returncode != 0 :
166- raise RuntimeError (f"Error merging PDFs: { qpdf_process .stderr .decode ('utf-8' , 'ignore' )} " )
179+ raise RuntimeError (
180+ f"Error merging PDFs: { qpdf_process .stderr .decode ('utf-8' , 'ignore' )} "
181+ )
167182
168183 with open (output_path , "rb" ) as merged :
169184 return merged .read ()
0 commit comments