pdf

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 29 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// ConflictChanged: the file changed on disk since it was opened or last saved.
	ConflictChanged = "changed"
	// ConflictOpen: the Save As target is open in another tab.
	ConflictOpen = "open"
	// ConflictExists: the export target exists and overwriting wasn't asked for.
	ConflictExists = "exists"
)

Reasons for a ConflictError.

View Source
const (
	MinDPI              = 10.0
	MaxDPI              = 600.0
	PDFOpenTimeout      = 120 * time.Second
	PDFRenderTimeout    = 300 * time.Second
	MaxPDFFileSizeBytes = 1 * 1024 * 1024 * 1024 // 1GB maximum file size
)
View Source
const (
	DefaultMaxRecentFiles = 20
)
View Source
const DocumentRoute = "/doc/"

DocumentRoute is where ServeHTTP is mounted on the Wails asset server; pdf.js loads a document from DocumentRoute + id.

Variables

This section is empty.

Functions

func DecodePNGDataURL added in v1.0.0

func DecodePNGDataURL(s string) ([]byte, error)

DecodePNGDataURL returns the raw PNG bytes of the data URLs RenderPage and GenerateThumbnail produce.

Types

type ConflictError added in v1.0.0

type ConflictError struct {
	Reason string `json:"conflict"`
	Path   string `json:"path"`
}

ConflictError is returned instead of writing when a save would clobber changes the app doesn't hold. It reaches the frontend as the error's cause.

func (*ConflictError) Error added in v1.0.0

func (e *ConflictError) Error() string

type Document added in v1.0.0

type Document struct {
	ID       string    `json:"id"`
	Path     string    `json:"path"`
	FileName string    `json:"fileName"`
	Size     int64     `json:"size"`
	ModTime  time.Time `json:"modTime"`
	// Modified: page or document operations left a working copy that isn't saved.
	Modified bool `json:"modified"`
	// UndoSteps: how many of those operations UndoPageOp can still take back.
	UndoSteps int `json:"undoSteps"`
	// RedoSteps: how many undone operations RedoPageOp can bring back.
	RedoSteps int `json:"redoSteps"`
	// contains filtered or unexported fields
}

Document is a PDF the GUI has open. The webview reads and writes it by its random id; the only paths it can name are files the user picked in a dialog or asked to open, and for Save As only the one chosen in ChooseSavePath.

type PDFMetadata

type PDFMetadata struct {
	Title     string `json:"title"`
	Author    string `json:"author"`
	Subject   string `json:"subject"`
	Creator   string `json:"creator"`
	PageCount int    `json:"pageCount"`
	FilePath  string `json:"filePath"`
}

PDFMetadata contains metadata extracted from a PDF document.

type PDFService

type PDFService struct {
	// contains filtered or unexported fields
}

PDFService keeps the registry of documents open in the GUI, serves their bytes to pdf.js and writes the bytes pdf.js produces back to disk.

func NewPDFService

func NewPDFService() *PDFService

NewPDFService creates an empty document registry.

func (*PDFService) ChooseExportFolder added in v1.1.0

func (s *PDFService) ChooseExportFolder(id string) (string, error)

ChooseExportFolder shows a folder dialog for files split from the document; ExportPDF then accepts new PDFs directly inside it. It returns "" when the user cancels.

func (*PDFService) ChooseSavePath added in v1.0.0

func (s *PDFService) ChooseSavePath(id, filename string) (*SaveTarget, error)

ChooseSavePath shows a Save As dialog next to the document, suggesting filename (the document's own name when empty). It returns nil when the user cancels. The GTK dialog doesn't confirm overwrites, so Exists tells the caller to ask.

func (*PDFService) ClosePDF

func (s *PDFService) ClosePDF(id string) error

ClosePDF forgets the document with the given id.

func (*PDFService) DropPageOp added in v1.1.0

func (s *PDFService) DropPageOp(id string) (*Document, error)

DropPageOp takes back the newest working copy as if it had never been pushed: it isn't kept for redo, and the redo steps it replaced come back. A page operation that fails calls it for the copy of unsaved edits it pushed to build on. A push that went past the undo bound already removed the oldest copy, which a drop doesn't bring back: undo then stops one step short.

func (*PDFService) ExportPDF added in v1.1.0

func (s *PDFService) ExportPDF(id, path string, data []byte, overwrite bool) error

ExportPDF writes data to a new file without changing the document: path is the one ChooseSavePath last returned, or a .pdf directly inside the folder ChooseExportFolder returned. An existing file is only replaced with overwrite, and never one that is open. Save options set just before (see SetSaveOptions) apply to this write, e.g. to protect it with a password.

func (*PDFService) InsertBlankPage added in v1.1.0

func (s *PDFService) InsertBlankPage(id string, after int) (*Document, error)

InsertBlankPage adds a blank page after page after (0 = at the front).

func (*PDFService) OpenPDF

func (s *PDFService) OpenPDF() (*Document, error)

OpenPDF shows a file dialog and registers the chosen PDF. It returns nil when the user cancels.

func (*PDFService) OpenPDFByPath

func (s *PDFService) OpenPDFByPath(path string) (*Document, error)

OpenPDFByPath registers the PDF at path under a new id.

func (*PDFService) OpenPDFs added in v1.1.0

func (s *PDFService) OpenPDFs() ([]*Document, error)

OpenPDFs shows a multi-select file dialog and registers each chosen PDF, e.g. to insert its pages into another document. It returns an empty list when the user cancels.

func (*PDFService) PushWorkingCopy added in v1.1.0

func (s *PDFService) PushWorkingCopy(id string, data []byte) (*Document, error)

PushWorkingCopy makes data the document's content, as a new undo step. pdf.js builds data, and its page editor drops the document information (title, author…) of merged files and stamps its own creator and producer, so the current content's information is put back.

func (*PDFService) RedoPageOp added in v1.1.0

func (s *PDFService) RedoPageOp(id string) (*Document, error)

RedoPageOp brings back the working copy UndoPageOp last took back.

func (*PDFService) RevertToSaved added in v1.1.0

func (s *PDFService) RevertToSaved(id string) (*Document, error)

RevertToSaved drops every working copy and pending save option, and takes the file as it is on disk now as the document's saved state.

func (*PDFService) RotatePages added in v1.1.0

func (s *PDFService) RotatePages(id string, pages []int, degrees int) (*Document, error)

RotatePages turns pages (1-based) of the current content clockwise by degrees, a multiple of 90.

func (*PDFService) SavePDF added in v1.0.0

func (s *PDFService) SavePDF(id string, data []byte, overwrite bool) (*Document, error)

SavePDF replaces the document's file with data. Unless overwrite is set it refuses, with a ConflictError, when the file changed on disk since it was opened or last saved.

func (*PDFService) SavePDFAs added in v1.0.0

func (s *PDFService) SavePDFAs(id, path string, data []byte) (*Document, error)

SavePDFAs writes data to path, which must be the one ChooseSavePath last returned for the document; the document then refers to the new file. It refuses, with a ConflictError, a path open under another id.

func (*PDFService) ServeHTTP added in v1.0.0

func (s *PDFService) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP streams a registered document's current content: its newest working copy, else its file. The body of a PUT is saved like SavePDF (?overwrite=1) or SavePDFAs (?saveAs=<path>), pushed like PushWorkingCopy (?work=1) or written like ExportPDF (?export=<path>[&overwrite=1]). Raw bytes avoid the binding call's base64 JSON body and its 64 MiB cap. The asset server strips DocumentRoute, so the request path is just the id.

func (*PDFService) ServiceShutdown added in v1.1.0

func (s *PDFService) ServiceShutdown() error

ServiceShutdown removes this session's working copies when the app exits.

func (*PDFService) ServiceStartup added in v1.1.0

ServiceStartup removes, best effort, the working copies of sessions on this host that ended without ServiceShutdown, by a crash or a kill: those of protected files are decrypted. Another host's sessions, in a home shared over the network, are left alone: their processes can't be seen. So are this host's from before its name changed (DHCP, or macOS naming it by network): they stay until a start under the old name.

func (*PDFService) SetMetadata added in v1.1.0

func (s *PDFService) SetMetadata(id string, m pdfops.Metadata) (*Document, error)

SetMetadata replaces the document information entries (empty ones are removed).

func (*PDFService) SetPassword added in v1.1.0

func (s *PDFService) SetPassword(id, password string) error

SetPassword tells Go the password the document was opened with, so pdfcpu operations and rewriting saves can decrypt it.

func (*PDFService) SetSaveOptions added in v1.1.0

func (s *PDFService) SetSaveOptions(id string, opts SaveOptions) error

SetSaveOptions applies opts to the next SavePDF or SavePDFAs call of the document only, whether it succeeds or not.

func (*PDFService) UndoPageOp added in v1.1.0

func (s *PDFService) UndoPageOp(id string) (*Document, error)

UndoPageOp takes back the newest working copy; RedoPageOp can bring it back until the next operation.

type PageDimensions

type PageDimensions struct {
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

PageDimensions represents the dimensions of a PDF page

type PageInfo

type PageInfo struct {
	PageNumber int    `json:"pageNumber"`
	Width      int    `json:"width"`
	Height     int    `json:"height"`
	ImageData  string `json:"imageData"` // Base64 encoded PNG
}

PageInfo contains information about a rendered PDF page including dimensions and image data.

type RecentFile

type RecentFile struct {
	FilePath   string    `json:"filePath"`
	FileName   string    `json:"fileName"`
	LastOpened time.Time `json:"lastOpened"`
	PageCount  int       `json:"pageCount"`
}

RecentFile represents a recently opened PDF file

type RecentFilesService

type RecentFilesService struct {
	// contains filtered or unexported fields
}

RecentFilesService tracks recently opened PDF files with persistence.

func NewRecentFilesService

func NewRecentFilesService() *RecentFilesService

NewRecentFilesService creates a new recent files service

func (*RecentFilesService) AddRecent

func (s *RecentFilesService) AddRecent(filePath string, pageCount int) error

AddRecent adds or moves a file to the top of the recent files list.

func (*RecentFilesService) ClearRecent

func (s *RecentFilesService) ClearRecent() error

ClearRecent removes all entries from the recent files list.

func (*RecentFilesService) GetRecent

func (s *RecentFilesService) GetRecent() []RecentFile

GetRecent returns recent files, automatically removing entries for deleted files.

func (*RecentFilesService) RemoveRecent

func (s *RecentFilesService) RemoveRecent(filePath string) error

RemoveRecent removes a specific file from the recent files list.

func (*RecentFilesService) ServiceStartup added in v1.0.0

ServiceStartup loads persisted recent files. Called by Wails on app start.

func (*RecentFilesService) Shutdown

func (s *RecentFilesService) Shutdown(timeout time.Duration) error

Shutdown waits for pending saves to complete with the given timeout.

type Renderer added in v1.0.0

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer rasterises PDF pages with MuPDF (go-fitz) for the CLI's info, pages, render and thumbnail commands. The GUI renders with pdf.js instead.

func NewRenderer added in v1.0.0

func NewRenderer() *Renderer

NewRenderer creates a MuPDF renderer with no document open.

func (*Renderer) ClosePDF added in v1.0.0

func (s *Renderer) ClosePDF() error

ClosePDF closes the currently open PDF document and releases resources.

func (*Renderer) GenerateThumbnail added in v1.0.0

func (s *Renderer) GenerateThumbnail(filePath string, maxWidth int) (string, error)

GenerateThumbnail generates a 16:9 cropped thumbnail of the first page as base64 PNG.

func (*Renderer) GetPageCount added in v1.0.0

func (s *Renderer) GetPageCount() int

GetPageCount returns the total number of pages in the open PDF.

func (*Renderer) GetPageDimensions added in v1.0.0

func (s *Renderer) GetPageDimensions(pageNum int) (*PageDimensions, error)

GetPageDimensions returns the width and height of a page in PDF points. Holds an exclusive lock because MuPDF's fz_context is not safe for concurrent access — see also GetMetadata.

func (*Renderer) OpenPDFByPath added in v1.0.0

func (s *Renderer) OpenPDFByPath(filePath string) (*PDFMetadata, error)

OpenPDFByPath opens a PDF file from the given path and returns its metadata.

func (*Renderer) RenderPage added in v1.0.0

func (s *Renderer) RenderPage(pageNum int, dpi float64) (*PageInfo, error)

RenderPage renders the specified page at the given DPI and returns base64-encoded PNG data.

type SaveOptions added in v1.1.0

type SaveOptions struct {
	// Rewrite has Go rewrite the bytes instead of writing them as they are:
	// decrypted with the document's password (see SetPassword) if they are
	// encrypted, optimized if asked, then encrypted with Password if set.
	Rewrite bool `json:"rewrite"`
	// Password protects the rewritten file (AES-256) as both the user and
	// owner password.
	Password string `json:"password"`
	// Optimize applies pdfcpu's size optimizations in the rewrite.
	Optimize bool `json:"optimize"`
}

SaveOptions change how the next save of a document is written.

type SaveTarget added in v1.0.0

type SaveTarget struct {
	Path   string `json:"path"`
	Exists bool   `json:"exists"`
}

SaveTarget is a destination picked in the Save As dialog.

Jump to

Keyboard shortcuts

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