api

package
v0.1.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package api provides support for interacting with pdf.

Index

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

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

AddWatermarks adds watermarks to all pages selected.

func ChangeOwnerPassword

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

ChangeOwnerPassword of fileIn and write result to fileOut.

func ChangeUserPassword

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

ChangeUserPassword of fileIn and write result to fileOut.

func Decrypt

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

Decrypt fileIn and write result to fileOut.

func Encrypt

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

Encrypt 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

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

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

func ExtractFonts

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

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

func ExtractImages

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

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

func ExtractMetadata added in v0.1.16

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

ExtractMetadata dumps all metadata dict entries for fileIn into dirOut.

func ExtractPages

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

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

func ImportImages added in v0.1.20

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

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

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

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

func OptimizeContext added in v0.1.18

func OptimizeContext(ctx *pdf.Context) error

OptimizeContext optimizes a PDF context.

func ParsePageSelection

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

ParsePageSelection ensures a correct page selection expression.

func Process

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

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

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

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

RemoveAttachments deletes embedded files from a PDF.

func Rotate added in v0.1.20

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

Rotate rotates selected pages clockwise.

func Split

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

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

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

func Validate

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

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

func ValidateContext added in v0.1.18

func ValidateContext(ctx *pdf.Context) error

ValidateContext validates a PDF context.

func Write

func Write(ctx *pdf.Context) error

Write generates a PDF file for a given Context.

func WriteContext added in v0.1.18

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

WriteContext writes 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
	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                //    -         -        -      -       -      -      -       -       -      -       -        -         -          -       -     -       -         -       *
}

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 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL