TPDFlibPrinter

Printing, VCL, document creation

Description

Provides a component facade with the familiar BeginDoc, Canvas, NewPage, EndDoc, and Abort lifecycle while creating a PDF document instead of submitting a physical printer job

Declaration

Uses PDFlibPDFPrinter;

TPDFlibPrinter = Class(TComponent)

Core methods

BeginDocStarts a new PDF document and exposes the first page through Canvas
NewPageCommits the current canvas as a PDF page and opens the next page canvas
EndDocCommits the final page and writes FileName when a destination was assigned
AbortDiscards the active job without writing its destination file and resets the component to an empty document
SetPageSizeSets portrait paper dimensions in PDF points before BeginDoc

Properties

CanvasThe active VCL canvas between BeginDoc and EndDoc or Abort
FileNameOptional PDF output path; an empty value retains the result only in LibraryInstance
TitleDocument title applied when the job begins
DPICanvas coordinate resolution from 24 through 2400, defaulting to 96
CanvasOptionsCanvas import mode from -1 through 4, defaulting to vector mode 2
OrientationpoPortrait or poLandscape; landscape swaps the configured paper dimensions
PaperWidth, PaperHeightPortrait paper dimensions from 1 through 14400 PDF points
PageWidth, PageHeightRead-only active dimensions in canvas pixels after DPI and orientation conversion
PageWidthPoints, PageHeightPointsRead-only active dimensions in PDF points
PageNumberOne-based current page during a job and zero while idle
Printing, AbortedRead-only lifecycle state
LibraryInstanceThe owned TPDFlib instance for inspecting or further processing the completed PDF
LastErrorCodeThe underlying library error code from the most recent PDF operation

Example

Printer := TPDFlibPrinter.Create(nil);
try
  Printer.FileName := 'report.pdf';
  Printer.BeginDoc;
  Printer.Canvas.TextOut(72, 72, 'First page');
  Printer.NewPage;
  Printer.Canvas.TextOut(72, 72, 'Second page');
  Printer.EndDoc;
finally
  Printer.Free;
end;

Remarks

Configuration properties cannot change during an active document and lifecycle misuse raises EPDFlibPrinter

The component owns its library and canvas, creates an independent document for each job, preserves a completed document for further API operations, and never opens a print dialog or accesses the operating-system spooler

Use try/except around a job and call Abort only while the job remains active; failed internal conversion operations terminate the job and set Aborted

See also

GetCanvasDC, NewPageFromCanvasDC, RenderPagesToPrintStream