Documentation ¶
Overview ¶
Package pdfcpu is a simple PDF processing library written in Go supporting encryption. It provides both an API and a command line tool. Supported are all versions up to PDF 1.7 (ISO-32000).
The available commands are:
validate validate PDF against PDF 32000-1:2008 (PDF 1.7) optimize optimize PDF by getting rid of redundant page resources split split multi-page PDF into several single-page PDFs merge concatenate 2 or more PDFs extract extract images, fonts, content or pages trim create trimmed version attach list, add, remove, extract embedded file attachments perm list, add user access permissions encrypt set password protection decrypt remove password protection changeupw change user password changeopw change owner password version print version
Index ¶
- func AddAttachments(fileIn string, files []string, config *types.Configuration) (err error)
- func AddPermissions(fileIn string, config *types.Configuration) (err error)
- func ChangeOwnerPassword(fileIn, fileOut string, config *types.Configuration, pwOld, pwNew *string) (err error)
- func ChangeUserPassword(fileIn, fileOut string, config *types.Configuration, pwOld, pwNew *string) (err error)
- func Decrypt(fileIn, fileOut string, config *types.Configuration) (err error)
- func Encrypt(fileIn, fileOut string, config *types.Configuration) (err error)
- func ExtractAttachments(fileIn, dirOut string, files []string, config *types.Configuration) (err error)
- func ExtractContent(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
- func ExtractFonts(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
- func ExtractImages(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
- func ExtractPages(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
- func ListAttachments(fileIn string, config *types.Configuration) (list []string, err error)
- func ListPermissions(fileIn string, config *types.Configuration) (list []string, err error)
- func Merge(filesIn []string, fileOut string, config *types.Configuration) (err error)
- func Optimize(fileIn, fileOut string, config *types.Configuration) (err error)
- func ParsePageSelection(s string) (ps []string, err error)
- func Process(cmd *Command) (out []string, err error)
- func Read(fileIn string, config *types.Configuration) (ctx *types.PDFContext, err error)
- func RemoveAttachments(fileIn string, files []string, config *types.Configuration) (err error)
- func Split(fileIn, dirOut string, config *types.Configuration) (err error)
- func Trim(fileIn, fileOut string, pageSelection []string, config *types.Configuration) (err error)
- func Validate(fileIn string, config *types.Configuration) (err error)
- func Verbose(verbose bool)
- func Write(ctx *types.PDFContext) (err error)
- type Command
- func AddAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *types.Configuration) Command
- func AddPermissionsCommand(pdfFileNameIn string, config *types.Configuration) Command
- func ChangeOwnerPWCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration, ...) Command
- func ChangeUserPWCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration, ...) Command
- func DecryptCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command
- func EncryptCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command
- func ExtractAttachmentsCommand(pdfFileNameIn, dirNameOut string, fileNamesIn []string, ...) Command
- func ExtractContentCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, ...) Command
- func ExtractFontsCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, ...) Command
- func ExtractImagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, ...) Command
- func ExtractPagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, ...) Command
- func ListAttachmentsCommand(pdfFileNameIn string, config *types.Configuration) Command
- func ListPermissionsCommand(pdfFileNameIn string, config *types.Configuration) Command
- func MergeCommand(pdfFileNamesIn []string, pdfFileNameOut string, config *types.Configuration) Command
- func OptimizeCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command
- func RemoveAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *types.Configuration) Command
- func SplitCommand(pdfFileNameIn, dirNameOut string, config *types.Configuration) Command
- func TrimCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, ...) Command
- func ValidateCommand(pdfFileName string, config *types.Configuration) Command
Examples ¶
- Process (AddAttachments)
- Process (AddPermissions)
- Process (ChangeOwnerPW)
- Process (ChangeUserPW)
- Process (Decrypt)
- Process (Encrypt)
- Process (ExtractAttachments)
- Process (ExtractImages)
- Process (ExtractPages)
- Process (ListAttachments)
- Process (ListPermissions)
- Process (Merge)
- Process (Optimize)
- Process (RemoveAttachments)
- Process (Split)
- Process (Trim)
- Process (Validate)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddAttachments ¶ added in v0.1.3
func AddAttachments(fileIn string, files []string, config *types.Configuration) (err error)
AddAttachments embeds files into a PDF.
func AddPermissions ¶ added in v0.1.6
func AddPermissions(fileIn string, config *types.Configuration) (err error)
AddPermissions sets the user access permissions.
func ChangeOwnerPassword ¶ added in v0.1.1
func ChangeOwnerPassword(fileIn, fileOut string, config *types.Configuration, pwOld, pwNew *string) (err error)
ChangeOwnerPassword of fileIn and write result to fileOut.
func ChangeUserPassword ¶ added in v0.1.1
func ChangeUserPassword(fileIn, fileOut string, config *types.Configuration, pwOld, pwNew *string) (err error)
ChangeUserPassword of fileIn and write result to fileOut.
func Decrypt ¶ added in v0.1.1
func Decrypt(fileIn, fileOut string, config *types.Configuration) (err error)
Decrypt fileIn and write result to fileOut.
func Encrypt ¶ added in v0.1.1
func Encrypt(fileIn, fileOut string, config *types.Configuration) (err error)
Encrypt fileIn and write result to fileOut.
func ExtractAttachments ¶ added in v0.1.3
func ExtractAttachments(fileIn, dirOut string, files []string, config *types.Configuration) (err error)
ExtractAttachments extracts embedded files from a PDF.
func ExtractContent ¶
func ExtractContent(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
ExtractContent dumps "PDF source" files from fileIn into dirOut for selected pages.
func ExtractFonts ¶
func ExtractFonts(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
ExtractFonts dumps embedded fontfiles from fileIn into dirOut for selected pages.
func ExtractImages ¶
func ExtractImages(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
ExtractImages dumps embedded image resources from fileIn into dirOut for selected pages.
func ExtractPages ¶
func ExtractPages(fileIn, dirOut string, pageSelection []string, config *types.Configuration) (err error)
ExtractPages generates single page PDF files from fileIn in dirOut for selected pages.
func ListAttachments ¶ added in v0.1.3
func ListAttachments(fileIn string, config *types.Configuration) (list []string, err error)
ListAttachments returns a list of embedded file attachments.
func ListPermissions ¶ added in v0.1.6
func ListPermissions(fileIn string, config *types.Configuration) (list []string, err error)
ListPermissions returns a list of user access permissions.
func Merge ¶
func Merge(filesIn []string, fileOut string, config *types.Configuration) (err error)
Merge some PDF files together and write the result to fileOut. This corresponds to concatenating these files in the order specified by filesIn. The first entry of filesIn serves as the destination xRefTable where all the remaining files gets merged into.
func Optimize ¶
func Optimize(fileIn, fileOut string, config *types.Configuration) (err error)
Optimize reads in fileIn, does validation, optimization and writes the result to fileOut.
func ParsePageSelection ¶
ParsePageSelection ensures a correct page selection expression.
func Process ¶
Process executes a pdfcpu command.
Example (AddAttachments) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" cmd := AddAttachmentsCommand("in.pdf", []string{"a.csv", "b.jpg", "c.pdf"}, config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (AddPermissions) ¶
config := types.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" config.UserAccessPermissions = types.PermissionsAll cmd := AddPermissionsCommand("in.pdf", config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (ChangeOwnerPW) ¶
config := types.NewDefaultConfiguration() // supply existing user pw like so config.UserPW = "upw" // old and new owner pw pwOld := "pwOld" pwNew := "pwNew" cmd := ChangeOwnerPWCommand("in.pdf", "out.pdf", config, &pwOld, &pwNew) _, err := Process(&cmd) if err != nil { return }
Output:
Example (ChangeUserPW) ¶
config := types.NewDefaultConfiguration() // supply existing owner pw like so config.OwnerPW = "opw" pwOld := "pwOld" pwNew := "pwNew" cmd := ChangeUserPWCommand("in.pdf", "out.pdf", config, &pwOld, &pwNew) _, err := Process(&cmd) if err != nil { return }
Output:
Example (Decrypt) ¶
config := types.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" cmd := DecryptCommand("in.pdf", "out.pdf", config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (Encrypt) ¶
config := types.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" cmd := EncryptCommand("in.pdf", "out.pdf", config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (ExtractAttachments) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Extract all attachments. cmd := ExtractAttachmentsCommand("in.pdf", "dirOut", nil, config) _, err := Process(&cmd) if err != nil { return } // Extract specific attachments. cmd = ExtractAttachmentsCommand("in.pdf", "dirOut", []string{"a.csv", "b.pdf"}, config) _, err = Process(&cmd) if err != nil { return }
Output:
Example (ExtractImages) ¶
// Extract all embedded images for first 5 and last 5 pages but not for page 4. selectedPages := []string{"-5", "5-", "!4"} config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" cmd := ExtractImagesCommand("in.pdf", "dirOut", selectedPages, config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (ExtractPages) ¶
// Extract single-page PDFs for pages 3, 4 and 5. selectedPages := []string{"3..5"} config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" cmd := ExtractPagesCommand("in.pdf", "dirOut", selectedPages, config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (ListAttachments) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = opw" cmd := ListAttachmentsCommand("in.pdf", config) list, err := Process(&cmd) if err != nil { return } // Print attachment list. for _, l := range list { fmt.Println(l) }
Output:
Example (ListPermissions) ¶
config := types.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" cmd := ListPermissionsCommand("in.pdf", config) list, err := Process(&cmd) if err != nil { return } // Print permissions list. for _, l := range list { fmt.Println(l) }
Output:
Example (Merge) ¶
// Concatenate this sequence of PDF files: filenamesIn := []string{"in1.pdf", "in2.pdf", "in3.pdf"} cmd := MergeCommand(filenamesIn, "out.pdf", types.NewDefaultConfiguration()) _, err := Process(&cmd) if err != nil { return }
Output:
Example (Optimize) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Generate optional stats. config.StatsFileName = "stats.csv" // Configure end of line sequence for writing. config.Eol = types.EolLF cmd := OptimizeCommand("in.pdf", "out.pdf", config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (RemoveAttachments) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Not to be confused with the ExtractAttachmentsCommand! // Remove all attachments. cmd := RemoveAttachmentsCommand("in.pdf", nil, config) _, err := Process(&cmd) if err != nil { return } // Remove specific attachments. cmd = RemoveAttachmentsCommand("in.pdf", []string{"a.csv", "b.jpg"}, config) _, err = Process(&cmd) if err != nil { return }
Output:
Example (Split) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Split into single-page PDFs. cmd := SplitCommand("in.pdf", "outDir", config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (Trim) ¶
// Trim to first three pages. selectedPages := []string{"-3"} config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" cmd := TrimCommand("in.pdf", "out.pdf", selectedPages, config) _, err := Process(&cmd) if err != nil { return }
Output:
Example (Validate) ¶
config := types.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Set relaxed validation mode. config.SetValidationRelaxed() cmd := ValidateCommand("in.pdf", config) _, err := Process(&cmd) if err != nil { return }
Output:
func Read ¶
func Read(fileIn string, config *types.Configuration) (ctx *types.PDFContext, err error)
Read reads in a PDF file and builds an internal structure holding its cross reference table aka the PDFContext.
func RemoveAttachments ¶ added in v0.1.3
func RemoveAttachments(fileIn string, files []string, config *types.Configuration) (err error)
RemoveAttachments deletes embedded files from a PDF.
func Split ¶
func Split(fileIn, dirOut string, config *types.Configuration) (err error)
Split generates a sequence of single page PDF files in dirOut creating one file for every page of inFile.
func Trim ¶
func Trim(fileIn, fileOut string, pageSelection []string, config *types.Configuration) (err error)
Trim generates a trimmed version of fileIn containing all pages selected.
func Validate ¶
func Validate(fileIn string, config *types.Configuration) (err error)
Validate validates a PDF file against ISO-32000-1:2008.
func Write ¶
func Write(ctx *types.PDFContext) (err error)
Write generates a PDF file for a given PDFContext.
Types ¶
type Command ¶
type Command struct { Mode types.CommandMode // VALIDATE OPTIMIZE SPLIT MERGE EXTRACT TRIM LISTATT ADDATT REMATT EXTATT ENCRYPT DECRYPT CHANGEUPW CHANGEOPW LISTP ADDP InFile *string // * * * - * * * * * * * * * * * * InFiles []string // - - - * - - - * * * - - - - - - InDir *string // - - - - - - - - - - - - - - - - OutFile *string // - * - * - * - - - - * * * * - - OutDir *string // - - * - * - - - - * - - - - - - PageSelection []string // - - - - * * - - - - - - - - - - Config *types.Configuration // * * * * * * * * * * * * * * * * PWOld *string // - - - - - - - - - - - - * * - - PWNew *string // - - - - - - - - - - - - * * - - }
Command represents an execution context.
func AddAttachmentsCommand ¶ added in v0.1.3
func AddAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *types.Configuration) Command
AddAttachmentsCommand creates a new AddAttachmentsCommand.
func AddPermissionsCommand ¶ added in v0.1.6
func AddPermissionsCommand(pdfFileNameIn string, config *types.Configuration) Command
AddPermissionsCommand creates a new AddPermissionsCommand.
func ChangeOwnerPWCommand ¶ added in v0.1.1
func ChangeOwnerPWCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration, pwOld, pwNew *string) Command
ChangeOwnerPWCommand creates a new ChangeOwnerPWCommand.
func ChangeUserPWCommand ¶ added in v0.1.1
func ChangeUserPWCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration, pwOld, pwNew *string) Command
ChangeUserPWCommand creates a new ChangeUserPWCommand.
func DecryptCommand ¶ added in v0.1.1
func DecryptCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command
DecryptCommand creates a new DecryptCommand.
func EncryptCommand ¶ added in v0.1.1
func EncryptCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command
EncryptCommand creates a new EncryptCommand.
func ExtractAttachmentsCommand ¶ added in v0.1.3
func ExtractAttachmentsCommand(pdfFileNameIn, dirNameOut string, fileNamesIn []string, config *types.Configuration) Command
ExtractAttachmentsCommand creates a new ExtractAttachmentsCommand.
func ExtractContentCommand ¶
func ExtractContentCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command
ExtractContentCommand creates a new ExtractContentCommand.
func ExtractFontsCommand ¶
func ExtractFontsCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command
ExtractFontsCommand creates a new ExtractFontsCommand. (experimental)
func ExtractImagesCommand ¶
func ExtractImagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command
ExtractImagesCommand creates a new ExtractImagesCommand. (experimental)
func ExtractPagesCommand ¶
func ExtractPagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *types.Configuration) Command
ExtractPagesCommand creates a new ExtractPagesCommand.
func ListAttachmentsCommand ¶ added in v0.1.3
func ListAttachmentsCommand(pdfFileNameIn string, config *types.Configuration) Command
ListAttachmentsCommand create a new ListAttachmentsCommand.
func ListPermissionsCommand ¶ added in v0.1.6
func ListPermissionsCommand(pdfFileNameIn string, config *types.Configuration) Command
ListPermissionsCommand create a new ListPermissionsCommand.
func MergeCommand ¶
func MergeCommand(pdfFileNamesIn []string, pdfFileNameOut string, config *types.Configuration) Command
MergeCommand creates a new MergeCommand.
func OptimizeCommand ¶
func OptimizeCommand(pdfFileNameIn, pdfFileNameOut string, config *types.Configuration) Command
OptimizeCommand creates a new OptimizeCommand.
func RemoveAttachmentsCommand ¶ added in v0.1.3
func RemoveAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *types.Configuration) Command
RemoveAttachmentsCommand creates a new RemoveAttachmentsCommand.
func SplitCommand ¶
func SplitCommand(pdfFileNameIn, dirNameOut string, config *types.Configuration) Command
SplitCommand creates a new SplitCommand.
func TrimCommand ¶
func TrimCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, config *types.Configuration) Command
TrimCommand creates a new TrimCommand.
func ValidateCommand ¶
func ValidateCommand(pdfFileName string, config *types.Configuration) Command
ValidateCommand creates a new ValidateCommand.
Directories ¶
Path | Synopsis |
---|---|
Package attach provides management code for file attachments / embedded files.
|
Package attach provides management code for file attachments / embedded files. |
cmd
|
|
Package crypto contains PDF encryption code.
|
Package crypto contains PDF encryption code. |
Package extract provides functions for extracting fonts, images, pages and page content.
|
Package extract provides functions for extracting fonts, images, pages and page content. |
Package filter contains PDF filter implementations.
|
Package filter contains PDF filter implementations. |
Package merge provides for the merging of two PDFContexts.
|
Package merge provides for the merging of two PDFContexts. |
Package optimize contains code for optimizing the resources of a PDF file.
|
Package optimize contains code for optimizing the resources of a PDF file. |
Package read provides for parsing a PDF file into memory.
|
Package read provides for parsing a PDF file into memory. |
Package types provides the PDFContext, representing an ecosystem for PDF processing.
|
Package types provides the PDFContext, representing an ecosystem for PDF processing. |
Package validate contains validation code for ISO 32000-1:2008.
|
Package validate contains validation code for ISO 32000-1:2008. |
Package write renders a PDF cross reference table to a PDF file.
|
Package write renders a PDF cross reference table to a PDF file. |