Documentation ¶
Overview ¶
Package api provides support for interacting with pdf.
Index ¶
- func AddAttachments(fileIn string, files []string, config *pdf.Configuration) error
- func AddPermissions(fileIn string, config *pdf.Configuration) error
- func AddWatermarks(cmd *Command) ([]string, error)
- func ChangeOwnerPassword(cmd *Command) ([]string, error)
- func ChangeUserPassword(cmd *Command) ([]string, error)
- func Decrypt(cmd *Command) ([]string, error)
- func Encrypt(cmd *Command) ([]string, error)
- func ExtractAttachments(fileIn, dirOut string, files []string, config *pdf.Configuration) error
- func ExtractContent(cmd *Command) ([]string, error)
- func ExtractFonts(cmd *Command) ([]string, error)
- func ExtractImages(cmd *Command) ([]string, error)
- func ExtractMetadata(cmd *Command) ([]string, error)
- func ExtractPages(cmd *Command) ([]string, error)
- func ImportImages(cmd *Command) ([]string, error)
- func ListAttachments(fileIn string, config *pdf.Configuration) ([]string, error)
- func ListPermissions(fileIn string, config *pdf.Configuration) ([]string, error)
- func Merge(cmd *Command) ([]string, error)
- func MergeContexts(rsc []pdf.ReadSeekerCloser, config *pdf.Configuration) (*pdf.Context, error)
- func NUp(cmd *Command) ([]string, error)
- func Optimize(cmd *Command) ([]string, error)
- func OptimizeContext(ctx *pdf.Context) error
- func ParsePageSelection(s string) ([]string, error)
- func Process(cmd *Command) (out []string, err error)
- func ReadContext(rs io.ReadSeeker, fileIn string, fileSize int64, config *pdf.Configuration) (*pdf.Context, error)
- func ReadContextFromFile(fileIn string, config *pdf.Configuration) (*pdf.Context, error)
- func RemoveAttachments(fileIn string, files []string, config *pdf.Configuration) error
- func Rotate(cmd *Command) ([]string, error)
- func Split(cmd *Command) ([]string, error)
- func Trim(cmd *Command) ([]string, error)
- func Validate(cmd *Command) ([]string, error)
- func ValidateContext(ctx *pdf.Context) error
- func Write(ctx *pdf.Context) error
- func WriteContext(ctx *pdf.Context, w io.Writer) error
- type Command
- func AddAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *pdf.Configuration) *Command
- func AddPermissionsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command
- func AddWatermarksCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, ...) *Command
- func ChangeOwnerPWCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration, ...) *Command
- func ChangeUserPWCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration, ...) *Command
- func DecryptCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command
- func EncryptCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.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 ExtractMetadataCommand(pdfFileNameIn, dirNameOut string, config *pdf.Configuration) *Command
- func ExtractPagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, ...) *Command
- func ImportImagesCommand(imageFileNamesIn []string, pdfFileNameOut string, imp *pdf.Import, ...) *Command
- func ListAttachmentsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command
- func ListPermissionsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command
- func MergeCommand(pdfFileNamesIn []string, pdfFileNameOut string, config *pdf.Configuration) *Command
- func NUpCommand(fileNamesIn []string, pdfFileNameOut string, pageSelection []string, ...) *Command
- func OptimizeCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command
- func RemoveAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *pdf.Configuration) *Command
- func RotateCommand(pdfFileNameIn string, rotation int, pageSelection []string, ...) *Command
- func SplitCommand(pdfFileNameIn, dirNameOut string, span int, config *pdf.Configuration) *Command
- func TrimCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, ...) *Command
- func ValidateCommand(pdfFileName string, config *pdf.Configuration) *Command
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddAttachments ¶
func AddAttachments(fileIn string, files []string, config *pdf.Configuration) error
AddAttachments embeds files into a PDF.
func AddPermissions ¶
func AddPermissions(fileIn string, config *pdf.Configuration) error
AddPermissions sets the user access permissions.
func AddWatermarks ¶ added in v0.1.16
AddWatermarks adds watermarks to all pages selected.
func ChangeOwnerPassword ¶
ChangeOwnerPassword of fileIn and write result to fileOut.
func ChangeUserPassword ¶
ChangeUserPassword of fileIn and write result to fileOut.
func ExtractAttachments ¶
func ExtractAttachments(fileIn, dirOut string, files []string, config *pdf.Configuration) error
ExtractAttachments extracts embedded files from a PDF.
func ExtractContent ¶
ExtractContent dumps "PDF source" files from fileIn into dirOut for selected pages.
func ExtractFonts ¶
ExtractFonts dumps embedded fontfiles from fileIn into dirOut for selected pages.
func ExtractImages ¶
ExtractImages dumps embedded image resources from fileIn into dirOut for selected pages.
func ExtractMetadata ¶ added in v0.1.16
ExtractMetadata dumps all metadata dict entries for fileIn into dirOut.
func ExtractPages ¶
ExtractPages generates single page PDF files from fileIn in dirOut for selected pages.
func ImportImages ¶ added in v0.1.20
ImportImages turns image files into a page sequence and writes the result to outFile. In its simplest form this operation converts an image into a PDF.
func ListAttachments ¶
func ListAttachments(fileIn string, config *pdf.Configuration) ([]string, error)
ListAttachments returns a list of embedded file attachments.
func ListPermissions ¶
func ListPermissions(fileIn string, config *pdf.Configuration) ([]string, error)
ListPermissions returns a list of user access permissions.
func Merge ¶
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 MergeContexts ¶ added in v0.1.18
func MergeContexts(rsc []pdf.ReadSeekerCloser, config *pdf.Configuration) (*pdf.Context, error)
MergeContexts merges a sequence of PDF's represented by a slice of ReadSeekerCloser.
func Optimize ¶
Optimize reads in fileIn, does validation, optimization and writes the result to fileOut.
func OptimizeContext ¶ added in v0.1.18
OptimizeContext optimizes a PDF context.
func ParsePageSelection ¶
ParsePageSelection ensures a correct page selection expression.
func Process ¶
Process executes a pdfcpu command.
Example ¶
package main import ( "fmt" "github.com/hhrutter/pdfcpu/pkg/pdfcpu" ) func main() { // Please refer to the following examples. } func exampleProcessValidate() { config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Set relaxed validation mode. config.ValidationMode = pdfcpu.ValidationRelaxed _, err := Process(ValidateCommand("in.pdf", config)) if err != nil { return } } func exampleProcessOptimize() { config := pdfcpu.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 = pdfcpu.EolLF _, err := Process(OptimizeCommand("in.pdf", "out.pdf", config)) if err != nil { return } } func exampleProcessMerge() { // Concatenate this sequence of PDF files: filenamesIn := []string{"in1.pdf", "in2.pdf", "in3.pdf"} _, err := Process(MergeCommand(filenamesIn, "out.pdf", pdfcpu.NewDefaultConfiguration())) if err != nil { return } } func exampleProcessSplit() { // Split into single-page PDFs. config := pdfcpu.NewDefaultConfiguration() _, err := Process(SplitCommand("in.pdf", "outDir", 1, config)) if err != nil { return } } func exampleProcessSplitWithSpan() { // Split into PDFs using a split span of 2 // Each generated file has 2 pages. config := pdfcpu.NewDefaultConfiguration() _, err := Process(SplitCommand("in.pdf", "outDir", 2, config)) if err != nil { return } } func exampleProcessTrim() { // Trim to first three pages. selectedPages := []string{"-3"} config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" _, err := Process(TrimCommand("in.pdf", "out.pdf", selectedPages, config)) if err != nil { return } } func exampleProcessExtractPages() { // Extract single-page PDFs for pages 3, 4 and 5. selectedPages := []string{"3..5"} config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" _, err := Process(ExtractPagesCommand("in.pdf", "dirOut", selectedPages, config)) if err != nil { return } } func exampleProcessExtractImages() { // Extract all embedded images for first 5 and last 5 pages but not for page 4. selectedPages := []string{"-5", "5-", "!4"} config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" _, err := Process(ExtractImagesCommand("in.pdf", "dirOut", selectedPages, config)) if err != nil { return } } func exampleProcessListAttachments() { config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = opw" list, err := Process(ListAttachmentsCommand("in.pdf", config)) if err != nil { return } // Print attachment list. for _, l := range list { fmt.Println(l) } } func exampleProcessAddAttachments() { config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" _, err := Process(AddAttachmentsCommand("in.pdf", []string{"a.csv", "b.jpg", "c.pdf"}, config)) if err != nil { return } } func exampleProcessRemoveAttachments() { config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Not to be confused with the ExtractAttachmentsCommand! // Remove all attachments. _, err := Process(RemoveAttachmentsCommand("in.pdf", nil, config)) if err != nil { return } // Remove specific attachments. _, err = Process(RemoveAttachmentsCommand("in.pdf", []string{"a.csv", "b.jpg"}, config)) if err != nil { return } } func exampleProcessExtractAttachments() { config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" // Extract all attachments. _, err := Process(ExtractAttachmentsCommand("in.pdf", "dirOut", nil, config)) if err != nil { return } // Extract specific attachments. _, err = Process(ExtractAttachmentsCommand("in.pdf", "dirOut", []string{"a.csv", "b.pdf"}, config)) if err != nil { return } } func exampleProcessEncrypt() { config := pdfcpu.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" _, err := Process(EncryptCommand("in.pdf", "out.pdf", config)) if err != nil { return } } func exampleProcessDecrypt() { config := pdfcpu.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" _, err := Process(DecryptCommand("in.pdf", "out.pdf", config)) if err != nil { return } } func exampleProcessChangeUserPW() { config := pdfcpu.NewDefaultConfiguration() // supply existing owner pw like so config.OwnerPW = "opw" pwOld := "pwOld" pwNew := "pwNew" _, err := Process(ChangeUserPWCommand("in.pdf", "out.pdf", config, &pwOld, &pwNew)) if err != nil { return } } func exampleProcessChangeOwnerPW() { config := pdfcpu.NewDefaultConfiguration() // supply existing user pw like so config.UserPW = "upw" // old and new owner pw pwOld := "pwOld" pwNew := "pwNew" _, err := Process(ChangeOwnerPWCommand("in.pdf", "out.pdf", config, &pwOld, &pwNew)) if err != nil { return } } func exampleProcesslistPermissions() { config := pdfcpu.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" list, err := Process(ListPermissionsCommand("in.pdf", config)) if err != nil { return } // Print permissions list. for _, l := range list { fmt.Println(l) } } func exampleProcessAddPermissions() { config := pdfcpu.NewDefaultConfiguration() config.UserPW = "upw" config.OwnerPW = "opw" config.UserAccessPermissions = pdfcpu.PermissionsAll _, err := Process(AddPermissionsCommand("in.pdf", config)) if err != nil { return } } func exampleProcessStamp() { // Stamp all but the first page. selectedPages := []string{"odd,!1"} var watermark *pdfcpu.Watermark config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" _, err := Process(AddWatermarksCommand("in.pdf", "out.pdf", selectedPages, watermark, config)) if err != nil { return } } func exampleProcessWatermark() { // Stamp all but the first page. selectedPages := []string{"even"} var watermark *pdfcpu.Watermark config := pdfcpu.NewDefaultConfiguration() // Set optional password(s). //config.UserPW = "upw" //config.OwnerPW = "opw" _, err := Process(AddWatermarksCommand("in.pdf", "out.pdf", selectedPages, watermark, config)) if err != nil { return } }
Output:
func ReadContext ¶ added in v0.1.18
func ReadContext(rs io.ReadSeeker, fileIn string, fileSize int64, config *pdf.Configuration) (*pdf.Context, error)
ReadContext uses an io.Readseeker to build an internal structure holding its cross reference table aka the Context.
Example ¶
// This example shows calling into the API with ReadSeeker/Writer. // This allows to run pdf as a backend to an http server for on the fly pdf processing. config := pdf.NewDefaultConfiguration() fileIn := filepath.Join(inDir, "CenterOfWhy.pdf") fileOut := filepath.Join(outDir, "test.pdf") rs, err := os.Open(fileIn) if err != nil { return } defer func() { rs.Close() }() fileInfo, err := rs.Stat() if err != nil { return } ctx, err := ReadContext(rs, fileIn, fileInfo.Size(), config) if err != nil { return } err = ValidateContext(ctx) if err != nil { return } err = OptimizeContext(ctx) if err != nil { return } w, err := os.Create(fileOut) if err != nil { return } defer func() { // The underlying bufio.Writer has already been flushed. // Processing error takes precedence. if err != nil { w.Close() return } // Do not miss out on closing errors. err = w.Close() }() err = WriteContext(ctx, w) if err != nil { return }
Output:
func ReadContextFromFile ¶ added in v0.1.18
ReadContextFromFile reads in a PDF file and builds an internal structure holding its cross reference table aka the Context.
func RemoveAttachments ¶
func RemoveAttachments(fileIn string, files []string, config *pdf.Configuration) error
RemoveAttachments deletes embedded files from a PDF.
func Split ¶
Split generates a sequence of PDF files in dirOut obeying given split span. The default span 1 creates a sequence of single page PDFs.
func ValidateContext ¶ added in v0.1.18
ValidateContext validates a PDF context.
Types ¶
type Command ¶
type Command struct { Mode pdf.CommandMode // VALIDATE OPTIMIZE SPLIT MERGE EXTRACT TRIM LISTATT ADDATT REMATT EXTATT ENCRYPT DECRYPT CHANGEUPW CHANGEOPW LISTP ADDP WATERMARK IMPORT ROTATE NUP InFile *string // * * * - * * * * * * * * * * * * * - * - InFiles []string // - - - * - - - * * * - - - - - - - * - * InDir *string // - - - - - - - - - - - - - - - - - - - - OutFile *string // - * - * - * - - - - * * * * - - * * - * OutDir *string // - - * - * - - - - * - - - - - - - - - - PageSelection []string // - - - - * * - - - - - - - - - - * - - * Config *pdf.Configuration // * * * * * * * * * * * * * * * * * * * * PWOld *string // - - - - - - - - - - - - * * - - - - - - PWNew *string // - - - - - - - - - - - - * * - - - - - - Watermark *pdf.Watermark // - - - - - - - - - - - - - - - - - - - - Span int // - - * - - - - - - - - - - - - - - - - - Import *pdf.Import // - - - - - - - - - - - - - - - - - * - - Rotation int // - - - - - - - - - - - - - - - - - - * - NUp *pdf.NUp // - - - - - - - - - - - - - - - - - - - * }
Command represents an execution context.
func AddAttachmentsCommand ¶
func AddAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *pdf.Configuration) *Command
AddAttachmentsCommand creates a new command to add attachments.
func AddPermissionsCommand ¶
func AddPermissionsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command
AddPermissionsCommand creates a new command to add permissions.
func AddWatermarksCommand ¶ added in v0.1.16
func AddWatermarksCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, wm *pdf.Watermark, config *pdf.Configuration) *Command
AddWatermarksCommand creates a new command to add Watermarks to a file.
func ChangeOwnerPWCommand ¶
func ChangeOwnerPWCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration, pwOld, pwNew *string) *Command
ChangeOwnerPWCommand creates a new command to change the owner password.
func ChangeUserPWCommand ¶
func ChangeUserPWCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration, pwOld, pwNew *string) *Command
ChangeUserPWCommand creates a new command to change the user password.
func DecryptCommand ¶
func DecryptCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command
DecryptCommand creates a new command to decrypt a file.
func EncryptCommand ¶
func EncryptCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command
EncryptCommand creates a new command to encrypt a file.
func ExtractAttachmentsCommand ¶
func ExtractAttachmentsCommand(pdfFileNameIn, dirNameOut string, fileNamesIn []string, config *pdf.Configuration) *Command
ExtractAttachmentsCommand creates a new command to extract attachments.
func ExtractContentCommand ¶
func ExtractContentCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command
ExtractContentCommand creates a new command to extract page content streams.
func ExtractFontsCommand ¶
func ExtractFontsCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command
ExtractFontsCommand creates a new command to extract embedded fonts. (experimental)
func ExtractImagesCommand ¶
func ExtractImagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command
ExtractImagesCommand creates a new command to extract embedded images. (experimental
func ExtractMetadataCommand ¶ added in v0.1.16
func ExtractMetadataCommand(pdfFileNameIn, dirNameOut string, config *pdf.Configuration) *Command
ExtractMetadataCommand creates a new command to extract metadata streams.
func ExtractPagesCommand ¶
func ExtractPagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command
ExtractPagesCommand creates a new command to extract specific pages of a file.
func ImportImagesCommand ¶ added in v0.1.20
func ImportImagesCommand(imageFileNamesIn []string, pdfFileNameOut string, imp *pdf.Import, config *pdf.Configuration) *Command
ImportImagesCommand creates a new command to import images.
func ListAttachmentsCommand ¶
func ListAttachmentsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command
ListAttachmentsCommand create a new command to list attachments.
func ListPermissionsCommand ¶
func ListPermissionsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command
ListPermissionsCommand create a new command to list permissions.
func MergeCommand ¶
func MergeCommand(pdfFileNamesIn []string, pdfFileNameOut string, config *pdf.Configuration) *Command
MergeCommand creates a new command to merge files.
func NUpCommand ¶ added in v0.1.21
func NUpCommand(fileNamesIn []string, pdfFileNameOut string, pageSelection []string, nUp *pdf.NUp, config *pdf.Configuration) *Command
NUpCommand creates a new command to render PDFs or image files in n-up fashion.
func OptimizeCommand ¶
func OptimizeCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command
OptimizeCommand creates a new command to optimize a file.
func RemoveAttachmentsCommand ¶
func RemoveAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *pdf.Configuration) *Command
RemoveAttachmentsCommand creates a new command to remove attachments.
func RotateCommand ¶ added in v0.1.20
func RotateCommand(pdfFileNameIn string, rotation int, pageSelection []string, config *pdf.Configuration) *Command
RotateCommand creates a new command to rotate pages.
func SplitCommand ¶
func SplitCommand(pdfFileNameIn, dirNameOut string, span int, config *pdf.Configuration) *Command
SplitCommand creates a new command to split a file into single page file.
func TrimCommand ¶
func TrimCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, config *pdf.Configuration) *Command
TrimCommand creates a new command to trim the pages of a file.
func ValidateCommand ¶
func ValidateCommand(pdfFileName string, config *pdf.Configuration) *Command
ValidateCommand creates a new command to validate a file.