Arabic Support for tFPDF (based on fpdf) - php

I am using tFPDF (linked below). I cannot find a way to render text right --> left for Arabic language. It renders fine but it is left to right. Below is the library I am using. I looked at source code and couldn't figure out how to render right to left.
http://www.fpdf.org/en/script/script92.php

Unfortunately it would be too hard to make the tFPDF library work for Arabic. This library only supports Arabic characters in their isolated form, instead of using their initial, medial or final form when they are in an initial, medial or final position in a word. See the document “Text Layout Requirements for the Arabic Script” by the W3C.
Furthermore it does not support true bidirectionality. Have a look at the document “Unicode Bidirectional Algorithm basics”, also by the W3C.
My suggestion is to use another library. The only non-commercial PHP library supporting RTL (right-to-left) scripts that I was able to find is TCPDF. The source code is on GitHub. It's based on the fpdf library just like tFPDF.
To try it out, all you have to do is
git clone https://github.com/tecnickcom/TCPDF.git
cd TCPDF/examples
# this is an example of an RTL script:
php example_018.php > example_018.pdf
open example_018.pdf
Starting from the example files I have put together the following example for Arabic (must be placed in the examples directory):
<?php
require_once('tcpdf_include.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Walter Tross');
$pdf->SetTitle('TCPDF Arabic Example');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'ar'; // 'ar' for Arabic
$lg['w_page'] = 'page';
$pdf->setLanguageArray($lg);
$pdf->SetFont('dejavusans', '', 12); // several fonts in TCPDF/fonts work
$pdf->AddPage();
$txt = <<<EOD
مرحبا يا عالم
hello world
EOD;
$pdf->setRTL(true); // optional here, depends on the desired BASE direction
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0); // 'C' for centered
$pdf->Output('hello_world_in_Arabic.pdf', 'I');
This is what is rendered at the center top of the resulting PDF:
For some reason that I don't have the time to investigate now you have to redirect the output to a file, like with example_018.pdf above, but I'm sure this can be solved. (A quick hack would be to use output buffering.)
Instead of including tcpdf_include.php, which in turn includes all that is needed and much more, you will have to find a reasonable place for what you really need and a reasonable way to include only that.
And, of course, you will have to study a bit the examples in order to put together the code that works for you.
P.S.: I have taken the translation of "hello world" from the Wikipedia entry for the قلب programming language.

use this package to reshape your arabic string
import arabic_reshaper # pip install arabic_reshaper
expression = r"^[a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z]"
if (re.search(expression, yourstring) is None):
pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
pdf.set_font('DejaVu', '', 10)
arabic_string = arabic_reshaper.reshape(yourstring)
arabic_string = arabic_string[::-1]
w = pdf.get_string_width(arabic_string) + 6
pdf.cell(w, 9, arabic_string, 0, 1, 'L', 0)

Related

Can't import most TTF files to TCPDF

I have read all the tutorials there are to read about this topic, I simply can't make this work, neither do the example codes and online tools...
When I try to import most .ttf files into TCPDF fonts, the resulting php file goes something like this:
<?php
$type='TrueType';
$name='FontAwesome5Free-Solid';
$desc=array('Ascent'=>875,'Descent'=>-125,'CapHeight'=>875,'Flags'=>96,'FontBBox'=>'[5000 5000 -5000 -5000]','ItalicAngle'=>-50,'StemV'=>70,'MissingWidth'=>1000);
$up=-123;
$ut=49;
$dw=1000;
$cw=array(
0=>1000,1=>1000,2=>1000,3=>1000,4=>1000,5=>1000,6=>1000,7=>1000,8=>1000,9=>1000,
10=>1000,11=>1000,12=>1000,13=>1000,14=>1000,15=>1000,16=>1000,17=>1000,18=>1000,19=>1000,
20=>1000,21=>1000,22=>1000,23=>1000,24=>1000,25=>1000,26=>1000,27=>1000,28=>1000,29=>1000,
30=>1000,31=>1000,32=>1000,33=>1000,34=>1000,35=>1000,36=>1000,37=>1000,38=>1000,39=>1000,
40=>1000,41=>1000,42=>1000,43=>1000,44=>1000,45=>1000,46=>1000,47=>1000,48=>1000,49=>1000,
50=>1000,51=>1000,52=>1000,53=>1000,54=>1000,55=>1000,56=>1000,57=>1000,58=>1000,59=>1000,
60=>1000,61=>1000,62=>1000,63=>1000,64=>1000,65=>1000,66=>1000,67=>1000,68=>1000,69=>1000,
70=>1000,71=>1000,72=>1000,73=>1000,74=>1000,75=>1000,76=>1000,77=>1000,78=>1000,79=>1000,
80=>1000,81=>1000,82=>1000,83=>1000,84=>1000,85=>1000,86=>1000,87=>1000,88=>1000,89=>1000,
90=>1000,91=>1000,92=>1000,93=>1000,94=>1000,95=>1000,96=>1000,97=>1000,98=>1000,99=>1000,
100=>1000,101=>1000,102=>1000,103=>1000,104=>1000,105=>1000,106=>1000,107=>1000,108=>1000,109=>1000,
110=>1000,111=>1000,112=>1000,113=>1000,114=>1000,115=>1000,116=>1000,117=>1000,118=>1000,119=>1000,
120=>1000,121=>1000,122=>1000,123=>1000,124=>1000,125=>1000,126=>1000,127=>1000,128=>1000,129=>1000,
130=>1000,131=>1000,132=>1000,133=>1000,134=>1000,135=>1000,136=>1000,137=>1000,138=>1000,139=>1000,
140=>1000,141=>1000,142=>1000,143=>1000,144=>1000,145=>1000,146=>1000,147=>1000,148=>1000,149=>1000,
150=>1000,151=>1000,152=>1000,153=>1000,154=>1000,155=>1000,156=>1000,157=>1000,158=>1000,159=>1000,
160=>1000,161=>1000,162=>1000,163=>1000,164=>1000,165=>1000,166=>1000,167=>1000,168=>1000,169=>1000,
170=>1000,171=>1000,172=>1000,173=>1000,174=>1000,175=>1000,176=>1000,177=>1000,178=>1000,179=>1000,
180=>1000,181=>1000,182=>1000,183=>1000,184=>1000,185=>1000,186=>1000,187=>1000,188=>1000,189=>1000,
190=>1000,191=>1000,192=>1000,193=>1000,194=>1000,195=>1000,196=>1000,197=>1000,198=>1000,199=>1000,
200=>1000,201=>1000,202=>1000,203=>1000,204=>1000,205=>1000,206=>1000,207=>1000,208=>1000,209=>1000,
210=>1000,211=>1000,212=>1000,213=>1000,214=>1000,215=>1000,216=>1000,217=>1000,218=>1000,219=>1000,
220=>1000,221=>1000,222=>1000,223=>1000,224=>1000,225=>1000,226=>1000,227=>1000,228=>1000,229=>1000,
230=>1000,231=>1000,232=>1000,233=>1000,234=>1000,235=>1000,236=>1000,237=>1000,238=>1000,239=>1000,
240=>1000,241=>1000,242=>1000,243=>1000,244=>1000,245=>1000,246=>1000,247=>1000,248=>1000,249=>1000,
250=>1000,251=>1000,252=>1000,253=>1000,254=>1000,255=>1000);
$enc='cp1252';
$diff='';
$file='lol.z';
$originalsize=191836;
// --- EOF ---
Look that all characters are invalid, basically.
This is the result I get using my PHP source, or using online converters made for TCPDF (eg.: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf or http://fonts.snm-portal.com/).
I am trying to add font-awesome's TTF files (eg. fa-solid-900.ttf) to my TCPDF, but I get this error.
I have no idea what to do now.
You need to import FontAwesome as a Unicode font. [Also noting: Characters 0-255 will have character widths of 1000 in your example because they don't exist in this font and 1000 is the width defined for your missing glyph width.] For your other fonts that are failing, try importing them as Unicode fonts as well or with a different encoding table.
//Ignore the warnings about undefined indexes on first import.
//There's no H or x glyphs in the font, so the warning is expected.
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4);
//Note: using empty string for $enc since this is a Unicode font
//Note: using 4 for $flags to signify a symbol font. 32 for text fonts.
$pdf->setFont($fa);
$pdf->writeHTMLCell(100,120,40,10,"");
//An example inlined with normal text:
$pdf->setFont('helvetica');
$pdf->writeHTMLCell(100,120,40,20,"Should be a gamepad: <span style=\"font-family:$fa;\"></span>");
Attached screenshot of test output results:
Test script:
<?php
//Update to your TCPDF path
include 'TCPDF/tcpdf.php';
$pdf = new TCPDF('P', 'mm','Letter', true, 'UTF-8', false, true);
$pdf->addPage();
$pdf->setFontSubsetting(true);
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4);
$testtext = "Font name: {$fa}<br>".
"Should be a gamepad: <span style=\"font-family:$fa;\"></span>";
$pdf->writeHTMLCell(100,10,10,10,$testtext);
$pdf->Output(dirname(__FILE__).'/fasolid-test.pdf', 'F');

All language supporting PDF Generation using PHP

I want to generate PDF which will have Chinese and English. Literally it has to be supported in all language.
I am using html2pdf, I tried many options but not working.
This is my code, tried with different fonts. It takes the font but not printing correctly. Printing some spacial characters instead.
$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8');
$html2pdf->setDefaultFont('arialunicid0'); //using this line
// display the full page
$html2pdf->pdf->SetDisplayMode('fullpage');
// convert
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
you can create other languages page in HTML and then convert that page into pdf
$htmlcode = 'http://localhost:8081/kaamtrade/resume.php?userid='.$userid;
$html2pdf->WriteHTML(file_get_contents($htmlcode));
i use this code in mpdf to convert whole resume html page into pdf.
maybe help in your case.

TCPDF awful text rendering

Hy,
I have some images that I converto to PDF pages with TCPDF::Image, and then I add the text on another level, but the results I get rendering text with TCPDF are quite awful, this is how the text should look
And this is the rendered PDF
As you can see TCPDF is inserting some new line "randomly", And I can't understand why, I'm using tcpdf 6.2.6
this is my code
$pdf->SetFont($TCPDFFontFile, $fontStyle, $fontSize, '', false);
$pdf->setTextRenderingMode(0, true);
$pdf->setCellHeightRatio($lineHeight);
$pdf->SetTextColorArray($textColorArray);
$pdf->MultiCell(
$removedItem->width,
$removedItem->height,
$removedItem->text,
0,
$textAlign,
false,
1,
$left,
$right
);
Any ideas? Maybe a problem with the latest version? Even if I already tried with many other version like 6.1.0 or 6.2.0 but nothing changed...
EDIT: I noticed that there are differences even in the charater spacing, and even the height of the lines are slithly different, is this any help?

Is possible to embed fontawesome font in fpdf?

I'd like to use fontawesome in pdf. I generate my pdf using php library fpdf and font embedding. However I cannot make it works.
I use this tool to generate afm file: http://fpdf.fruit-lab.de/
But when I try to use fontawesome I always get white square instead of icons.
I use this syntax to add icon:
MultiCell(0,8,"\uF000 ",0,'C')
I cannot answer for fpdf since I have never used it. However, I do use mPDF and there I use fontawesome regularly - no issues at all. The only thing I have to ensure is that the content I output to the PDF document (mPDF takes this in the form of HTML markup) hast to be UTF8 encoded.
mPDF is very good so if you are at an early stage of your project you might just consider switching to it. Otherwise, it is worth exploring whether you too are not running into a UTF8 encoding issue.
I figured this out even though this thread is over a year old. It is possible to use font-awesome although you can only use up to 256 characters per subset. I created several character maps to encompass all of the glyphs as of font awesome version 4.3.0. You just use the map that contains the characters you're going to use or you can make three subsets of it. It's not necessarily as performant as other solutions, but fPDF is still much faster than some of the alternatives because it is lacking a lot of the more modern features like unicode support.
http://code.deweyartdesign.com/fpdf/makefont/fa1.map
http://code.deweyartdesign.com/fpdf/makefont/fa2.map
http://code.deweyartdesign.com/fpdf/makefont/fa3.map
First, you need to use ttf2pt1 to create the afm file for the font.
ttf2pt1 -a fontawesome-webfont.ttf fontawesome-webfont
I made three copies of the webfont to run through makefont.php and used each encoding on the corresponding font.
require "makefont.php";
makefont('fontawesome-webfont1.ttf','fa1.map');
makefont('fontawesome-webfont2.ttf','fa2.map');
makefont('fontawesome-webfont3.ttf','fa3.map');
To use them in fPDF, put the files generated in the font folder and add them like this:
$pdf->AddFont('FA1','',fontawesome-webfont1.php);
$pdf->AddFont('FA2','',fontawesome-webfont2.php);
$pdf->AddFont('FA3','',fontawesome-webfont3.php);
Then you use the character number to render the glyph for the corresponding subset of font-awesome. The three font maps above contain the character numbers to use :
$pdf->SetFont('FA1','',14);
$WineGlass = chr(32);
$pdf->Cell(40,10,$WineGlass);
I also made a character map generator that will show the character numbers below the glyphs.
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/pdf/fpdf.php');
// Establish / Get variables
function GETVAR($key, $default = null, $prefix = null, $suffix = null) {
return isset($_GET[$key]) ? $prefix . $_GET[$key] . $suffix : $prefix . $default . $suffix;
}
$font = GETVAR('font','fontawesome-webfont1','','.php');
$pdf = new FPDF('L','mm',array(268.33,415.3));
$pdf->AddPage();
$pdf->SetMargins(0,0,0);
$pdf->SetAutoPageBreak(0,0);
// add custom fonts
$pdf->AddFont('H','','helvetica.php');
$pdf->AddFont('FA','',$font);
$pdf->SetFillColor(200,200,200);
$pdf->SetXY(9,9);
for ($i = 32; $i <= 256; $i++) {
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetX($x);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('FA','',14);
$pdf->Cell(12,12,chr($i),1,0,'C');
$pdf->SetXY($x,$y+12);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFont('H','',14);
$pdf->Cell(12,12,$i,1,0,'C',1);
$y = $pdf->GetY();
$x = $pdf->GetX();
$pdf->SetXY($x,$y-12);
if ($x > 400) {
$pdf->SetXY(9,$y+14);
}
if ($i == 328){
$pdf->AddPage();
}
}
$pdf->Output("charmap.pdf",'I');
You can re-create the font-awesome and re-map it to A-Z characters,
and that way, use it easily in fpdf.
// Import and prepare font for usage by engine:
$pdf->AddFont('fontawesome-123-full','','fontawesome-123-full.php');
...
// print FontAwesome's asterisk
$pdf->Text($x, $y, chr(0x21));
You can download the ready font and ready-to-use FPDF font files here:
http://mdb-blog.blogspot.com/2021/12/using-fontawesome-in-php-fpdf.html

Text getting clipped when generating PDF using FPDF/FPDI/TCPDF

I have a PHP project where I am opening up a premade PDF, and filling it out with data via PHP. The problem I am having is that one of the text elements isn't showing up. I am positioning it towards the bottom right of the PDF page. If I move it to the left a little, it shows up. It's as if there is some clipping or something.
I am using TCPDF, and since I am needing to modify an existing PDF, I am also having to use the FPDI class. It appears to me that FPDI normally is integrated with FPDF, so I've been using the FPDF methods to build out my PDF. OK, so here is some of my code (or the relevant parts)...
$pdf = new PDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetXY(261,200);
$pdf->Write(5, 'test');
There is at least a centimeter of whitespace to the right of the text when I position the text with a value of 260. If I move it just one more unit to 261, like in the code above, it just disappears. I'm able to position the text so far on the bottom of the page, that only the top half of the letters show, however, I can't even approach the right side of the page, or the text will completely disappear. I've set the SetAutoPageBreak to false, so new pages aren't created, and I've also flirted with zeroing out the margins.
Might be a bit late in the game...
I had a look at the fpdf.php file and it looks like it adds a margin of 1cm.
If you lower it then you can get text closer to the edge of the page. Below is the original line:
// Page margins (1 cm)
$margin = 28.35/$this->k;
But if you change it to something like
$margin = 10/$this->k;
This gets you closer to the edge of your document.
I've had the best luck using cells to position text, for some reason they have shown to be more accurate and easier to work with than simply writing text onto the document:
$pdf->SetXY(261,200);
$pdf->Cell(0,10,'My text',0,1, 'C');
Docs: http://www.fpdf.org/en/doc/cell.htm
Write() is used for flowing text (internally it uses several Cell() calls). If it reaches the right margin an automatic line break is done and the next word/character will start a new line at the left margin.
The word will not disappear but will be shown at the lower left area.
You can see the characters flowing with this simple script:
$pdf = new FPDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetFont('Helvetica');
$pdf->SetXY(261,200);
$pdf->Write(5, 't e s t');
$pdf->Output();
Try the following:
$pdf->SetAutoPageBreak('auto',0);

Categories