diff --git a/ai.go b/ai.go index 5e93877..470b43e 100644 --- a/ai.go +++ b/ai.go @@ -116,8 +116,14 @@ func ConvertToImage(filePath string) ([]byte, error) { return nil, fmt.Errorf("pdftoppm failed: %w", err) } - pngPath := filepath.Join(tmpDir, "page-1.png") - return os.ReadFile(pngPath) + // pdftoppm uses variable-width zero-padding depending on page count + // (e.g. page-01.png for <100 pages, page-001.png for <1000 pages). + // Glob for the first match instead of hardcoding "page-1.png". + matches, err := filepath.Glob(filepath.Join(tmpDir, "page-*.png")) + if err != nil || len(matches) == 0 { + return nil, fmt.Errorf("pdftoppm output not found in %s", tmpDir) + } + return os.ReadFile(matches[0]) } // Image files — read directly @@ -483,8 +489,13 @@ func ProcessPDFPageByPage(filePath string, jobID string) (string, error) { continue } - pngPath := filepath.Join(tmpDir, fmt.Sprintf("page-%d.png", page)) - imageData, err := os.ReadFile(pngPath) + // Glob for the output — pdftoppm zero-pads based on total page count + pageMatches, _ := filepath.Glob(filepath.Join(tmpDir, "page-*.png")) + if len(pageMatches) == 0 { + os.RemoveAll(tmpDir) + continue + } + imageData, err := os.ReadFile(pageMatches[0]) os.RemoveAll(tmpDir) if err != nil { continue