Khmer words are not separated by spaces in the same way English words are. Standard PDF libraries often rely on whitespace to determine where to break a line. This can result in long Khmer sentences running off the edge of the page.
Before diving into PDF resources, it is crucial to understand why Flutter is worth your time.
SlaK is a local platform that aggregates Khmer tech articles. Some authors compile their blog series into downloadable PDFs for offline reading. A search for "Flutter" on SlaK often yields links to Google Drive PDFs. flutter khmer pdf
The pdf package requires you to load the font as bytes. Here is the critical part: You must pass the true flag for useFontShaping to allow the underlying HarfBuzz shaping engine to process Khmer correctly.
import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart' as pw; import 'package:flutter/services.dart' show rootBundle;Future<Uint8List> generateKhmerPdf() async // 1. Load Khmer font final fontData = await rootBundle.load('assets/fonts/Khmer_OS_Battambang.ttf'); final ttf = pw.Font.ttf(fontData.buffer.asByteData()); Khmer words are not separated by spaces in
// 2. Create PDF with custom font final pdf = pw.Document();
pdf.addPage( pw.Page( build: (pw.Context context) return pw.Center( child: pw.Text( 'សួស្តីពិភពលោក! នេះជា PDF ជាភាសាខ្មែរ។', // "Hello World! This is a PDF in Khmer." style: pw.TextStyle(font: ttf, fontSize: 24), ), ); , ), ); Before diving into PDF resources, it is crucial
return pdf.save();