pdfcpu: github.com/hhrutter/pdfcpu/pkg/api Index | Examples | Files

package api

import "github.com/hhrutter/pdfcpu/pkg/api"

Package api provides support for interacting with pdf.

Index

Examples

Package Files

api.go process.go selectPages.go

func AddAttachments Uses

func AddAttachments(fileIn string, files []string, config *pdf.Configuration) error

AddAttachments embeds files into a PDF.

func AddPermissions Uses

func AddPermissions(fileIn string, config *pdf.Configuration) error

AddPermissions sets the user access permissions.

func AddWatermarks Uses

func AddWatermarks(cmd *Command) ([]string, error)

AddWatermarks adds watermarks to all pages selected.

func ChangeOwnerPassword Uses

func ChangeOwnerPassword(cmd *Command) ([]string, error)

ChangeOwnerPassword of fileIn and write result to fileOut.

func ChangeUserPassword Uses

func ChangeUserPassword(cmd *Command) ([]string, error)

ChangeUserPassword of fileIn and write result to fileOut.

func Decrypt Uses

func Decrypt(cmd *Command) ([]string, error)

Decrypt fileIn and write result to fileOut.

func Encrypt Uses

func Encrypt(cmd *Command) ([]string, error)

Encrypt fileIn and write result to fileOut.

func ExtractAttachments Uses

func ExtractAttachments(fileIn, dirOut string, files []string, config *pdf.Configuration) error

ExtractAttachments extracts embedded files from a PDF.

func ExtractContent Uses

func ExtractContent(cmd *Command) ([]string, error)

ExtractContent dumps "PDF source" files from fileIn into dirOut for selected pages.

func ExtractFonts Uses

func ExtractFonts(cmd *Command) ([]string, error)

ExtractFonts dumps embedded fontfiles from fileIn into dirOut for selected pages.

func ExtractImages Uses

func ExtractImages(cmd *Command) ([]string, error)

ExtractImages dumps embedded image resources from fileIn into dirOut for selected pages.

func ExtractMetadata Uses

func ExtractMetadata(cmd *Command) ([]string, error)

ExtractMetadata dumps all metadata dict entries for fileIn into dirOut.

func ExtractPages Uses

func ExtractPages(cmd *Command) ([]string, error)

ExtractPages generates single page PDF files from fileIn in dirOut for selected pages.

func ImportImages Uses

func ImportImages(cmd *Command) ([]string, error)

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 InsertPages Uses

func InsertPages(cmd *Command) ([]string, error)

InsertPages inserts a blank page at every page selected.

func ListAttachments Uses

func ListAttachments(fileIn string, config *pdf.Configuration) ([]string, error)

ListAttachments returns a list of embedded file attachments.

func ListPermissions Uses

func ListPermissions(fileIn string, config *pdf.Configuration) ([]string, error)

ListPermissions returns a list of user access permissions.

func Merge Uses

func Merge(cmd *Command) ([]string, 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 MergeContexts Uses

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 NUp Uses

func NUp(cmd *Command) ([]string, error)

NUp rearranges pages or images into page grids.

func Optimize Uses

func Optimize(cmd *Command) ([]string, error)

Optimize reads in fileIn, does validation, optimization and writes the result to fileOut.

func OptimizeContext Uses

func OptimizeContext(ctx *pdf.Context) error

OptimizeContext optimizes a PDF context.

func ParsePageSelection Uses

func ParsePageSelection(s string) ([]string, error)

ParsePageSelection ensures a correct page selection expression.

func Process Uses

func Process(cmd *Command) (out []string, err error)

Process executes a pdfcpu command.

Code:

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()

    // Provide 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()

    // Provide 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
    }

}

func ReadContext Uses

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.

Code:


// 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()
config.Cmd = pdf.OPTIMIZE

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
}

func ReadContextFromFile Uses

func ReadContextFromFile(fileIn string, config *pdf.Configuration) (*pdf.Context, error)

ReadContextFromFile reads in a PDF file and builds an internal structure holding its cross reference table aka the Context.

func RemoveAttachments Uses

func RemoveAttachments(fileIn string, files []string, config *pdf.Configuration) error

RemoveAttachments deletes embedded files from a PDF.

func RemovePages Uses

func RemovePages(cmd *Command) ([]string, error)

RemovePages removes selected pages.

func Rotate Uses

func Rotate(cmd *Command) ([]string, error)

Rotate rotates selected pages clockwise.

func Split Uses

func Split(cmd *Command) ([]string, error)

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 Trim Uses

func Trim(cmd *Command) ([]string, error)

Trim generates a trimmed version of fileIn containing all pages selected.

func Validate Uses

func Validate(cmd *Command) ([]string, error)

Validate validates a PDF file against ISO-32000-1:2008.

func ValidateContext Uses

func ValidateContext(ctx *pdf.Context) error

ValidateContext validates a PDF context.

func Write Uses

func Write(ctx *pdf.Context) error

Write generates a PDF file for a given Context.

func WriteContext Uses

func WriteContext(ctx *pdf.Context, w io.Writer) error

WriteContext writes a PDF context.

type Command Uses

type Command struct {
    Mode          pdf.CommandMode    // VALIDATE  OPTIMIZE  SPLIT  MERGE  EXTRACT  TRIM  LISTATT ADDATT REMATT EXTATT  ENCRYPT  DECRYPT  CHANGEUPW  CHANGEOPW LISTP ADDP  WATERMARK  IMPORT  INSERTP REMOVEP 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 Uses

func AddAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *pdf.Configuration) *Command

AddAttachmentsCommand creates a new command to add attachments.

func AddPermissionsCommand Uses

func AddPermissionsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command

AddPermissionsCommand creates a new command to add permissions.

func AddWatermarksCommand Uses

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 Uses

func ChangeOwnerPWCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration, pwOld, pwNew *string) *Command

ChangeOwnerPWCommand creates a new command to change the owner password.

func ChangeUserPWCommand Uses

func ChangeUserPWCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration, pwOld, pwNew *string) *Command

ChangeUserPWCommand creates a new command to change the user password.

func DecryptCommand Uses

func DecryptCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command

DecryptCommand creates a new command to decrypt a file.

func EncryptCommand Uses

func EncryptCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command

EncryptCommand creates a new command to encrypt a file.

func ExtractAttachmentsCommand Uses

func ExtractAttachmentsCommand(pdfFileNameIn, dirNameOut string, fileNamesIn []string, config *pdf.Configuration) *Command

ExtractAttachmentsCommand creates a new command to extract attachments.

func ExtractContentCommand Uses

func ExtractContentCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command

ExtractContentCommand creates a new command to extract page content streams.

func ExtractFontsCommand Uses

func ExtractFontsCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command

ExtractFontsCommand creates a new command to extract embedded fonts. (experimental)

func ExtractImagesCommand Uses

func ExtractImagesCommand(pdfFileNameIn, dirNameOut string, pageSelection []string, config *pdf.Configuration) *Command

ExtractImagesCommand creates a new command to extract embedded images. (experimental

func ExtractMetadataCommand Uses

func ExtractMetadataCommand(pdfFileNameIn, dirNameOut string, config *pdf.Configuration) *Command

ExtractMetadataCommand creates a new command to extract metadata streams.

func ExtractPagesCommand Uses

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 Uses

func ImportImagesCommand(imageFileNamesIn []string, pdfFileNameOut string, imp *pdf.Import, config *pdf.Configuration) *Command

ImportImagesCommand creates a new command to import images.

func InsertPagesCommand Uses

func InsertPagesCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, config *pdf.Configuration) *Command

InsertPagesCommand creates a new command to insert a blank page before selected pages.

func ListAttachmentsCommand Uses

func ListAttachmentsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command

ListAttachmentsCommand create a new command to list attachments.

func ListPermissionsCommand Uses

func ListPermissionsCommand(pdfFileNameIn string, config *pdf.Configuration) *Command

ListPermissionsCommand create a new command to list permissions.

func MergeCommand Uses

func MergeCommand(pdfFileNamesIn []string, pdfFileNameOut string, config *pdf.Configuration) *Command

MergeCommand creates a new command to merge files.

func NUpCommand Uses

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 Uses

func OptimizeCommand(pdfFileNameIn, pdfFileNameOut string, config *pdf.Configuration) *Command

OptimizeCommand creates a new command to optimize a file.

func RemoveAttachmentsCommand Uses

func RemoveAttachmentsCommand(pdfFileNameIn string, fileNamesIn []string, config *pdf.Configuration) *Command

RemoveAttachmentsCommand creates a new command to remove attachments.

func RemovePagesCommand Uses

func RemovePagesCommand(pdfFileNameIn, pdfFileNameOut string, pageSelection []string, config *pdf.Configuration) *Command

RemovePagesCommand creates a new command to remove selected pages.

func RotateCommand Uses

func RotateCommand(pdfFileNameIn string, rotation int, pageSelection []string, config *pdf.Configuration) *Command

RotateCommand creates a new command to rotate pages.

func SplitCommand Uses

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 Uses

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 Uses

func ValidateCommand(pdfFileName string, config *pdf.Configuration) *Command

ValidateCommand creates a new command to validate a file.

Package api imports 14 packages (graph) and is imported by 2 packages. Updated 2019-03-30. Refresh now. Tools for package owners.