Skip to content

Commit

Permalink
Merge branch '224-need-a-script-to-compress-pdf' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
profvjreddi committed May 26, 2024
2 parents 99bdd81 + 0f07a90 commit cf580bd
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions scripts/quarto_publish/gs_compress_pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import argparse
import subprocess
import sys

def convert_pdf(input_file, output_file, settings='/screen', compatibility='1.4', debug=False):
command = [
'gs',
'-sDEVICE=pdfwrite',
'-dNOPAUSE',
'-dQUIET' if not debug else '-dQUIET=false',
'-dBATCH',
f'-dPDFSETTINGS={settings}',
f'-dCompatibilityLevel={compatibility}',
f'-sOutputFile={output_file}',
input_file
]

if debug:
print(f"Running command: {' '.join(command)}")

try:
result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if debug:
print(result.stdout.decode())
except subprocess.CalledProcessError as e:
print(f"Error: {e.stderr.decode()}", file=sys.stderr)
sys.exit(e.returncode)

def main():
parser = argparse.ArgumentParser(description="Convert PDF using Ghostscript with various options.")
parser.add_argument('-i', '--input', required=True, help="Input PDF file")
parser.add_argument('-o', '--output', required=True, help="Output PDF file")
parser.add_argument('-s', '--settings', default='/screen', help="PDF settings (default: /screen)")
parser.add_argument('-c', '--compatibility', default='1.4', help="PDF compatibility level (default: 1.4)")
parser.add_argument('-d', '--debug', action='store_true', help="Enable debug mode")

args = parser.parse_args()

convert_pdf(args.input, args.output, settings=args.settings, compatibility=args.compatibility, debug=args.debug)

if __name__ == "__main__":
main()

0 comments on commit cf580bd

Please sign in to comment.