Skip to content

Commit

Permalink
Add config flag postProcessValidate
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Mar 2, 2024
1 parent 8735421 commit 5b7d844
Show file tree
Hide file tree
Showing 36 changed files with 145 additions and 557 deletions.
101 changes: 6 additions & 95 deletions pkg/api/annotation.go
Expand Up @@ -20,7 +20,6 @@ import (
"io"
"os"

"github.com/pdfcpu/pdfcpu/pkg/log"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
"github.com/pkg/errors"
Expand All @@ -42,10 +41,6 @@ func Annotations(rs io.ReadSeeker, selectedPages []string, conf *model.Configura
return nil, err
}

if err := ctx.EnsurePageCount(); err != nil {
return nil, err
}

pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return nil, err
Expand All @@ -70,10 +65,6 @@ func AddAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages []string, ann m
return err
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return err
Expand All @@ -87,15 +78,7 @@ func AddAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages []string, ann m
return errors.New("pdfcpu: AddAnnotations: No annotations added")
}

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if err = ValidateContext(ctx); err != nil {
return err
}

return WriteContext(ctx, w)
return Write(ctx, w, conf)
}

// AddAnnotationsAsIncrement adds annotations for selected pages in rws and writes out a PDF increment.
Expand All @@ -118,10 +101,6 @@ func AddAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages []string, a
return errors.New("Incremental writing not supported for PDF version < V1.4 (Hint: Use pdfcpu optimize then try again)")
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return err
Expand All @@ -135,19 +114,7 @@ func AddAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages []string, a
return errors.New("pdfcpu: AddAnnotationsAsIncrement: No annotations added")
}

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if err = ValidateContext(ctx); err != nil {
return err
}

if _, err = rws.Seek(0, io.SeekEnd); err != nil {
return err
}

return WriteIncrement(ctx, rws)
return WriteIncr(ctx, rws, conf)
}

// AddAnnotationsFile adds annotations for selected pages to a PDF context read from inFile and writes the result to outFile.
Expand Down Expand Up @@ -216,10 +183,6 @@ func AddAnnotationsMap(rs io.ReadSeeker, w io.Writer, m map[int][]model.Annotati
return err
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

ok, err := pdfcpu.AddAnnotationsMap(ctx, m, false)
if err != nil {
return err
Expand All @@ -228,15 +191,7 @@ func AddAnnotationsMap(rs io.ReadSeeker, w io.Writer, m map[int][]model.Annotati
return errors.New("pdfcpu: AddAnnotationsMap: No annotations added")
}

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if err = ValidateContext(ctx); err != nil {
return err
}

return WriteContext(ctx, w)
return Write(ctx, w, conf)
}

// AddAnnotationsMapAsIncrement adds annotations in m to corresponding pages of rws and writes out a PDF increment.
Expand All @@ -259,10 +214,6 @@ func AddAnnotationsMapAsIncrement(rws io.ReadWriteSeeker, m map[int][]model.Anno
return errors.New("Increment writing not supported for PDF version < V1.4 (Hint: Use pdfcpu optimize then try again)")
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

ok, err := pdfcpu.AddAnnotationsMap(ctx, m, true)
if err != nil {
return err
Expand All @@ -271,19 +222,7 @@ func AddAnnotationsMapAsIncrement(rws io.ReadWriteSeeker, m map[int][]model.Anno
return errors.New("pdfcpu: AddAnnotationsMapAsIncrement: No annotations added")
}

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if err = ValidateContext(ctx); err != nil {
return err
}

if _, err = rws.Seek(0, io.SeekEnd); err != nil {
return err
}

return WriteIncrement(ctx, rws)
return WriteIncr(ctx, rws, conf)
}

// AddAnnotationsMapFile adds annotations in m to corresponding pages of inFile and writes the result to outFile.
Expand Down Expand Up @@ -354,10 +293,6 @@ func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, idsAndTypes
return err
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return err
Expand All @@ -371,15 +306,7 @@ func RemoveAnnotations(rs io.ReadSeeker, w io.Writer, selectedPages, idsAndTypes
return errors.New("pdfcpu: RemoveAnnotations: No annotation removed")
}

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if err = ValidateContext(ctx); err != nil {
return err
}

return WriteContext(ctx, w)
return Write(ctx, w, conf)
}

// RemoveAnnotationsAsIncrement removes annotations for selected pages by ids and object number
Expand All @@ -403,10 +330,6 @@ func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, idsAndT
return errors.New("pdfcpu: Incremental writing unsupported for PDF version < V1.4 (Hint: Use pdfcpu optimize then try again)")
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return err
Expand All @@ -420,19 +343,7 @@ func RemoveAnnotationsAsIncrement(rws io.ReadWriteSeeker, selectedPages, idsAndT
return errors.New("pdfcpu: RemoveAnnotationsAsIncrement: No annotation removed")
}

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if err = ValidateContext(ctx); err != nil {
return err
}

if _, err = rws.Seek(0, io.SeekEnd); err != nil {
return err
}

return WriteIncrement(ctx, rws)
return WriteIncr(ctx, rws, conf)
}

// RemoveAnnotationsFile removes annotations for selected pages by id and object number
Expand Down
47 changes: 32 additions & 15 deletions pkg/api/api.go
Expand Up @@ -55,6 +55,7 @@ func logDisclaimerPDF20() {
* Please let us know which feature you would like to see supported, *
* provide a sample PDF file and create an issue: *
* https://github.com/pdfcpu/pdfcpu/issues/new/choose *
* Thank you for using pdfcpu <3 *
*********************************************************************`

if log.ValidateEnabled() {
Expand Down Expand Up @@ -99,11 +100,9 @@ func ReadContextFile(inFile string) (*model.Context, error) {

// ValidateContext validates ctx.
func ValidateContext(ctx *model.Context) error {

if ctx.Version() == model.V20 {
logDisclaimerPDF20()
}

return validate.XRefTable(ctx.XRefTable)
}

Expand Down Expand Up @@ -149,11 +148,7 @@ func ReadAndValidate(rs io.ReadSeeker, conf *model.Configuration) (ctx *model.Co
return nil, err
}

if ctx.Version() == model.V20 {
logDisclaimerPDF20()
}

if err = validate.XRefTable(ctx.XRefTable); err != nil {
if err := ValidateContext(ctx); err != nil {
return nil, err
}

Expand All @@ -174,21 +169,43 @@ func ReadValidateAndOptimize(rs io.ReadSeeker, conf *model.Configuration) (ctx *
return ctx, nil
}

func logOperationStats(ctx *model.Context, op string, durRead, durVal, durOpt, durWrite, durTotal float64) {
func logWritingTo(s string) {
if log.CLIEnabled() {
log.CLI.Printf("writing %s...\n", s)
}
}

func Write(ctx *model.Context, w io.Writer, conf *model.Configuration) error {
if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}
model.TimingStats(op, durRead, durVal, durOpt, durWrite, durTotal)
if ctx.Read.FileSize > 0 {
ctx.Read.LogStats(ctx.Optimized)
ctx.Write.LogStats()

if conf.PostProcessValidate {
if err := ValidateContext(ctx); err != nil {
return err
}
}

return WriteContext(ctx, w)
}

func logWritingTo(s string) {
if log.CLIEnabled() {
log.CLI.Printf("writing %s...\n", s)
func WriteIncr(ctx *model.Context, rws io.ReadWriteSeeker, conf *model.Configuration) error {

if log.StatsEnabled() {
log.Stats.Printf("XRefTable:\n%s\n", ctx)
}

if conf.PostProcessValidate {
if err := ValidateContext(ctx); err != nil {
return err
}
}

if _, err := rws.Seek(0, io.SeekEnd); err != nil {
return err
}

return WriteIncrement(ctx, rws)
}

// EnsureDefaultConfigAt switches to the pdfcpu config dir located at path.
Expand Down
12 changes: 2 additions & 10 deletions pkg/api/attach.go
Expand Up @@ -107,11 +107,7 @@ func AddAttachments(rs io.ReadSeeker, w io.Writer, files []string, coll bool, co
return errors.New("pdfcpu: AddAttachments: No attachment added")
}

if err = WriteContext(ctx, w); err != nil {
return err
}

return nil
return WriteContext(ctx, w)
}

// AddAttachmentsFile embeds files into a PDF context read from inFile and writes the result to outFile.
Expand Down Expand Up @@ -180,11 +176,7 @@ func RemoveAttachments(rs io.ReadSeeker, w io.Writer, files []string, conf *mode
return errors.New("pdfcpu: RemoveAttachments: No attachment removed")
}

if err = WriteContext(ctx, w); err != nil {
return err
}

return nil
return WriteContext(ctx, w)
}

// RemoveAttachmentsFile deletes embedded files from a PDF context read from inFile and writes the result to outFile.
Expand Down
16 changes: 1 addition & 15 deletions pkg/api/booklet.go
Expand Up @@ -87,10 +87,6 @@ func Booklet(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nu
return err
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, true, true)
if err != nil {
return err
Expand All @@ -101,17 +97,7 @@ func Booklet(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nu
}
}

if err = ValidateContext(ctx); err != nil {
return err
}

if err = WriteContext(ctx, w); err != nil {
return err
}

log.Stats.Printf("XRefTable:\n%s\n", ctx)

return nil
return Write(ctx, w, conf)
}

// BookletFile rearranges PDF pages or images into a booklet layout and writes the result to outFile.
Expand Down
14 changes: 1 addition & 13 deletions pkg/api/bookmark.go
Expand Up @@ -70,10 +70,6 @@ func ExportBookmarksJSON(rs io.ReadSeeker, w io.Writer, source string, conf *mod
return err
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

ok, err := pdfcpu.ExportBookmarksJSON(ctx, source, w)
if err != nil {
return err
Expand Down Expand Up @@ -138,10 +134,6 @@ func ImportBookmarks(rs io.ReadSeeker, rd io.Reader, w io.Writer, replace bool,
return err
}

if err := ctx.EnsurePageCount(); err != nil {
return err
}

ok, err := pdfcpu.ImportBookmarks(ctx, rd, replace)
if err != nil {
return err
Expand Down Expand Up @@ -221,11 +213,7 @@ func AddBookmarks(rs io.ReadSeeker, w io.Writer, bms []pdfcpu.Bookmark, replace
return err
}

if err = WriteContext(ctx, w); err != nil {
return err
}

return nil
return WriteContext(ctx, w)
}

// AddBookmarksFile adds outlines to the PDF context read from inFile and writes the result to outFile.
Expand Down

0 comments on commit 5b7d844

Please sign in to comment.