Skip to content

Commit 5ae469c

Browse files
committed
wip
1 parent e5880c5 commit 5ae469c

File tree

15 files changed

+558
-124
lines changed

15 files changed

+558
-124
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Go Report Card](https://goreportcard.com/badge/github.com/hhrutter/pdfcpu)](https://goreportcard.com/report/github.com/hhrutter/pdfcpu)
55
[![License: MIT](https://img.shields.io/github/license/mashape/apistatus.svg)](https://opensource.org/licenses/MIT)
66

7-
![logo](pdfchip3.png)
7+
![logo](resources/pdfchip3.png)
88
# pdfcpu: a golang pdf processor
99

1010
Package pdfcpu is a simple PDF processing library written in [Go](http://golang.org).
@@ -25,14 +25,16 @@ I also wanted to have my own swiss army knife for PDFs written entirely in [Go](
2525
* Optimize (gets rid of redundancies like duplicate fonts, images)
2626
* Split (split a multi page PDF file into single page PDF files)
2727
* Merge (a set of PDF files into one consolidated PDF file)
28-
* Trim (generate a custom version of a PDF file)
2928
* Extract Images (extract all embedded images of a PDF file into a given dir)
3029
* Extract Fonts (extract all embedded fonts of a PDF file into a given dir)
3130
* Extract Pages (extract specific pages into a given dir)
3231
* Extract Content (extract the PDF-Source into given dir)
32+
* Trim (generate a custom version of a PDF file)
33+
* Encrypt
34+
* Decrypt
3335

3436
### Demo Screencast
35-
[![asciicast](demo.png)](https://asciinema.org/a/P5jaAo9kgZXKj2iSA1OqIdLAU)
37+
[![asciicast](resources/demo.png)](https://asciinema.org/a/P5jaAo9kgZXKj2iSA1OqIdLAU)
3638

3739
### Installation
3840
`go get github.com/hhrutter/pdfcpu/cmd/...`
@@ -52,6 +54,10 @@ I also wanted to have my own swiss army knife for PDFs written entirely in [Go](
5254

5355
pdfcpu trim [-verbose] -pages pageSelection [-upw userpw] [-opw ownerpw] inFile outFile
5456

57+
pdfcpu encrypt [-verbose] [-upw userpw] [-opw ownerpw] inFile [outFile]
58+
59+
pdfcpu decrypt [-verbose] [-upw userpw] [-opw ownerpw] inFile [outFile]
60+
5561
[Please read the documentation ](https://godoc.org/github.com/hhrutter/pdfcpu)
5662

5763

cmd/pdfcpu/main.go

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ The commands are:
3131
optimize optimize PDF by getting rid of redundant page resources
3232
split split multi-page PDF into several single-page PDFs
3333
merge concatenate 2 or more PDFs
34-
extract extract images, fonts, content, pages out of a PDF
35-
trim create trimmed version of a PDF
36-
version print pdfcpu version
34+
extract extract images, fonts, content or pages
35+
trim create trimmed version
36+
encrypt set password protection
37+
decrypt remove password protection
38+
changeupw change user password
39+
changeopw change owner password
40+
version print version
3741
3842
Single-letter Unix-style supported for commands and flags.
3943
@@ -62,7 +66,7 @@ verbose ... extensive log output
6266
upw ... user password
6367
opw ... owner password
6468
inFile ... input pdf file
65-
outFile ... output pdf file (default: inputPdfFile-opt.pdf)`
69+
outFile ... output pdf file (default: inFile-new.pdf)`
6670

6771
usageSplit = "usage: pdfcpu split [-verbose] [-upw userpw] [-opw ownerpw] inFile outDir"
6872
usageLongSplit = `Split generates a set of single page PDFs for the input file in outDir.
@@ -126,6 +130,24 @@ Valid pageSelections e.g. -3,5,7- or 4-7,!6 or 1-,!5
126130
127131
A missing pageSelection means all pages are selected for generation.`
128132

133+
usageEncrypt = "usage: pdfcpu encrypt [-verbose] [-upw userpw] [-opw ownerpw] inFile [outFile]"
134+
usageLongEncrypt = `Encrypt sets password protection based on user and owner password.
135+
136+
verbose ... extensive log output
137+
upw ... user password
138+
opw ... owner password
139+
inFile ... input pdf file
140+
outFile ... output pdf file (default: inFile-new.pdf)`
141+
142+
usageDecrypt = "usage: pdfcpu decrypt [-verbose] [-upw userpw] [-opw ownerpw] inFile [outFile]"
143+
usageLongDecrypt = `Decrypt removes the password protection.
144+
145+
verbose ... extensive log output
146+
upw ... user password
147+
opw ... owner password
148+
inFile ... input pdf file
149+
outFile ... output pdf file (default: inFile-new.pdf)`
150+
129151
usageVersion = "usage: pdfcpu version"
130152
usageLongVersion = "Version prints the pdfcpu version"
131153
)
@@ -209,6 +231,12 @@ func help() {
209231
case "trim":
210232
fmt.Printf("%s\n\n%s\n\n%s\n", usageTrim, usageLongTrim, usagePageSelection)
211233

234+
case "encrypt":
235+
fmt.Printf("%s\n\n%s\n", usageEncrypt, usageLongEncrypt)
236+
237+
case "decrypt":
238+
fmt.Printf("%s\n\n%s\n", usageDecrypt, usageLongDecrypt)
239+
212240
case "version":
213241
fmt.Printf("%s\n\n%s\n", usageVersion, usageLongVersion)
214242

@@ -396,6 +424,77 @@ func main() {
396424

397425
cmd = pdfcpu.TrimCommand(filenameIn, filenameOut, pages, config)
398426

427+
case "decrypt", "d", "dec":
428+
429+
d := true
430+
config.Decrypt = &d
431+
432+
if len(flag.Args()) == 0 || len(flag.Args()) > 2 || pageSelection != "" {
433+
fmt.Printf("%s\n\n", usageDecrypt)
434+
return
435+
}
436+
437+
filenameIn := flag.Arg(0)
438+
ensurePdfExtension(filenameIn)
439+
440+
filenameOut := defaultFilenameOut(filenameIn)
441+
if len(flag.Args()) == 2 {
442+
filenameOut = flag.Arg(1)
443+
ensurePdfExtension(filenameOut)
444+
}
445+
446+
// Always write using 0x0A end of line sequence.
447+
cmd = pdfcpu.OptimizeCommand(filenameIn, filenameOut, config)
448+
449+
case "encrypt", "enc":
450+
451+
d := false
452+
config.Decrypt = &d
453+
454+
if len(flag.Args()) == 0 || len(flag.Args()) > 2 || pageSelection != "" {
455+
fmt.Printf("%s\n\n", usageEncrypt)
456+
return
457+
}
458+
459+
filenameIn := flag.Arg(0)
460+
ensurePdfExtension(filenameIn)
461+
462+
filenameOut := defaultFilenameOut(filenameIn)
463+
if len(flag.Args()) == 2 {
464+
filenameOut = flag.Arg(1)
465+
ensurePdfExtension(filenameOut)
466+
}
467+
468+
cmd = pdfcpu.OptimizeCommand(filenameIn, filenameOut, config)
469+
470+
case "changeupw":
471+
if len(flag.Args()) != 3 {
472+
fmt.Println("changeupw needs 3 args")
473+
return
474+
}
475+
config.UserPW = flag.Arg(1)
476+
s := flag.Arg(2)
477+
config.UserPWNew = &s
478+
fmt.Printf("changeupw: old=%s new=%s\n", config.UserPW, *config.UserPWNew)
479+
480+
filenameIn := flag.Arg(0)
481+
ensurePdfExtension(filenameIn)
482+
cmd = pdfcpu.OptimizeCommand(filenameIn, defaultFilenameOut(filenameIn), config)
483+
484+
case "changeopw":
485+
if len(flag.Args()) != 3 {
486+
fmt.Println("changeopw needs 3 args")
487+
return
488+
}
489+
config.OwnerPW = flag.Arg(1)
490+
s := flag.Arg(2)
491+
config.OwnerPWNew = &s
492+
fmt.Printf("changeopw: old=%s new=%s\n", config.UserPW, *config.OwnerPWNew)
493+
494+
filenameIn := flag.Arg(0)
495+
ensurePdfExtension(filenameIn)
496+
cmd = pdfcpu.OptimizeCommand(filenameIn, defaultFilenameOut(filenameIn), config)
497+
399498
case "version":
400499

401500
if len(flag.Args()) != 0 {

0 commit comments

Comments
 (0)