Skip to content

Commit

Permalink
Fix Ghostscript PDF/A progressbar not displaying
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarlow83 committed May 19, 2024
1 parent 26432c3 commit 950c700
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/ocrmypdf/_exec/ghostscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ def __init__(self, progressbar_class):
self.progressbar_class = progressbar_class
self.progressbar = None

def __enter__(self):
# We can't actually set up the progressbar here, because we don't know
# how many pages there are until the first __call__() happens. So we
# do it in __call__().
return self

def __exit__(self, exc_type, exc_value, traceback):
if self.progressbar:
return self.progressbar.__exit__(exc_type, exc_value, traceback)
return False

def __call__(self, line):
if not self.progressbar_class:
return
Expand All @@ -187,7 +198,8 @@ def __call__(self, line):
self.progressbar = self.progressbar_class(
total=self.count, desc="PDF/A conversion", unit='page'
)
return
# Now that we know the count, we can set up the progressbar.
self.progressbar.__enter__()
else:
if self.re_page.match(line.strip()):
self.progressbar.update()
Expand Down Expand Up @@ -265,7 +277,10 @@ def generate_pdfa(
)
args_gs.extend(fspath(s) for s in pdf_pages) # Stringify Path objs
try:
with Path(output_file).open('wb') as output:
with (
Path(output_file).open('wb') as output,
GhostscriptFollower(progressbar_class) as pbar,
):
p = run_polling_stderr(
args_gs,
stdout=output,
Expand All @@ -274,7 +289,7 @@ def generate_pdfa(
text=True,
encoding='utf-8',
errors='replace',
callback=GhostscriptFollower(progressbar_class),
callback=pbar,
)
except CalledProcessError as e:
# Ghostscript does not change return code when it fails to create
Expand Down

0 comments on commit 950c700

Please sign in to comment.