Skip to content

Commit

Permalink
Add api.PageDims
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Sep 1, 2019
1 parent 87915e4 commit 98e24ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
22 changes: 17 additions & 5 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"time"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
pdf "github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/validate"
"github.com/pkg/errors"
Expand Down Expand Up @@ -96,22 +95,35 @@ func PageCount(inFile string) (int, error) {
return ctx.PageCount, nil
}

// PageDims returns a sorted slice of mediaBox dimensions for inFile.
func PageDims(inFile string) ([]pdf.Dim, error) {
ctx, err := ReadContextFile(inFile)
// PageDims returns a sorted slice of mediaBox dimensions for rs.
func PageDims(rs io.ReadSeeker, conf *pdf.Configuration) ([]pdf.Dim, error) {
ctx, err := ReadContext(rs, conf)
if err != nil {
return nil, err
}

pd, err := ctx.PageDims()
if err != nil {
return nil, err
}
if len(pd) != ctx.PageCount {
return nil, errors.New("pdfcpu: corrupt page dimensions")
}

return pd, nil
}

// PageDimsFile returns a sorted slice of mediaBox dimensions for inFile.
func PageDimsFile(inFile string) ([]pdf.Dim, error) {
f, err := os.Open(inFile)
if err != nil {
return nil, err
}
defer f.Close()

return PageDims(f, pdf.NewDefaultConfiguration())
}

// ValidateContext validates a PDF context.
func ValidateContext(ctx *pdf.Context) error {
return validate.XRefTable(ctx.XRefTable)
Expand Down Expand Up @@ -962,7 +974,7 @@ func ImportImages(rs io.ReadSeeker, w io.Writer, imgs []io.Reader, imp *pdf.Impo
conf.Cmd = pdf.IMPORTIMAGES

if imp == nil {
imp = pdfcpu.DefaultImportConfig()
imp = pdf.DefaultImportConfig()
}

var (
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ func TestPageDimensions(t *testing.T) {
msg := "TestPageDimensions"
for _, fn := range AllPDFs(t, inDir) {
inFile := filepath.Join(inDir, fn)

// Retrieve page dimensions for inFile.
_, err := PageDims(inFile)
_, err := PageDimsFile(inFile)
if err != nil {
t.Fatalf("%s: %v\n", msg, err)
}
Expand Down

0 comments on commit 98e24ae

Please sign in to comment.