pdf

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 13 Imported by: 0

README

pdf — PDF manipulation

import "github.com/downsized-devs/sdk-go/pdf"

Stability: Stable. See STABILITY.md.

In-memory PDF operations: AES-256 password protection, page-level merge / split, text watermarking, page count, and best-effort text extraction.

Features

Method What it does
Encrypt AES-256 password protect (sets both user and owner password).
RemovePassword Strip password from an encrypted PDF given the password.
Merge Concatenate two or more PDFs into one.
Split Split a PDF into chunks of span pages.
AddTextWatermark Stamp text across every page.
ExtractText Best-effort plain text extraction (no layout).
PageCount Number of pages in a PDF.

All methods take and return []byte so no temp files are visible to callers (Split uses a temp directory internally; it is removed before returning).

Installation

go get github.com/downsized-devs/sdk-go/pdf

Quick Start

log := logger.Init(logger.Config{})
p := pdf.Init(log)

encrypted, err := p.Encrypt(ctx, raw, "s3cret")
if err != nil { /* ... */ }

merged, err := p.Merge(ctx, partA, partB, partC)
chunks, err := p.Split(ctx, big, 1)            // one page per chunk
stamped, err := p.AddTextWatermark(ctx, raw, "DRAFT")
text,    err := p.ExtractText(ctx, raw)
pages,   err := p.PageCount(ctx, raw)

API Reference

Symbol Signature
Init func Init(log logger.Interface) Interface
Interface.Encrypt (ctx, data []byte, password string) ([]byte, error)
Interface.RemovePassword (ctx, data []byte, password string) ([]byte, error)
Interface.Merge (ctx, parts ...[]byte) ([]byte, error)
Interface.Split (ctx, data []byte, span int) ([][]byte, error)
Interface.AddTextWatermark (ctx, data []byte, text string) ([]byte, error)
Interface.ExtractText (ctx, data []byte) (string, error)
Interface.PageCount (ctx, data []byte) (int, error)

ctx is accepted on every method for forward compatibility; cancellation is not yet propagated to the underlying libraries.

Error Handling

All []byte inputs are rejected with codes.CodeBadRequest when empty. Underlying pdfcpu / ledongthuc errors are surfaced unchanged.

Dependencies

  • Internal: codes, errors, logger
  • External: github.com/pdfcpu/pdfcpu (all operations except ExtractText), github.com/ledongthuc/pdf (ExtractText only).

Testing

go test ./pdf/...

Coverage ≥85% via the bundled example.pdf round-tripped through every method.

  • files — extension validation for input paths.
  • security — for non-PDF crypto needs.

Documentation

Overview

Package pdf is a thin in-memory wrapper around pdfcpu (encryption, merge, split, watermark, page count) plus ledongthuc/pdf for text extraction.

All methods take and return []byte; no temp files leak out except where pdfcpu itself only exposes a directory-based API (Split), in which case a temp directory is created and removed before returning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface added in v1.0.0

type Interface interface {
	Encrypt(ctx context.Context, data []byte, password string) ([]byte, error)
	RemovePassword(ctx context.Context, data []byte, password string) ([]byte, error)
	Merge(ctx context.Context, parts ...[]byte) ([]byte, error)
	Split(ctx context.Context, data []byte, span int) ([][]byte, error)
	AddTextWatermark(ctx context.Context, data []byte, text string) ([]byte, error)
	ExtractText(ctx context.Context, data []byte) (string, error)
	PageCount(ctx context.Context, data []byte) (int, error)
}

func Init

func Init(log logger.Interface) Interface

Jump to

Keyboard shortcuts

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