PDFium Delphi Component Docs

Bounded random access storage

Component: TPdf  ·  Unit: PDFium
Keeps seekable working data under a caller-selected memory ceiling and spills larger content to private temporary storage

Syntax

const PdfDefaultStorageMemoryLimit = 64 * 1024 * 1024;

type TPdfStorageBackend = (psbMemory, psbTemporaryFile);

constructor TPdfRandomAccessStore.Create(AMemoryLimit: Int64 = PdfDefaultStorageMemoryLimit; const ATemporaryDirectory: string = '');

destructor Destroy; override;

function Read(var Buffer; Count: Longint): Longint; override;

function Seek(Offset: Int64; Origin: TSeekOrigin): Int64; override;

function Write(const Buffer; Count: Longint): Longint; override;

function ReadAt(Offset: Int64; var Buffer; Count: Longint): Longint;

function WriteAt(Offset: Int64; const Buffer; Count: Longint): Longint;

procedure CopyToStream(Destination: TStream; BufferSize: Integer = 1048576);

procedure Flush;

procedure SpillToDisk;

property Backend: TPdfStorageBackend;

property MemoryBytes: Int64;

property MemoryLimit: Int64;

property PeakMemoryBytes: Int64;

property Spilled: Boolean;

property TemporaryDirectory: string;

property TemporaryFileName: string;

TPdf save settings

property SaveMemoryLimit: Int64;

property SaveTemporaryDirectory: string;

property LastSaveSpillCount: Integer;

property LastSaveLargestMemoryBuffer: Int64;

SaveMemoryLimitMaximum backing-memory capacity of each internal save store before it spills; defaults to 64 MiB
SaveTemporaryDirectoryExisting directory used for spill files; an empty value uses the process temporary directory
LastSaveSpillCountNumber of internal working stores that spilled during the most recent save pipeline
LastSaveLargestMemoryBufferLargest single backing-memory capacity observed during the most recent save pipeline

Store members

CreateBuilds the store with a ceiling between zero and High(Integer), defaulting to PdfDefaultStorageMemoryLimit, and an optional spill directory that must already exist when non-empty
DestroyZeroes and releases the memory buffer, closes the exclusive spill file, and deletes it from disk
ReadTStream override that transfers up to Count bytes from the sequential cursor and advances it
WriteTStream override that writes Count bytes at the sequential cursor, spilling first when the write would end beyond MemoryLimit
SeekMoves the cursor relative to the beginning, current position, or end and returns the new offset; negative results and Int64 overflow raise EPdfError
CopyToStreamCopies the whole logical content from offset zero into Destination in BufferSize chunks; nil or self destinations and non-positive buffer sizes raise EPdfError
FlushPushes an open spill file’s buffered bytes to the storage device and raises the last OS error when the flush fails; a memory backend makes the call a no-op
SpillToDiskRequests the one-time migration to the temporary file immediately whilst the store is still memory-resident
BackendReports psbMemory whilst content is RAM-resident and psbTemporaryFile after the spill
SpilledTrue once the store has migrated to its temporary file; the migration never reverses
TemporaryDirectoryDirectory selected at construction for the spill file; an empty value resolves to the process temporary directory

Description

TPdfRandomAccessStore derives from TStream and supports normal sequential reads, writes, seeks and sizing in addition to cursor-independent positioned I/O

Data remains in a dynamically sized memory buffer whilst its logical end stays at or below MemoryLimit

A store created without an explicit ceiling uses PdfDefaultStorageMemoryLimit, the same 64 MiB default TPdf applies to SaveMemoryLimit

The first operation that crosses the ceiling creates an exclusive temporary file, copies the existing bytes once, scrubs the memory buffer and keeps the file backend for the remainder of the store lifetime

The temporary handle uses delete-on-close and random-access flags, permits no sharing and is removed on normal destruction or constructor rollback

Individual operations are serialised by an internal critical section; ReadAt and WriteAt are preferred when concurrent callers must not compete for the sequential cursor

A store can be passed directly to LoadCustomDocument; PDFium block callbacks use positioned reads and do not alter the store cursor

TPdf uses the configured policy for core saves, every saved-PDF post-processing workspace and intermediate standards conversion, validation, encryption, DSS and PAdES staging

Native incremental xref-stream repair reads fixed-size tail and dictionary windows, then applies validated edits whilst copying unchanged ranges in bounded chunks

Memory accounting

Constraints

Example

var
  Store: TPdfRandomAccessStore;
begin
  Pdf.SaveMemoryLimit := 16 * 1024 * 1024;
  Pdf.SaveTemporaryDirectory := TPath.GetTempPath;
  Pdf.SaveAs('output.pdf');
  Log(Pdf.LastSaveSpillCount, Pdf.LastSaveLargestMemoryBuffer);

  Store := TPdfRandomAccessStore.Create(4 * 1024 * 1024);
  try
    Store.CopyFrom(SourceStream, SourceStream.Size);
    Store.Position := 0;
    Pdf.LoadCustomDocument(Store, False);
  finally
    Pdf.Active := False;
    Store.Free;
  end;
end;

See Also

LoadCustomDocument, SaveAs