Hello Marzio,
The issue lies in the font selection, not strictly your coding logic.
In the PDF standard, the "Standard 14 fonts" (like Courier, Helvetica, Times) are usually limited to ANSI encodings (WinAnsiEncoding). They do not contain the glyphs required to render UTF-8 characters like Cyrillic, Chinese, or Arabic.
To display Cyrillic characters in a PDF, you must use an external TrueType Font (.ttf) that contains those specific Unicode glyphs, and it must be embedded into the PDF.
Here is the solution to fix your function.
The Solution: Use a .TTF File
Instead of asking for "Courier", you need to point the function to the actual path of a physical font file (like Arial or Courier New) that supports Unicode.
Here is the corrected code:
function PdfGen()
local oPrn, oFont
local cCyrillicFont := "C:\Windows\Fonts\arial.ttf" // Use a full path to a TTF
// 1. Ensure Unicode is ON
FW_SetUnicode( .t. )
HB_SETCODEPAGE( "UTF8" )
oPrn := FWPdf():New( cFileSetExt( ExeName(), "pdf" ) )
oPrn:lUnicode := .t.
oPrn:lPreview := .t.
// 2. DEFINE FONT using the .TTF filename, not just the name "Courier"
// Note: We use -10 for height to ensure scaling works correctly with TTFs in some versions
if File( cCyrillicFont )
oFont := oPrn:DefineFont( cCyrillicFont, 10 )
else
MsgStop( "Font file not found: " + cCyrillicFont )
return nil
endif
if Empty( oPrn:hPdf )
MsgStop("PDFs not available!")
Return( NIL )
endif
// 3. Ensure your text file is saved as UTF-8 NO BOM (Byte Order Mark)
// Or sanitize it after reading.
cText := MemoRead("utf8.txt")
// Optional: Remove BOM if it exists at the start of the file
if Left( cText, 3 ) == Chr(239) + Chr(187) + Chr(191)
cText := SubStr( cText, 4 )
endif
oPrn:StartPage()
oPrn:Say( 1, 1, cText, oFont )
oPrn:EndPage()
oPrn:end()
Return( NIL )
Key Changes Explained
- DefineFont( "Path\To\File.ttf", ... ):
When FWPdf sees a filename extension (.ttf), it switches from using internal standard fonts to embedding the external font file. This embedding allows the PDF viewer to "see" the Cyrillic shapes.
- Font Choice:
"Courier" (the standard PDF font) does not have Cyrillic characters. "Arial.ttf" or "Courier New.ttf" (found in Windows/Fonts) usually includes the full WGL4 character set, including Cyrillic.
- BOM Handling:
If you edit utf8.txt in Notepad, it might add a "BOM" (3 invisible bytes) at the start. I added a small check to remove them, otherwise, the first character might look like garbage.
Summary Checklist
- [ ] Does C:\Windows\Fonts\arial.ttf exist on your machine?
- [ ] Is utf8.txt actually saved as UTF-8 encoding?
- [ ] Did you use the full path to the .ttf in DefineFont?
-----