Create Unicode Text, Embed CID in PDFs - PHP Sample Code

Sample code for using Apryse SDK to create Unicode text and embed composite fonts in PDF files. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. Learn more about our Server SDK.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14//---------------------------------------------------------------------------------------
15// This example illustrates how to create Unicode text and how to embed composite fonts.
16//
17// Note: This demo assumes that 'arialuni.ttf' is present in '/Samples/TestFiles'
18// directory. Arial Unicode MS is about 24MB in size and it comes together with Windows and
19// MS Office.
20//---------------------------------------------------------------------------------------
21function main()
22{
23 global $input_path, $output_path, $LicenseKey;
24
25 PDFNet::Initialize($LicenseKey);
26 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
27
28 $doc = new PDFDoc();
29
30 $builder = new ElementBuilder();
31 $writer = new ElementWriter();
32
33 // Start a new page ------------------------------------
34 $page = $doc->PageCreate(new Rect(0.0, 0.0, 612.0, 794.0));
35
36 $writer->Begin($page); // begin writing to this page
37
38 // Embed and subset the font
39 $font_program = $input_path."ARIALUNI.TTF";
40 if (!file_exists($font_program)) {
41 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
42 $font_program = "C:/Windows/Fonts/ARIALUNI.TTF";
43 }
44 }
45 $fnt = NULL;
46 try {
47 $fnt = Font::CreateCIDTrueTypeFont($doc->GetSDFDoc(), $font_program, true, true);
48 }
49 catch(Exception $e){
50
51 }
52 if($fnt)
53 {
54 echo(nl2br("Note: using " . $font_program . " for unshaped unicode text\n"));
55 }
56 else
57 {
58 echo(nl2br("Note: using system font substitution for unshaped unicode text\n"));
59 $fnt = Font::Create($doc->GetSDFDoc(), "Helvetica", "");
60 }
61
62 $element = $builder->CreateTextBegin($fnt, 1.0);
63 $element->SetTextMatrix(10.0, 0.0, 0.0, 10.0, 50.0, 600.0);
64 $element->GetGState()->SetLeading(2); // Set the spacing between lines
65 $writer->WriteElement($element);
66
67 // Hello World!
68 $hello = array( 'H','e','l','l','o',' ','W','o','r','l','d','!');
69 $writer->WriteElement($builder->CreateUnicodeTextRun($hello, count($hello)));
70 $writer->WriteElement($builder->CreateTextNewLine());
71
72 // Latin
73 $latin = array(
74 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 0x45, 0x0046, 0x00C0,
75 0x00C1, 0x00C2, 0x0143, 0x0144, 0x0145, 0x0152, '1', '2' // etc.
76 );
77 $writer->WriteElement($builder->CreateUnicodeTextRun($latin, count($latin)));
78 $writer->WriteElement($builder->CreateTextNewLine());
79
80 // Greek
81 $greek = array(
82 0x039E, 0x039F, 0x03A0, 0x03A1,0x03A3, 0x03A6, 0x03A8, 0x03A9 // etc.
83 );
84 $writer->WriteElement($builder->CreateUnicodeTextRun($greek, count($greek)));
85 $writer->WriteElement($builder->CreateTextNewLine());
86
87 // Cyrillic
88 $cyrillic = array(
89 0x0409, 0x040A, 0x040B, 0x040C, 0x040E, 0x040F, 0x0410, 0x0411,
90 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419 // etc.
91 );
92 $writer->WriteElement($builder->CreateUnicodeTextRun($cyrillic, count($cyrillic)));
93 $writer->WriteElement($builder->CreateTextNewLine());
94
95 // Hebrew
96 $hebrew = array(
97 0x05D0, 0x05D1, 0x05D3, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8,
98 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF, 0x05E0, 0x05E1 // etc.
99 );
100 $writer->WriteElement($builder->CreateUnicodeTextRun($hebrew, count($hebrew)));
101 $writer->WriteElement($builder->CreateTextNewLine());
102
103 // Arabic
104 $arabic = array(
105 0x0624, 0x0625, 0x0626, 0x0627, 0x0628, 0x0629, 0x062A, 0x062B, 0x062C,
106 0x062D, 0x062E, 0x062F, 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635 // etc.
107 );
108 $writer->WriteElement($builder->CreateUnicodeTextRun($arabic, count($arabic)));
109 $writer->WriteElement($builder->CreateTextNewLine());
110
111 // Thai
112 $thai = array(
113 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09,
114 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12 // etc.
115 );
116 $writer->WriteElement($builder->CreateUnicodeTextRun($thai, count($thai)));
117 $writer->WriteElement($builder->CreateTextNewLine());
118
119 // Hiragana - Japanese
120 $hiragana = array(
121 0x3041, 0x3042, 0x3043, 0x3044, 0x3045, 0x3046, 0x3047, 0x3048, 0x3049,
122 0x304A, 0x304B, 0x304C, 0x304D, 0x304E, 0x304F, 0x3051, 0x3051, 0x3052 // etc.
123 );
124 $writer->WriteElement($builder->CreateUnicodeTextRun($hiragana, count($hiragana)));
125 $writer->WriteElement($builder->CreateTextNewLine());
126
127 // CJK Unified Ideographs
128 $cjk_uni = array(
129 0x5841, 0x5842, 0x5843, 0x5844, 0x5845, 0x5846, 0x5847, 0x5848, 0x5849,
130 0x584A, 0x584B, 0x584C, 0x584D, 0x584E, 0x584F, 0x5850, 0x5851, 0x5852 // etc.
131 );
132 $writer->WriteElement($builder->CreateUnicodeTextRun($cjk_uni, count($cjk_uni)));
133 $writer->WriteElement($builder->CreateTextNewLine());
134
135 // Simplified Chinese
136 $chinese_simplified = array(
137 0x4e16, 0x754c, 0x60a8, 0x597d
138 );
139 $writer->WriteElement($builder->CreateUnicodeTextRun($chinese_simplified, count($chinese_simplified)));
140 $writer->WriteElement($builder->CreateTextNewLine());
141
142 echo("Now using text shaping logic to place text\n");
143
144 // Create a font in indexed encoding mode
145 // normally this would mean that we are required to provide glyph indices
146 // directly to CreateUnicodeTextRun, but instead, we will use the GetShapedText
147 // method to take care of this detail for us.
148 $indexed_font = Font::CreateCIDTrueTypeFont($doc->GetSDFDoc(), $input_path . "NotoSans_with_hindi.ttf", true, true, Font::e_Indices);
149 $element = $builder->CreateTextBegin($indexed_font, 10.0);
150 $writer->WriteElement($element);
151
152 $line_pos = 350.0;
153 $line_space = 20.0;
154
155 // Transform unicode text into an abstract collection of glyph indices and positioning info
156 $shaped_text = $indexed_font->GetShapedText("Shaped Hindi Text:");
157
158 // transform the shaped text info into a PDF element and write it to the page
159 $element = $builder->CreateShapedTextRun($shaped_text);
160 $element->SetTextMatrix(1.5, 0.0, 0.0, 1.5, 50.0, $line_pos);
161 $writer->WriteElement($element);
162
163 # read in unicode text lines from a file
164 $f = fopen($input_path . "hindi_sample_utf16le.txt", "r");
165 $i = 0;
166 while($hindi_text = fgets($f)){$i++;}
167 fclose($f);
168 echo("Read in " . $i . " lines of Unicode text from file\n");
169
170 $f = fopen($input_path . "hindi_sample_utf16le.txt", "r");
171 $i = 0;
172 while($hindi_text = fgets($f)){
173 if ($i == 0)
174 $tmp1 = substr($hindi_text,0,-1);
175 else if($i == 1)
176 $tmp1 = substr($hindi_text,1,-2); // remove the first and the last 2 characters so encoding to UTF-8 looks correct in PHP
177 $tmp = iconv($in_charset = "UTF-16LE", $out_charset="UTF-8", $tmp1);
178 $shaped_text = $indexed_font->GetShapedText($tmp);
179 $element = $builder->CreateShapedTextRun($shaped_text);
180 $element->SetTextMatrix(1.5, 0.0, 0.0, 1.5, 50.0, $line_pos-$line_space*($i+1));
181 $writer->WriteElement($element);
182 echo("Wrote shaped line to page\n");
183 $i++;
184
185 }
186 fclose($f);
187
188 // Finish the block of text
189 $writer->WriteElement($builder->CreateTextEnd());
190
191 $writer->End(); // save changes to the current page
192 $doc->PagePushBack($page);
193
194 $doc->Save($output_path."unicodewrite.pdf", SDFDoc::e_remove_unused | SDFDoc::e_hex_strings);
195 PDFNet::Terminate();
196 echo "Done. Result saved in unicodewrite.pdf...\n";
197}
198
199main();
200?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales