Skip to content

Commit 694f81f

Browse files
committed
Fix #794 , add PDF 2.0 disclaimer
1 parent 032b32d commit 694f81f

File tree

19 files changed

+169
-124
lines changed

19 files changed

+169
-124
lines changed

pkg/api/api.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ import (
4747
"github.com/pkg/errors"
4848
)
4949

50+
func logDisclaimerPDF20() {
51+
disclaimer := `
52+
***************************** Disclaimer ****************************
53+
* PDF 2.0 features are supported on a need basis. *
54+
* (See ISO 32000:2 6.3.2 Conformance of PDF processors) *
55+
* At the moment pdfcpu comes with basic PDF 2.0 support. *
56+
* Please let us know which feature you would like to see supported, *
57+
* provide a sample PDF file and create an issue: *
58+
* https://github.com/pdfcpu/pdfcpu/issues/new/choose *
59+
*********************************************************************`
60+
61+
if log.ValidateEnabled() {
62+
log.Validate.Println(disclaimer)
63+
}
64+
if log.CLIEnabled() {
65+
log.CLI.Println(disclaimer)
66+
}
67+
}
68+
5069
// ReadContext uses an io.ReadSeeker to build an internal structure holding its cross reference table aka the Context.
5170
func ReadContext(rs io.ReadSeeker, conf *model.Configuration) (*model.Context, error) {
5271
if rs == nil {
@@ -68,6 +87,10 @@ func ReadContextFile(inFile string) (*model.Context, error) {
6887
return nil, err
6988
}
7089

90+
if ctx.Version() == model.V20 {
91+
logDisclaimerPDF20()
92+
}
93+
7194
if err = validate.XRefTable(ctx.XRefTable); err != nil {
7295
return nil, err
7396
}
@@ -77,6 +100,11 @@ func ReadContextFile(inFile string) (*model.Context, error) {
77100

78101
// ValidateContext validates ctx.
79102
func ValidateContext(ctx *model.Context) error {
103+
104+
if ctx.Version() == model.V20 {
105+
logDisclaimerPDF20()
106+
}
107+
80108
return validate.XRefTable(ctx.XRefTable)
81109
}
82110

@@ -130,6 +158,10 @@ func readAndValidate(rs io.ReadSeeker, conf *model.Configuration, from1 time.Tim
130158

131159
from2 := time.Now()
132160

161+
if ctx.Version() == model.V20 {
162+
logDisclaimerPDF20()
163+
}
164+
133165
if err = validate.XRefTable(ctx.XRefTable); err != nil {
134166
return nil, 0, 0, err
135167
}
@@ -146,10 +178,6 @@ func ReadValidateAndOptimize(rs io.ReadSeeker, conf *model.Configuration, from1
146178
return nil, 0, 0, 0, err
147179
}
148180

149-
if ctx.Version() == model.V20 {
150-
return nil, 0, 0, 0, pdfcpu.ErrUnsupportedVersion
151-
}
152-
153181
from3 := time.Now()
154182
if err = OptimizeContext(ctx); err != nil {
155183
return nil, 0, 0, 0, err

pkg/api/booklet.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ func Booklet(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nu
8888
return err
8989
}
9090

91-
if ctx.Version() == model.V20 {
92-
return pdfcpu.ErrUnsupportedVersion
93-
}
94-
9591
if err := ctx.EnsurePageCount(); err != nil {
9692
return err
9793
}

pkg/api/extract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func ExtractPages(rs io.ReadSeeker, outDir, fileName string, selectedPages []str
264264

265265
if len(pages) == 0 {
266266
if log.CLIEnabled() {
267-
log.CLI.Println("aborted: nothing to extract!")
267+
log.CLI.Println("aborted: missing page numbers!")
268268
}
269269
return nil
270270
}

pkg/api/merge.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func appendTo(rs io.ReadSeeker, fName string, ctxDest *model.Context, dividerPag
3636
return err
3737
}
3838

39-
if ctxSource.Version() == model.V20 {
39+
if ctxDest.Version() < model.V20 && ctxSource.Version() == model.V20 {
4040
return pdfcpu.ErrUnsupportedVersion
4141
}
4242

@@ -93,7 +93,9 @@ func prepDestContext(destFile string, rs io.ReadSeeker, conf *model.Configuratio
9393
}
9494
}
9595

96-
ctxDest.EnsureVersionForWriting()
96+
if ctxDest.Version() < model.V20 {
97+
ctxDest.EnsureVersionForWriting()
98+
}
9799

98100
return ctxDest, nil
99101
}
@@ -135,9 +137,6 @@ func Merge(destFile string, inFiles []string, w io.Writer, conf *model.Configura
135137
if err != nil {
136138
return err
137139
}
138-
if ctxDest.Version() == model.V20 {
139-
return pdfcpu.ErrUnsupportedVersion
140-
}
141140

142141
for _, fName := range inFiles {
143142
if err := func() error {
@@ -181,6 +180,7 @@ func MergeCreateFile(inFiles []string, outFile string, dividerPage bool, conf *m
181180
return
182181
}
183182
os.Remove(outFile)
183+
return
184184
}
185185
if err = f.Close(); err != nil {
186186
return

pkg/api/nup.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ func NUp(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nup *m
119119
return err
120120
}
121121

122-
if ctx.Version() == model.V20 {
123-
return pdfcpu.ErrUnsupportedVersion
124-
}
125-
126122
if err := ctx.EnsurePageCount(); err != nil {
127123
return err
128124
}

pkg/api/page.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package api
1919
import (
2020
"io"
2121
"os"
22+
"sort"
2223
"time"
2324

2425
"github.com/pdfcpu/pdfcpu/pkg/log"
26+
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
2527
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
2628
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
2729
"github.com/pkg/errors"
@@ -134,7 +136,7 @@ func RemovePages(rs io.ReadSeeker, w io.Writer, selectedPages []string, conf *mo
134136
conf.Cmd = model.REMOVEPAGES
135137

136138
fromStart := time.Now()
137-
ctx, durRead, durVal, durOpt, err := ReadValidateAndOptimize(rs, conf, fromStart)
139+
ctx, _, _, _, err := ReadValidateAndOptimize(rs, conf, fromStart)
138140
if err != nil {
139141
return err
140142
}
@@ -143,31 +145,38 @@ func RemovePages(rs io.ReadSeeker, w io.Writer, selectedPages []string, conf *mo
143145
return err
144146
}
145147

146-
fromWrite := time.Now()
147-
148-
pages, err := PagesForPageSelection(ctx.PageCount, selectedPages, false, true)
148+
pages, err := RemainingPagesForPageRemoval(ctx.PageCount, selectedPages, true)
149149
if err != nil {
150150
return err
151151
}
152152

153-
// ctx.Pagecount gets set during validation.
154-
if len(pages) >= ctx.PageCount {
155-
return errors.New("pdfcpu: operation invalid")
153+
if len(pages) == 0 {
154+
if log.CLIEnabled() {
155+
log.CLI.Println("aborted: missing page numbers!")
156+
}
157+
return nil
156158
}
157159

158-
// No special context processing required.
159-
// WriteContext decides which pages get written by checking conf.Cmd
160+
var pageNrs []int
161+
for k, v := range pages {
162+
if v {
163+
pageNrs = append(pageNrs, k)
164+
}
165+
}
166+
sort.Ints(pageNrs)
160167

161-
ctx.Write.SelectedPages = pages
162-
if err = WriteContext(ctx, w); err != nil {
168+
ctxDest, err := pdfcpu.ExtractPages(ctx, pageNrs, false)
169+
if err != nil {
163170
return err
164171
}
165172

166-
durWrite := time.Since(fromWrite).Seconds()
167-
durTotal := time.Since(fromStart).Seconds()
168-
logOperationStats(ctx, "remove pages, write", durRead, durVal, durOpt, durWrite, durTotal)
173+
if conf.ValidationMode != model.ValidationNone {
174+
if err = ValidateContext(ctxDest); err != nil {
175+
return err
176+
}
177+
}
169178

170-
return nil
179+
return WriteContext(ctxDest, w)
171180
}
172181

173182
// RemovePagesFile removes selected inFile pages and writes the result to outFile..

pkg/api/pageLayout.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"os"
2222
"time"
2323

24-
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
2524
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
2625
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
2726
"github.com/pkg/errors"
@@ -113,10 +112,6 @@ func SetPageLayout(rs io.ReadSeeker, w io.Writer, val model.PageLayout, conf *mo
113112
return err
114113
}
115114

116-
if ctx.Version() == model.V20 {
117-
return pdfcpu.ErrUnsupportedVersion
118-
}
119-
120115
ctx.RootDict["PageLayout"] = types.Name(val.String())
121116

122117
if err = WriteContext(ctx, w); err != nil {
@@ -182,10 +177,6 @@ func ResetPageLayout(rs io.ReadSeeker, w io.Writer, conf *model.Configuration) e
182177
return err
183178
}
184179

185-
if ctx.Version() == model.V20 {
186-
return pdfcpu.ErrUnsupportedVersion
187-
}
188-
189180
delete(ctx.RootDict, "PageLayout")
190181

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

pkg/api/pageMode.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"os"
2222
"time"
2323

24-
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
2524
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
2625
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
2726
"github.com/pkg/errors"
@@ -113,10 +112,6 @@ func SetPageMode(rs io.ReadSeeker, w io.Writer, val model.PageMode, conf *model.
113112
return err
114113
}
115114

116-
if ctx.Version() == model.V20 {
117-
return pdfcpu.ErrUnsupportedVersion
118-
}
119-
120115
ctx.RootDict["PageMode"] = types.Name(val.String())
121116

122117
if err = WriteContext(ctx, w); err != nil {
@@ -182,10 +177,6 @@ func ResetPageMode(rs io.ReadSeeker, w io.Writer, conf *model.Configuration) err
182177
return err
183178
}
184179

185-
if ctx.Version() == model.V20 {
186-
return pdfcpu.ErrUnsupportedVersion
187-
}
188-
189180
delete(ctx.RootDict, "PageMode")
190181

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

pkg/api/permission.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"time"
2323

24+
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
2425
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
2526
"github.com/pkg/errors"
2627
)
@@ -68,6 +69,10 @@ func SetPermissions(rs io.ReadSeeker, w io.Writer, conf *model.Configuration) er
6869
return err
6970
}
7071

72+
if ctx.Version() == model.V20 {
73+
return pdfcpu.ErrUnsupportedVersion
74+
}
75+
7176
fromWrite := time.Now()
7277
if err = WriteContext(ctx, w); err != nil {
7378
return err
@@ -141,6 +146,11 @@ func GetPermissions(rs io.ReadSeeker, conf *model.Configuration) (*int16, error)
141146
if err != nil {
142147
return nil, err
143148
}
149+
150+
if ctx.Version() == model.V20 {
151+
return nil, pdfcpu.ErrUnsupportedVersion
152+
}
153+
144154
if ctx.E == nil {
145155
// Full access - permissions don't apply.
146156
return nil, nil

pkg/api/selectPages.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ func PagesForPageSelection(pageCount int, pageSelection []string, ensureAllforNo
391391
return m, nil
392392
}
393393

394+
func RemainingPagesForPageRemoval(pageCount int, pageSelection []string, log bool) (types.IntSet, error) {
395+
pagesToRemove, err := selectedPages(pageCount, pageSelection, log)
396+
if err != nil {
397+
return nil, err
398+
}
399+
400+
m := types.IntSet{}
401+
for i := 1; i <= pageCount; i++ {
402+
m[i] = true
403+
}
404+
405+
for k, v := range pagesToRemove {
406+
if v {
407+
m[k] = false
408+
}
409+
}
410+
411+
return m, nil
412+
}
413+
394414
func deletePageFromCollection(cp *[]int, p int) {
395415
a := []int{}
396416
for _, i := range *cp {

0 commit comments

Comments
 (0)