TPdf
Source: source/classes/tpdf.prg
Standalone class (uses libharu / hbhpdf.lib)
TPdf provides PDF document generation using the Haru Free PDF Library (libharu). It supports creating multi-page PDF documents with text, lines, rectangles, images, and fonts. Documents can be password-protected with configurable permissions.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
hPdf | Handle | libharu PDF document handle |
hPage | Handle | Current page handle |
aPages | Array | Array of all page handles in the document |
nPage | Numeric | Current page number |
nPageSize | Numeric | Page size constant (default: HPDF_PAGE_SIZE_A4) |
nOrientation | Numeric | HPDF_PAGE_PORTRAIT or HPDF_PAGE_LANDSCAPE |
nHeight | Numeric | Current page height in points |
nWidth | Numeric | Current page width in points |
cFileName | Character | Output PDF file name |
cPassword | Character | User password for PDF encryption |
cOwnerPassword | Character | Owner password for PDF permissions |
nPermission | Numeric | PDF permission flags |
lPreview | Logical | If .T., open PDF after save |
hImageList | Hash | Loaded image cache |
LoadedFonts | Array | Array of loaded font handles |
Methods
| Method | Description |
|---|---|
New( cFileName, cPassword, cOwnerPassword, nPermission, lPreview ) | Create a new PDF document |
SetPage( nPageSize ) | Set page size (e.g., HPDF_PAGE_SIZE_A4, HPDF_PAGE_SIZE_LETTER) |
SetLandscape() | Switch page orientation to landscape |
SetPortrait() | Switch page orientation to portrait |
SetCompression( cMode ) | Set PDF compression mode |
StartPage() | Start a new page (adds to document) |
EndPage() | Finalize the current page |
Say( nRow, nCol, cText, oFont, nWidth, nClrText, nBkMode, nPad ) | Draw text at position in points |
CmSay( nRow, nCol, cText, oFont, nWidth, nClrText, nBkMode, nPad, lO2A ) | Draw text at position in centimeters |
SayRotate( nTop, nLeft, cTxt, oFont, nClrText ) | Draw rotated text |
DefineFont( cFontName, nSize, lEmbed ) | Define and load a TrueType font |
AddFont( cFontName, cTtfFile ) | Register a TrueType font for embedding |
Line( nTop, nLeft, nBottom, nRight, oPen ) | Draw a line in points |
CmLine( nTop, nLeft, nBottom, nRight, oPen ) | Draw a line in centimeters |
Rect( nTop, nLeft, nBottom, nRight, oPen, nColor ) | Draw a rectangle in points |
CmRect( nTop, nLeft, nBottom, nRight, oPen, nColor ) | Draw a rectangle in centimeters |
RoundBox( nTop, nLeft, nBottom, nRight, nWidth, nHeight, oPen, nColor, nBackColor, lFondo ) | Draw a rounded rectangle |
DashLine( nTop, nLeft, nBottom, nRight, oPen ) | Draw a dashed line |
SayBitmap( nRow, nCol, xBitmap, nWidth, nHeight ) | Place a bitmap image on the page |
CmSayBitmap( nRow, nCol, xBitmap, nWidth, nHeight, nRaster, lStrech ) | Place a bitmap using centimeter coordinates |
GetImageFromFile( cImageFile ) | Load an image from file for use on a page |
GetTextWidth( cText, oFont ) | Measure text width in points |
Save( cFileName ) | Save the PDF document to file |
End() | Close and release PDF resources |
Example: Create a PDF with Text and Shapes
#include "FiveWin.ch"
#include "harupdf.ch"
function Main()
local oPdf, oFont
oPdf := TPdf():New( "output.pdf" )
// Set up the first page
oPdf:SetPage( HPDF_PAGE_SIZE_A4 )
oPdf:StartPage()
// Define a font
oFont := oPdf:DefineFont( "Courier", 12 )
// Draw text
oPdf:Say( 100, 100, "Hello, PDF World!", oFont )
// Draw a rectangle
oPdf:Rect( 80, 80, 300, 150, , CLR_RED )
// Draw a line
oPdf:Line( 80, 80, 300, 150 )
oPdf:EndPage()
// Second page with landscape orientation
oPdf:SetPage( HPDF_PAGE_SIZE_A4 )
oPdf:SetLandscape()
oPdf:StartPage()
oPdf:Say( 100, 100, "This is landscape", oFont )
oPdf:EndPage()
oPdf:Save()
oPdf:End()
return nil
Notes
- TPdf requires linking
hbhpdf.libandlibhpdf.libfrom the FWH lib folder. - Coordinates are in points (1/72 inch) by default. Use
CmSay,CmLine,CmRectfor centimeter-based coordinates. - Font embedding is supported via
AddFont()andDefineFont( cFontName, nSize, lEmbed ). - Password protection with permissions is set via the constructor. Permissions include
HPDF_ENABLE_READ,HPDF_ENABLE_PRINT,HPDF_ENABLE_COPY, andHPDF_ENABLE_EDIT. - TrueType fonts from the system
Fontsdirectory are auto-detected and available by face name.