Skip to content

Commit f855d54

Browse files
authored
Merge pull request #6 from zeetee1235/fix/pdftoppm-filename-compat
Fix pdftoppm output filename detection across versions
2 parents ba85841 + f186c74 commit f855d54

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

src/ocr/renderer.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,26 @@ impl PageRenderer {
4848
anyhow::bail!("pdftoppm failed with status: {status}");
4949
}
5050

51-
// pdftoppm will create a file like `<prefix>-1.png` for this page
52-
let image_path = self
53-
.out_dir
54-
.join(format!("page_{:03}-{}.png", page_number, page_number));
55-
56-
if !image_path.exists() {
57-
anyhow::bail!(
58-
"expected rendered image not found: {}",
59-
image_path.display()
60-
);
61-
}
51+
// pdftoppm naming varies by version (`-1`, `-01`, etc.). Find the
52+
// rendered output by prefix rather than assuming one suffix pattern.
53+
let page_prefix = format!("page_{:03}-", page_number);
54+
let image_path = fs::read_dir(&self.out_dir)?
55+
.filter_map(Result::ok)
56+
.map(|entry| entry.path())
57+
.filter(|path| path.is_file())
58+
.find(|path| {
59+
path.file_name()
60+
.and_then(|name| name.to_str())
61+
.map(|name| name.starts_with(&page_prefix) && name.ends_with(".png"))
62+
.unwrap_or(false)
63+
})
64+
.ok_or_else(|| {
65+
anyhow::anyhow!(
66+
"expected rendered image not found for prefix {} in {}",
67+
page_prefix,
68+
self.out_dir.display()
69+
)
70+
})?;
6271

6372
// We could inspect the image to get exact dimensions in the future.
6473
// For now, approximate using the default layout used elsewhere.

0 commit comments

Comments
 (0)