Skip to content

Commit

Permalink
PDF 2.0 safe guard, fix #740, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Dec 10, 2023
1 parent dce5085 commit e3358c4
Show file tree
Hide file tree
Showing 28 changed files with 417 additions and 331 deletions.
11 changes: 8 additions & 3 deletions README.md
Expand Up @@ -14,6 +14,8 @@
pdfcpu is a PDF processing library written in [Go](http://golang.org) supporting encryption.
It provides both an API and a CLI. Supported are all versions up to PDF 1.7 (ISO-32000).

Support for PDF 2.0 is basic and ongoing work.

## Motivation

This is an effort to build a comprehensive PDF processing library from the ground up written in Go. Over time pdfcpu aims to support the standard range of PDF processing features and also any interesting use cases that may present themselves along the way.
Expand Down Expand Up @@ -68,6 +70,8 @@ The main focus lies on strong support for batch processing and scripting via a r
* [ndown](https://pdfcpu.io/generate/ndown)
* [nup](https://pdfcpu.io/generate/nup)
* [optimize](https://pdfcpu.io/core/optimize)
* [pagelayout](https://pdfcpu.io/pagelayout/pagelayout)
* [pagemode](https://pdfcpu.io/pagemode/pagemode)
* [pages](https://pdfcpu.io/pages/pages)
* [permissions](https://pdfcpu.io/encrypt/perm_add)
* [portfolio](https://pdfcpu.io/portfolio/portfolio)
Expand All @@ -78,8 +82,9 @@ The main focus lies on strong support for batch processing and scripting via a r
* [split](https://pdfcpu.io/core/split)
* [stamp](https://pdfcpu.io/core/stamp)
* [trim](https://pdfcpu.io/core/trim)
* [validate](https://pdfcpu.io/core/validate)
* [watermark](https://pdfcpu.io/core/watermark)
* [validate](https://pdfcpu.io/core/validate) 👉 now including rudimentory support for PDF 2.0
* [viewerpref](https://pdfcpu.io/viewerpref/viewerpref)
* [watermark](https://pdfcpu.io/core/watermark)

## Documentation

Expand Down Expand Up @@ -187,7 +192,7 @@ If processing your PDF with pdfcpu crashes during validation and can be opened b

## Contributors

Thanks goes to these wonderful people:
Thanks 💚 goes to these wonderful people:

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
||||||||
Expand Down
74 changes: 39 additions & 35 deletions cmd/pdfcpu/process.go
Expand Up @@ -231,7 +231,7 @@ func processOptimizeCommand(conf *model.Configuration) {
process(cli.OptimizeCommand(inFile, outFile, conf))
}

func processSplitVyPageNumberCommand(inFile, outDir string, conf *model.Configuration) {
func processSplitByPageNumberCommand(inFile, outDir string, conf *model.Configuration) {
if len(flag.Args()) == 2 {
fmt.Fprintln(os.Stderr, "split: missing page numbers")
os.Exit(1)
Expand Down Expand Up @@ -274,7 +274,7 @@ func processSplitCommand(conf *model.Configuration) {
outDir := flag.Arg(1)

if mode == "page" {
processSplitVyPageNumberCommand(inFile, outDir, conf)
processSplitByPageNumberCommand(inFile, outDir, conf)
return
}

Expand Down Expand Up @@ -666,24 +666,7 @@ func isHex(s string) bool {
return err == nil
}

func processSetPermissionsCommand(conf *model.Configuration) {
if perm != "" {
perm = permCompletion(perm)
}
if len(flag.Args()) != 1 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usagePermSet)
os.Exit(1)
}
if perm != "" && perm != "none" && perm != "print" && perm != "all" && !isBinary(perm) && !isHex(perm) {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usagePermSet)
os.Exit(1)
}

inFile := flag.Arg(0)
if conf.CheckFileNameExt {
ensurePDFExtension(inFile)
}

func configPerm(perm string, conf *model.Configuration) {
if perm != "" {
switch perm {
case "none":
Expand All @@ -702,6 +685,27 @@ func processSetPermissionsCommand(conf *model.Configuration) {
conf.Permissions = model.PermissionFlags(p)
}
}
}

func processSetPermissionsCommand(conf *model.Configuration) {
if perm != "" {
perm = permCompletion(perm)
}
if len(flag.Args()) != 1 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usagePermSet)
os.Exit(1)
}
if perm != "" && perm != "none" && perm != "print" && perm != "all" && !isBinary(perm) && !isHex(perm) {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usagePermSet)
os.Exit(1)
}

inFile := flag.Arg(0)
if conf.CheckFileNameExt {
ensurePDFExtension(inFile)
}

configPerm(perm, conf)

process(cli.SetPermissionsCommand(inFile, "", conf))
}
Expand Down Expand Up @@ -871,7 +875,7 @@ func addWatermarks(conf *model.Configuration, onTop bool) {
}

if len(flag.Args()) < 3 || len(flag.Args()) > 4 {
fmt.Fprintf(os.Stderr, "%s\n\n", u)
fmt.Fprintf(os.Stderr, "usage: %s\n\n", u)
os.Exit(1)
}

Expand Down Expand Up @@ -934,9 +938,9 @@ func processAddWatermarksCommand(conf *model.Configuration) {
}

func updateWatermarks(conf *model.Configuration, onTop bool) {
u := usageWatermarkAdd
u := usageWatermarkUpdate
if onTop {
u = usageStampAdd
u = usageStampUpdate
}

if len(flag.Args()) < 3 || len(flag.Args()) > 4 {
Expand Down Expand Up @@ -1107,7 +1111,7 @@ func processImportImagesCommand(conf *model.Configuration) {

func processInsertPagesCommand(conf *model.Configuration) {
if len(flag.Args()) == 0 || len(flag.Args()) > 2 {
fmt.Fprintf(os.Stderr, "%s\n\n", usagePagesInsert)
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usagePagesInsert)
os.Exit(1)
}

Expand Down Expand Up @@ -1138,7 +1142,7 @@ func processInsertPagesCommand(conf *model.Configuration) {

func processRemovePagesCommand(conf *model.Configuration) {
if len(flag.Args()) == 0 || len(flag.Args()) > 2 || selectedPages == "" {
fmt.Fprintf(os.Stderr, "%s\n\n", usagePagesRemove)
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usagePagesRemove)
os.Exit(1)
}

Expand Down Expand Up @@ -1529,7 +1533,7 @@ func processRemoveKeywordsCommand(conf *model.Configuration) {

func processListPropertiesCommand(conf *model.Configuration) {
if len(flag.Args()) != 1 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageKeywordsList)
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePropertiesList)
os.Exit(1)
}

Expand Down Expand Up @@ -1903,7 +1907,7 @@ func processDumpCommand(conf *model.Configuration) {

func processCreateCommand(conf *model.Configuration) {
if len(flag.Args()) <= 1 || len(flag.Args()) > 3 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n\n", usageCreate)
fmt.Fprintf(os.Stderr, "%s\n\n", usageCreate)
os.Exit(1)
}

Expand Down Expand Up @@ -2166,7 +2170,7 @@ func processMultiFillFormCommand(conf *model.Configuration) {

func processResizeCommand(conf *model.Configuration) {
if len(flag.Args()) < 2 || len(flag.Args()) > 3 {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageResize)
fmt.Fprintf(os.Stderr, "%s\n", usageResize)
os.Exit(1)
}

Expand Down Expand Up @@ -2200,7 +2204,7 @@ func processResizeCommand(conf *model.Configuration) {

func processPosterCommand(conf *model.Configuration) {
if len(flag.Args()) < 3 || len(flag.Args()) > 4 {
fmt.Fprintf(os.Stderr, "usage: %s\n", usagePoster)
fmt.Fprintf(os.Stderr, "%s\n", usagePoster)
os.Exit(1)
}

Expand Down Expand Up @@ -2236,7 +2240,7 @@ func processPosterCommand(conf *model.Configuration) {

func processNDownCommand(conf *model.Configuration) {
if len(flag.Args()) < 3 || len(flag.Args()) > 5 {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageNDown)
fmt.Fprintf(os.Stderr, "%s\n", usageNDown)
os.Exit(1)
}

Expand Down Expand Up @@ -2305,7 +2309,7 @@ func processNDownCommand(conf *model.Configuration) {

func processCutCommand(conf *model.Configuration) {
if len(flag.Args()) < 3 || len(flag.Args()) > 4 {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageCut)
fmt.Fprintf(os.Stderr, "%s\n", usageCut)
os.Exit(1)
}

Expand Down Expand Up @@ -2491,7 +2495,7 @@ func processSetPageModeCommand(conf *model.Configuration) {
v := flag.Arg(1)

if !validate.DocumentPageMode(v) {
fmt.Fprintln(os.Stderr, "invalid page mode, use one of: UseNone, UseThumb, FullScreen, UseOC, UseAttachments")
fmt.Fprintln(os.Stderr, "invalid page mode, use one of: UseNone, UseOutlines, UseThumbs, FullScreen, UseOC, UseAttachments")
os.Exit(1)
}

Expand All @@ -2513,7 +2517,7 @@ func processResetPageModeCommand(conf *model.Configuration) {

func processListViewerPreferencesCommand(conf *model.Configuration) {
if len(flag.Args()) != 1 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageViewerPreferences)
fmt.Fprintf(os.Stderr, "usage: %s\n", usageViewerPreferencesList)
os.Exit(1)
}

Expand All @@ -2526,7 +2530,7 @@ func processListViewerPreferencesCommand(conf *model.Configuration) {

func processSetViewerPreferencesCommand(conf *model.Configuration) {
if len(flag.Args()) != 2 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageViewerPreferences)
fmt.Fprintf(os.Stderr, "usage: %s\n", usageViewerPreferencesSet)
os.Exit(1)
}

Expand All @@ -2549,7 +2553,7 @@ func processSetViewerPreferencesCommand(conf *model.Configuration) {

func processResetViewerPreferencesCommand(conf *model.Configuration) {
if len(flag.Args()) != 1 || selectedPages != "" {
fmt.Fprintf(os.Stderr, "usage: %s\n", usageViewerPreferences)
fmt.Fprintf(os.Stderr, "usage: %s\n", usageViewerPreferencesReset)
os.Exit(1)
}

Expand Down

1 comment on commit e3358c4

@falco467
Copy link

@falco467 falco467 commented on e3358c4 Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error-handling on [pkg/api/merge.go] is broken now. The code will always return "file already closed" and swallow original errors, because both branches of the if-statement are executed.

if err != nil {
	if err1 := f.Close(); err1 != nil {
		return
	}
	os.Remove(outFile)
	// missing return after this line ?
}
if err = f.Close(); err != nil {
	return
}

Please sign in to comment.