This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user