DEV Community

Jehadur Rahman Emran
Jehadur Rahman Emran

Posted on

Bengali in a Flutter PDF needs HarfBuzz, not just a Unicode font

Embedding a Bengali Unicode font in a PDF is not enough. Conjuncts and dependent vowel marks need OpenType GSUB/GPOS shaping. If you skip that step, the glyphs look wrong, and search or copy often break because the visual run no longer matches the logical Unicode.

bengali_pdf is a Flutter library I built for that gap. It is a substantially reworked fork of zamansheikh/bangla_pdf, and the original copyright is kept. It shapes each run with HarfBuzz using the same font face that is embedded, paints the returned glyph IDs and positions into a Type 0 PDF font, and places an invisible logical Unicode layer on top so viewers can select, copy, search, and highlight.

What 0.1.0 actually covers

  • Native platforms only: Android, iOS, Linux, macOS, and Windows, through package:harfbuzz_ffi.
  • Web is deliberately out of this release. There is no equivalent WebAssembly shaping backend in this version, and I will not claim web support.
  • Bundled Noto Sans Bengali (OFL-1.1), usable offline. You can also load another legally embeddable TTF/OTF.
  • Cluster-safe wrapping that is not supposed to split Bengali conjuncts or combining marks.
  • License: BSD-3-Clause.

The README still calls 0.1.0 a native-platform release candidate. I have not published it to pub.dev. When I do, it will go under the jehadurre.me publisher after a dart pub publish --dry-run. Until that listing exists, install from GitHub, not from a pub.dev version I have not shipped.

The API, from the README

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();
Enter fullscreen mode Exit fullscreen mode

The example string is taken from the public README. I am not inventing a generated-PDF screenshot here.

If you generate Bengali PDFs in Flutter on mobile or desktop, the repo is here: https://github.com/JehadurRE/bengali_pdf

Top comments (1)

Collapse
 
theomnikit profile image
TheOmniKit •

good that you kept the invisible logical layer, that's the part most people skip. one thing worth testing is the pre-base vowel signs like ি and ে, visually they sit before the consonant but in unicode they come after, so a copy test with কি and কে is a quick way to see if your layer order is right. also reph (র্ক) and ক্ষ, try searching them in chrome's viewer and in pdf.js because both treat the text layer a bit differently. did you check ZWNJ cases too?