bengali_pdf

Important

Version 0.1.0 is a native-platform release candidate. Bengali text is shaped with HarfBuzz and emitted through a Type 0 PDF font plus an invisible logical Unicode layer for selection, copying, search, and highlight geometry. Human visual review is complete. Windows is build-validated; Android, iOS, Linux, and macOS are validated by dedicated target CI jobs. Web remains deliberately outside the native release and is tracked separately until an equivalent WebAssembly shaping backend is ready.

HarfBuzz-shaped Bengali PDF text maintained by JehadurRE. The native pipeline preserves the original Unicode for selection, copying, and search while painting the GSUB/GPOS glyph IDs and positions required by Bengali.

Highlights

  • Bundled Noto Sans Bengali variable font (OFL-1.1), usable offline.
  • Load any legally embeddable TTF/OTF from bytes or Flutter assets.
  • Deterministic font cache with concurrent-load deduplication.
  • Mixed Bengali/English/digits/punctuation segmentation without destructive ANSI conversion.
  • BengaliPdfTextBuilder for reusable shaped widgets without duplicate font embedding.
  • Deprecated nominal-glyph widgets remain available only for source compatibility; production Bengali output should use the shaped builder.
  • Explicit composition with package:pdf widgets for Latin or other scripts.
  • Native HarfBuzz backend on Android, iOS, Linux, macOS, and Windows.
  • Searchable Type 0 PDF embedding with explicit CID-to-GID plus an invisible Unicode text layer that provides normal selection and search geometry.
  • Cluster-safe wrapping that never splits Bengali conjuncts or combining marks.

Shaping architecture

A Bengali Unicode font alone is insufficient. Bengali conjuncts and dependent vowel marks require OpenType GSUB/GPOS shaping. bengali_pdf shapes each run with HarfBuzz using the exact font face that is embedded in the PDF, then paints the returned glyph IDs, advances, and offsets. An invisible Unicode anchor is aligned to each shaped cluster. Viewers use that logical layer for selection, copying, extraction, search, and visible search highlights, while the independently positioned Type 0 glyphs remain the rendered output.

The native implementation is available on Android, iOS, Linux, macOS, and Windows through package:harfbuzz_ffi. Web is deliberately excluded from the native 0.1.0 platform declaration and tracked as a separate backend. The current Dart HarfBuzz ecosystem remains FFI based; no mature WASM package yet satisfies the same in-memory font, glyph ID, cluster, advance, and offset contract. This does not block the native release. Legacy ANSI conversion is not used.

API

dependencies:
  bengali_pdf: ^0.1.0
  pdf: ^3.13.0

Load the bundled offline font before constructing PDF pages:

import 'package:bengali_pdf/bengali_pdf.dart';
import 'package:pdf/widgets.dart' as pw;

final registry = BengaliFontRegistry();
final noto = await BundledBengaliFonts.loadNotoSansBengali(
  registry: registry,
);

final text = BengaliPdfTextBuilder(font: noto);
final bengali = await text.build('বাংলাদেশ — মূল্য ৳৫০০।', fontSize: 16);
final document = pw.Document();
document.addPage(
  pw.Page(
    build: (_) => pw.Row(
      children: [pw.Text('Hello '), bengali],
    ),
  ),
);
final bytes = await document.save();

Custom font bytes

final provider = MemoryBengaliFontProvider(fontBytes);
const descriptor = BengaliFontDescriptor(
  family: 'My Bengali Font',
  source: 'app:my-font',
);
final custom = await registry.load(descriptor, provider);

Flutter asset font

const descriptor = BengaliFontDescriptor(
  family: 'My Asset Font',
  source: 'asset:my-font',
);
final custom = await registry.load(
  descriptor,
  const AssetBengaliFontProvider('assets/fonts/my_bengali_font.ttf'),
);

Declare application-owned assets in the consuming app's pubspec.yaml.

Emoji and multilingual fallback

final bengali = await shapeBengaliText('বাংলা', font: noto);
// Compose BengaliShapedText beside ordinary package:pdf widgets for scripts
// covered by other fonts. Full bidirectional itemization is planned separately.
pw.Row(children: [bengali, pw.Text(' English 2026')]);

Font support model

Source Offline Bundled Notes
Noto Sans Bengali Yes Yes Unmodified variable font; OFL-1.1
Custom bytes Yes No TTF/OTF signature validated
Flutter asset Yes No Consumer controls licensing and asset declaration
Network/Google Fonts Optional No Fetch externally, cache deliberately, then use memory provider
Legacy ANSI font Font-specific No Requires an audited compatibility profile; not interchangeable with Unicode fonts

The core package deliberately performs no hidden network calls. This makes PDF creation deterministic, private, and suitable for offline environments.

Migration from bangla_pdf

The original package exported generic Text, Table, RichText, and TextSpan names and applied Kalpurush-specific ANSI conversion to Bengali text. This fork uses explicit fonts and namespaced widgets:

// Before
// Text('বাংলা');

// Now
final text = await shapeBengaliText('বাংলা', font: noto);

A temporary compatibility entrypoint is available:

import 'package:bengali_pdf/legacy.dart';

Legacy wrappers require an explicit banglaFont and are deprecated. The main library never exports collision-prone names.

Verification

flutter pub get
dart format --output=none --set-exit-if-changed .
flutter analyze
flutter test
flutter pub publish --dry-run

The native validation generator and Flutter example exercise real HarfBuzz shaping, cluster-safe wrapping, Type 0 glyph embedding, and /ToUnicode metadata. Run the manual generator with:

flutter test test/manual/generate_shaped_validation_pdf_test.dart

It writes outputs/bengali_pdf_harfbuzz_validation.pdf. Reuse one BengaliPdfTextBuilder for multiple widgets in the same document so the source font is embedded only once.

Note

Human review of the validation PDF confirmed conjuncts, vowel signs, reph/ra forms, wrapping, and mixed content. Each shaped cluster carries an invisible Unicode anchor as wide as the cluster, so extraction and interaction get a normal text-run geometry; visible shaped glyph CIDs are zero-width and map to U+200B to prevent duplicate extraction. CI checks the regression fixture with Poppler (pdftotext), PDFium (the engine in Chrome's viewer), and pdf.js (Firefox's viewer), including reph and ZWNJ cases.

Licensing and attribution

Package code is BSD-3-Clause. This project is a substantially reworked fork of zamansheikh/bangla_pdf; the original copyright is retained. The bundled Noto font remains under OFL-1.1. See LICENSE, NOTICE, and assets/fonts/noto_sans_bengali/OFL.txt.

Copyright (c) 2026 JehadurRE.

Libraries

bangla_pdf
Deprecated import shim for applications migrating from bangla_pdf.
bengali_pdf
Professional Bengali text and font support for package:pdf.
legacy
Deprecated compatibility names for applications migrating from the former package.