use pdfcpu for pagecount
All checks were successful
Go / build (push) Successful in 13m36s

This commit is contained in:
2025-07-02 00:50:04 +02:00
parent 16c68cd0f8
commit b75c46ec2f
4 changed files with 43 additions and 51 deletions

View File

@@ -4,8 +4,7 @@ import (
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/dslipak/pdf"
"io"
"github.com/pdfcpu/pdfcpu/pkg/api"
)
func GenerateSessionId(length int) string {
@@ -21,46 +20,13 @@ func GenerateToken() string {
return GenerateSessionId(16)
}
const match = "/Page\x00"
// Pages reads the given io.ByteReader until EOF is reached, returning the
// number of pages encountered.
func Pages(reader io.ByteReader) (pages int) {
i := 0
for {
b, err := reader.ReadByte()
if err != nil {
fmt.Println(err)
return
}
check:
switch match[i] {
case 0:
if !(b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') {
pages++
}
i = 0
goto check
case b:
i++
default:
i = 0
}
}
}
// PagesAtPath opens a PDF file at the given file path, returning the number
// of pages found.
func CountPagesAtPath(path string) (pages int) {
r, err := pdf.Open(path)
ctx, err := api.ReadContextFile(path)
if err != nil {
fmt.Println("LOL")
fmt.Println(err)
return 0
fmt.Println("Error reading PDF:", err)
return
}
pages = r.NumPage()
pages = ctx.PageCount
return
}