bluenevus commited on
Commit
728b915
·
verified ·
1 Parent(s): 611be2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -31
app.py CHANGED
@@ -31,17 +31,17 @@ def compress_pdf(input_file, url, strength):
31
  pdf_content.seek(0)
32
 
33
  if strength == "Low":
34
- compress_level = 1
35
  elif strength == "Medium":
36
- compress_level = 2
37
  else: # High
38
- compress_level = 3
39
 
40
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
41
  temp_file_path = temp_file.name
42
 
43
  pdf = pikepdf.Pdf.open(pdf_content)
44
- pdf.save(temp_file_path, compress_streams=True, compress_images=True, optimize_images=True)
45
 
46
  # Check the compression ratio achieved
47
  compressed_size = os.path.getsize(temp_file_path)
@@ -57,30 +57,4 @@ def compress_pdf(input_file, url, strength):
57
  except Exception as e:
58
  return None, f"Error compressing PDF: {str(e)}"
59
 
60
- # The rest of the code remains the same
61
-
62
- def process_and_compress(input_file, url, strength):
63
- output_file, message = compress_pdf(input_file, url, strength)
64
- if output_file:
65
- return output_file, message
66
- else:
67
- return None, message
68
-
69
- with gr.Blocks() as demo:
70
- gr.Markdown("# PDF Compressor")
71
- with gr.Row():
72
- input_file = gr.File(label="Upload PDF")
73
- url_input = gr.Textbox(label="Or enter PDF URL")
74
- strength = gr.Radio(["Low", "Medium", "High"], label="Compression Strength", value="Medium", info="Low: Minimal compression, Medium: Moderate compression, High: Maximum compression")
75
- compress_btn = gr.Button("Compress")
76
- output_file = gr.File(label="Download Compressed PDF")
77
- message = gr.Textbox(label="Message")
78
-
79
- compress_btn.click(
80
- process_and_compress,
81
- inputs=[input_file, url_input, strength],
82
- outputs=[output_file, message]
83
- )
84
-
85
- if __name__ == "__main__":
86
- demo.launch(share=True)
 
31
  pdf_content.seek(0)
32
 
33
  if strength == "Low":
34
+ compress_level = pikepdf.compress.CompressSettings(level=1)
35
  elif strength == "Medium":
36
+ compress_level = pikepdf.compress.CompressSettings(level=2)
37
  else: # High
38
+ compress_level = pikepdf.compress.CompressSettings(level=3)
39
 
40
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
41
  temp_file_path = temp_file.name
42
 
43
  pdf = pikepdf.Pdf.open(pdf_content)
44
+ pdf.save(temp_file_path, compress_streams=True, compress=compress_level)
45
 
46
  # Check the compression ratio achieved
47
  compressed_size = os.path.getsize(temp_file_path)
 
57
  except Exception as e:
58
  return None, f"Error compressing PDF: {str(e)}"
59
 
60
+ # The rest of the code remains the same