$text->utf8, $lines))); // The CID program retains every glyph. The specification requires removing cmap // for FontFile2/CIDFontType2; mappings remain in our PDF objects. // The sfnt directory and checksums are recalculated without glyph subsetting. // Length1 measures embedded-program bytes after filter decoding. // StemV is explicitly supplied (80 PDF units in this example), because // TrueType has no universal field giving vertical stem thickness. // The Symbolic flag suits this repertoire containing Greek (Table 123). $cidProgram = new CIDFontProgram($program); $fontFile = new FontFile2($cidProgram->bytes, new FlateDecode()); $map = (new CharacterMapFactory($cidProgram))->create($fontFile, $repertoire); $encoder = new TextEncoder($map); $descriptor = (new FontDescriptorFactory($cidProgram))->create($fontFile, new FontDescriptorFlags(4), new NumericType(80)); $glyphMap = new CIDToGIDMap($map, new FlateDecode()); $widths = new CIDWidths($map); $descendant = new CIDFontType2($descriptor, $glyphMap, $widths); $toUnicode = new ToUnicodeCMap($map, new FlateDecode()); $font = new Type0Font($descendant, $toUnicode); $resources = new ResourceDictionary(new Font(new DictionaryType( new Entry(new NameType('F1'), new IndirectReference($font)), ))); // Identity-H expects two-byte codes, not the original UTF-8 string. // The encoder supplies these codes; ToUnicode supplies the reverse mapping // for selection, search and extraction in PDF readers. $contents = new StreamObject(new ContentStreams( new BeginTextObject(), new SetTextFontAndSize(new NameType('F1'), new NumericType(22)), new MoveTextPosition(new NumericType(48), new NumericType(748)), new ShowText($encoder->encode($lines[0])), new SetTextFontAndSize(new NameType('F1'), new NumericType(14)), new MoveTextPosition(new NumericType(0), new NumericType(-42)), new ShowText($encoder->encode($lines[1])), new MoveTextPosition(new NumericType(0), new NumericType(-28)), new ShowText($encoder->encode($lines[2])), new EndTextObject(), )); $page = new PageObject( new Rectangle(new NumericType(0), new NumericType(0), new NumericType(595), new NumericType(842)), $resources, $contents, ); $pages = (new PageTreeNode())->addKid($page); $catalog = new DocumentCatalog($pages); $file = new FileStructure(new FileHeader('1.7'), new FileBody($catalog), new IndirectReference($catalog)); $path = $argv[1] ?? __DIR__ . '/embedded-font.pdf'; $file->writeTo($path); echo $path . PHP_EOL;