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

package api

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

Package api lets you integrate pdfcpu's operations into your Go backend.

There are two api layers supporting all pdfcpu operations:

1) The file based layer (used by pdfcpu's cli)
2) The io.ReadSeeker/io.Writer based layer for backend integration.

For any pdfcpu command there are two functions.

The file based function always calls the io.ReadSeeker/io.Writer based function:

func CommandFile(inFile, outFile string, conf *pdf.Configuration) error
func Command(rs io.ReadSeeker, w io.Writer, conf *pdf.Configuration) error

eg. for optimization:

func OptimizeFile(inFile, outFile string, conf *pdf.Configuration) error
func Optimize(rs io.ReadSeeker, w io.Writer, conf *pdf.Configuration) error

Index

Examples

Package Files

api.go attach.go create.go extract.go selectPages.go

func AddAttachments Uses

func AddAttachments(rs io.ReadSeeker, w io.Writer, files []string, conf *pdf.Configuration) error

AddAttachments embeds files into a PDF context read from rs and writes the result to w.

func AddAttachmentsFile Uses

func AddAttachmentsFile(inFile, outFile string, files []string, conf *pdf.Configuration) (err error)

AddAttachmentsFile embeds files into a PDF context read from inFile and writes the result to outFile.

Code:


// Attach 3 files to in.pdf.
AddAttachmentsFile("in.pdf", "", []string{"img.jpg", "attach.pdf", "test.zip"}, nil)

func AddWatermarks Uses

func AddWatermarks(rs io.ReadSeeker, w io.Writer, selectedPages []string, wm *pdf.Watermark, conf *pdf.Configuration) error

AddWatermarks adds watermarks to all pages selected in rs and writes the result to w.

func AddWatermarksFile Uses

func AddWatermarksFile(inFile, outFile string, selectedPages []string, wm *pdf.Watermark, conf *pdf.Configuration) (err error)

AddWatermarksFile adds watermarks to all selected pages of inFile and writes the result to outFile.

Code:


// Add a "Demo" watermark to all pages of in.pdf along the diagonal running from lower left to upper right.
onTop := false
wm, _ := pdfcpu.ParseWatermarkDetails("Demo", onTop)
AddWatermarksFile("in.pdf", "", nil, wm, nil)

// Stamp all odd pages of in.pdf in red "Confidential" in 48 point Courier using a rotation angle of 45 degrees.
onTop = true
wm, _ = pdfcpu.ParseWatermarkDetails("Confidential, f:Courier, c: 1 0 0, r:45, s:1 abs, p:48", onTop)
AddWatermarksFile("in.pdf", "", []string{"odd"}, wm, nil)

// Add image stamps to in.pdf using absolute scaling and a negative rotation of 90 degrees.
onTop = true
wm, _ = pdfcpu.ParseWatermarkDetails("image.png, s:.5 a, r:-90", onTop)
AddWatermarksFile("in.pdf", "", nil, wm, nil)

// Add a PDF stamp to all pages of in.pdf using the 2nd page of stamp.pdf, use absolute scaling of 0.5
// and rotate along the 2nd diagonal running from upper left to lower right corner.
onTop = true
wm, _ = pdfcpu.ParseWatermarkDetails("stamp.pdf:2, s:.5 a, d:2", onTop)
AddWatermarksFile("in.pdf", "", nil, wm, nil)

func ChangeOwnerPasswordFile Uses

func ChangeOwnerPasswordFile(inFile, outFile string, pwOld, pwNew string, conf *pdf.Configuration) error

ChangeOwnerPasswordFile reads inFile, changes the user password and writes the result to outFile. A configuration containing the current passwords is required.

Code:


// Changing the owner password for an AES-256 encrypted file.
conf := pdf.NewAESConfiguration("upw", "opw", 256)
ChangeOwnerPasswordFile("in.pdf", "", "opw", "opwNew", conf)

func ChangeUserPasswordFile Uses

func ChangeUserPasswordFile(inFile, outFile string, pwOld, pwNew string, conf *pdf.Configuration) error

ChangeUserPasswordFile reads inFile, changes the user password and writes the result to outFile. A configuration containing the current passwords is required.

Code:


// Changing the user password for an AES-256 encrypted file.
conf := pdf.NewAESConfiguration("upw", "opw", 256)
ChangeUserPasswordFile("in.pdf", "", "upw", "upwNew", conf)

func CreatePDFFile Uses

func CreatePDFFile(xRefTable *pdf.XRefTable, outFile string, conf *pdf.Configuration) error

CreatePDFFile creates a PDF file for an xRefTable and writes it to outFile.

func DecryptFile Uses

func DecryptFile(inFile, outFile string, conf *pdf.Configuration) error

DecryptFile decrypts inFile and writes the result to outFile. A configuration containing the current passwords is required.

Code:


// Decrypting an AES-256 encrypted file.
conf := pdf.NewAESConfiguration("upw", "opw", 256)
DecryptFile("in.pdf", "", conf)

func EncryptFile Uses

func EncryptFile(inFile, outFile string, conf *pdf.Configuration) error

EncryptFile encrypts inFile and writes the result to outFile. A configuration containing the current passwords is required.

Code:


// Encrypting a file using AES-256.
conf := pdf.NewAESConfiguration("upw", "opw", 256)
EncryptFile("in.pdf", "", conf)

func ExtractAttachments Uses

func ExtractAttachments(rs io.ReadSeeker, outDir string, fileNames []string, conf *pdf.Configuration) error

ExtractAttachments extracts embedded files from a PDF context read from rs into outDir.

func ExtractAttachmentsFile Uses

func ExtractAttachmentsFile(inFile, outDir string, fileNames []string, conf *pdf.Configuration) error

ExtractAttachmentsFile extracts embedded files from a PDF context read from inFile into outDir.

Code:


// Extract 1 attachment from in.pdf into outDir.
ExtractAttachmentsFile("in.pdf", "outDir", []string{"img.jpg"}, nil)

// Extract all attachments from in.pdf into outDir
ExtractAttachmentsFile("in.pdf", "outDir", nil, nil)

func ExtractContent Uses

func ExtractContent(rs io.ReadSeeker, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractContent dumps "PDF source" files from rs into outDir for selected pages.

func ExtractContentFile Uses

func ExtractContentFile(inFile, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractContentFile dumps "PDF source" files from inFile into outDir for selected pages.

Code:


// Extract content for all pages in PDF syntax from in.pdf into outDir.
ExtractContentFile("in.pdf", "outDir", nil, nil)

func ExtractFonts Uses

func ExtractFonts(rs io.ReadSeeker, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractFonts dumps embedded fontfiles from rs into outDir for selected pages.

func ExtractFontsFile Uses

func ExtractFontsFile(inFile, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractFontsFile dumps embedded fontfiles from inFile into outDir for selected pages.

Code:


// Extract embedded fonts for pages 1-3 from in.pdf into outDir.
ExtractFontsFile("in.pdf", outDir, []string{"1-3"}, nil)

func ExtractImages Uses

func ExtractImages(rs io.ReadSeeker, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractImages dumps embedded image resources from rs into outDir for selected pages.

func ExtractImagesFile Uses

func ExtractImagesFile(inFile, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractImagesFile dumps embedded image resources from inFile into outDir for selected pages.

Code:


// Extract embedded images from in.pdf into outDir.
ExtractImagesFile("in.pdf", "outDir", nil, nil)

func ExtractMetadata Uses

func ExtractMetadata(rs io.ReadSeeker, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractMetadata dumps all metadata dict entries for rs into outDir.

func ExtractMetadataFile Uses

func ExtractMetadataFile(inFile, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractMetadataFile dumps all metadata dict entries for inFile into outDir.

Code:


// Extract all metadata from in.pdf into outDir.
ExtractMetadataFile("in.pdf", "outDir", nil, nil)

func ExtractPages Uses

func ExtractPages(rs io.ReadSeeker, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractPages generates single page PDF files from rs in outDir for selected pages.

func ExtractPagesFile Uses

func ExtractPagesFile(inFile, outDir string, selectedPages []string, conf *pdf.Configuration) error

ExtractPagesFile generates single page PDF files from inFile in outDir for selected pages.

Code:


// Extract all even numbered pages from in.pdf into outDir.
ExtractPagesFile("in.pdf", outDir, []string{"even"}, nil)

func ImportImages Uses

func ImportImages(rs io.ReadSeeker, w io.Writer, imgs []io.Reader, imp *pdf.Import, conf *pdf.Configuration) error

ImportImages appends PDF pages containing images to rs and writes the result to w. If rs == nil a new PDF file will be written to w.

func ImportImagesFile Uses

func ImportImagesFile(imgFiles []string, outFile string, imp *pdf.Import, conf *pdf.Configuration) (err error)

ImportImagesFile appends PDF pages containing images to outFile which will be created if necessary.

Code:


// Convert an image into a single page of out.pdf which will be created if necessary.
// The page dimensions will match the image dimensions.
// If out.pdf already exists, append a new page.
// Use the default import configuration.
ImportImagesFile([]string{"image.png"}, "out.pdf", nil, nil)

// Import images by creating an A3 page for each image.
// Images are page centered with 1.0 relative scaling.
// Import an image as a new page of the existing out.pdf.
imp, _ := pdf.ParseImportDetails("f:A3, p:c, s:1.0")
ImportImagesFile([]string{"a1.png", "a2.jpg", "a3.tiff"}, "out.pdf", imp, nil)

func Info Uses

func Info(rs io.ReadSeeker, conf *pdf.Configuration) ([]string, error)

Info returns information about rs.

func InfoFile Uses

func InfoFile(inFile string, conf *pdf.Configuration) ([]string, error)

InfoFile returns information about inFile.

func InsertPages Uses

func InsertPages(rs io.ReadSeeker, w io.Writer, selectedPages []string, conf *pdf.Configuration) error

InsertPages inserts a blank page at every page selected of rs and writes the result to w.

func InsertPagesFile Uses

func InsertPagesFile(inFile, outFile string, selectedPages []string, conf *pdf.Configuration) (err error)

InsertPagesFile inserts a blank page at every inFile page selected and writes the result to w.

Code:


// Insert a blank page into in.pdf before page #3.
InsertPagesFile("in.pdf", "", []string{"3"}, nil)

// Insert a blank page into in.pdf before every page.
InsertPagesFile("in.pdf", "", nil, nil)

func ListAttachments Uses

func ListAttachments(rs io.ReadSeeker, conf *pdf.Configuration) ([]string, error)

ListAttachments returns a list of embedded file attachments of rs.

func ListAttachmentsFile Uses

func ListAttachmentsFile(inFile string, conf *pdf.Configuration) ([]string, error)

ListAttachmentsFile returns a list of embedded file attachments of inFile.

Code:


// Output a list of attachments of in.pdf.
list, _ := ListAttachmentsFile("in.pdf", nil)
for _, s := range list {
    fmt.Println(s)
}

func ListPermissions Uses

func ListPermissions(rs io.ReadSeeker, conf *pdf.Configuration) ([]string, error)

ListPermissions returns a list of user access permissions.

func ListPermissionsFile Uses

func ListPermissionsFile(inFile string, conf *pdf.Configuration) ([]string, error)

ListPermissionsFile returns a list of user access permissions for inFile.

Code:


// Output the current permissions of in.pdf.
list, _ := ListPermissionsFile("in.pdf", nil)
for _, s := range list {
    fmt.Println(s)
}

func Merge Uses

func Merge(rsc []io.ReadSeeker, w io.Writer, conf *pdf.Configuration) error

Merge merges a sequence of PDF streams and writes the result to w.

func MergeFile Uses

func MergeFile(inFiles []string, outFile string, conf *pdf.Configuration) error

MergeFile merges a sequence of inFiles and writes the result to outFile. This operation corresponds to file concatenation in the order specified by inFiles. The first entry of inFiles serves as the destination context where all remaining files get merged into.

Code:


// Merge inFiles by concatenation in the order specified and write the result to out.pdf.
inFiles := []string{"in1.pdf", "in2.pdf"}
MergeFile(inFiles, "out.pdf", nil)

func NUp Uses

func NUp(rs io.ReadSeeker, w io.Writer, imgFiles, selectedPages []string, nup *pdf.NUp, conf *pdf.Configuration) error

NUp rearranges PDF pages or images into page grids and writes the result to w. Either rs or imgFiles will be used.

func NUpFile Uses

func NUpFile(inFiles []string, outFile string, selectedPages []string, nup *pdf.NUp, conf *pdf.Configuration) (err error)

NUpFile rearranges PDF pages or images into page grids and writes the result to outFile.

Code:


// 4-Up in.pdf and write result to out.pdf.
nup, _ := pdf.PDFNUpConfig(4, "")
inFiles := []string{"in.pdf"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

// 9-Up a sequence of images using format Tabloid w/o borders and no margins.
nup, _ = pdf.ImageNUpConfig(9, "f:Tabloid, b:off, m:0")
inFiles = []string{"in1.png", "in2.jpg", "in3.tiff"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

// TestGridFromPDF
nup, _ = pdf.PDFGridConfig(1, 3, "f:LegalL")
inFiles = []string{"in.pdf"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

// TestGridFromImages
nup, _ = pdf.ImageGridConfig(4, 2, "d:500 500, m:20, b:off")
inFiles = []string{"in1.png", "in2.jpg", "in3.tiff"}
NUpFile(inFiles, "out.pdf", nil, nup, nil)

func Optimize Uses

func Optimize(rs io.ReadSeeker, w io.Writer, conf *pdf.Configuration) error

Optimize reads a PDF stream from rs and writes the optimized PDF stream to w.

func OptimizeContext Uses

func OptimizeContext(ctx *pdf.Context) error

OptimizeContext optimizes a PDF context.

func OptimizeFile Uses

func OptimizeFile(inFile, outFile string, conf *pdf.Configuration) (err error)

OptimizeFile reads inFile and writes the optimized PDF to outFile. If outFile is not provided then inFile gets overwritten which leads to the same result as when inFile equals outFile.

Code:


conf := pdfcpu.NewDefaultConfiguration()

// Set passwords for encrypted files.
conf.UserPW = "upw"
conf.OwnerPW = "opw"

// Configure end of line sequence for writing.
conf.Eol = pdfcpu.EolLF

// Create an optimized version of in.pdf and write it to out.pdf.
OptimizeFile("in.pdf", "out.pdf", conf)

// Create an optimized version of inFile.
// If you want to modify the original file, pass an empty string for outFile.
// Use nil for a default configuration.
OptimizeFile("in.pdf", "", nil)

func PageCount Uses

func PageCount(inFile string) (int, error)

PageCount returns inFile's page count.

func ParsePageSelection Uses

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

ParsePageSelection ensures a correct page selection expression.

func ReadContext Uses

func ReadContext(rs io.ReadSeeker, conf *pdf.Configuration) (*pdf.Context, error)

ReadContext uses an io.Readseeker to build an internal structure holding its cross reference table aka the Context.

func ReadContextFile Uses

func ReadContextFile(inFile string) (*pdf.Context, error)

ReadContextFile returns inFile's validated context.

func RemoveAttachments Uses

func RemoveAttachments(rs io.ReadSeeker, w io.Writer, fileNames []string, conf *pdf.Configuration) error

RemoveAttachments deletes embedded files from a PDF context read from rs and writes the result to w.

func RemoveAttachmentsFile Uses

func RemoveAttachmentsFile(inFile, outFile string, files []string, conf *pdf.Configuration) (err error)

RemoveAttachmentsFile deletes embedded files from a PDF context read from inFile and writes the result to outFile.

Code:


// Remove 1 attachment from in.pdf.
RemoveAttachmentsFile("in.pdf", "", []string{"img.jpg"}, nil)

// Remove all attachments from in.pdf
RemoveAttachmentsFile("in.pdf", "", nil, nil)

func RemovePages Uses

func RemovePages(rs io.ReadSeeker, w io.Writer, selectedPages []string, conf *pdf.Configuration) error

RemovePages removes selected pages from rs and writes the result to w.

func RemovePagesFile Uses

func RemovePagesFile(inFile, outFile string, selectedPages []string, conf *pdf.Configuration) (err error)

RemovePagesFile removes selected inFile pages and writes the result to outFile..

Code:


// Remove pages 2 and 8 of in.pdf.
RemovePagesFile("in.pdf", "", []string{"2", "8"}, nil)

// Remove first 2 pages of in.pdf.
RemovePagesFile("in.pdf", "", []string{"-2"}, nil)

// Remove all pages >= 10 of in.pdf.
RemovePagesFile("in.pdf", "", []string{"10-"}, nil)

func Rotate Uses

func Rotate(rs io.ReadSeeker, w io.Writer, rotation int, selectedPages []string, conf *pdf.Configuration) error

Rotate rotates selected pages of rs clockwise by rotation degrees and writes the result to w.

func RotateFile Uses

func RotateFile(inFile, outFile string, rotation int, selectedPages []string, conf *pdf.Configuration) (err error)

RotateFile rotates selected pages of inFile clockwise by rotation degrees and writes the result to outFile.

Code:


// Rotate all pages of in.pdf, clockwise by 90 degrees and write the result to out.pdf.
RotateFile("in.pdf", "out.pdf", 90, nil, nil)

// Rotate the first page of in.pdf by 180 degrees.
// If you want to modify the original file, pass an empty string as outFile.
RotateFile("in.pdf", "", 180, []string{"1"}, nil)

func SetPermissions Uses

func SetPermissions(rs io.ReadSeeker, w io.Writer, conf *pdf.Configuration) error

SetPermissions sets user access permissions. inFile has to be encrypted. A configuration containing the current passwords is required.

func SetPermissionsFile Uses

func SetPermissionsFile(inFile, outFile string, conf *pdf.Configuration) (err error)

SetPermissionsFile sets inFile's user access permissions. inFile has to be encrypted. A configuration containing the current passwords is required.

Code:


// Setting all permissions for the AES-256 encrypted in.pdf.
conf := pdf.NewAESConfiguration("upw", "opw", 256)
conf.Permissions = pdfcpu.PermissionsAll
SetPermissionsFile("in.pdf", "", conf)

// Restricting permissions for the AES-256 encrypted in.pdf.
conf = pdf.NewAESConfiguration("upw", "opw", 256)
conf.Permissions = pdfcpu.PermissionsNone
SetPermissionsFile("in.pdf", "", conf)

func Split Uses

func Split(rs io.ReadSeeker, outDir, fileName string, span int, conf *pdf.Configuration) error

Split generates a sequence of PDF files in outDir for the PDF stream read from rs obeying given split span. The default span 1 creates a sequence of single page PDFs.

func SplitFile Uses

func SplitFile(inFile, outDir string, span int, conf *pdf.Configuration) error

SplitFile generates a sequence of PDF files in outDir for inFile obeying given split span. The default span 1 creates a sequence of single page PDFs.

Code:


// Create single page PDFs for in.pdf in outDir using the default configuration.
SplitFile("in.pdf", "outDir", 1, nil)

// Create dual page PDFs for in.pdf in outDir using the default configuration.
SplitFile("in.pdf", "outDir", 2, nil)

func Trim Uses

func Trim(rs io.ReadSeeker, w io.Writer, selectedPages []string, conf *pdf.Configuration) error

Trim generates a trimmed version of rs containing all selected pages and writes the result to w.

func TrimFile Uses

func TrimFile(inFile, outFile string, selectedPages []string, conf *pdf.Configuration) (err error)

TrimFile generates a trimmed version of inFile containing all selected pages and writes the result to outFile.

Code:


// Create a trimmed version of in.pdf containing odd page numbers only.
TrimFile("in.pdf", "outFile", []string{"odd"}, nil)

// Create a trimmed version of in.pdf containing the first two pages only.
// If you want to modify the original file, pass an empty string for outFile.
TrimFile("in.pdf", "", []string{"1-2"}, nil)

func Validate Uses

func Validate(rs io.ReadSeeker, conf *pdf.Configuration) error

Validate validates a PDF stream read from rs.

func ValidateContext Uses

func ValidateContext(ctx *pdf.Context) error

ValidateContext validates a PDF context.

func ValidateFile Uses

func ValidateFile(inFile string, conf *pdf.Configuration) error

ValidateFile validates inFile.

Code:


// Use the default configuration to validate in.pdf.
ValidateFile("in.pdf", nil)

func WriteContext Uses

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

WriteContext writes a PDF context to w.

type ReadSeekerCloser Uses

type ReadSeekerCloser interface {
    io.ReadSeeker
    io.Closer
}

ReadSeekerCloser combines io.ReadSeeker and io.Closer

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