Bangla (Unicode) font not rendering correctly in tcpdf - php

I am facing a problem with generating pdf in php by using TCPDF library. I need to show the bangla font correctly. I tried to add some bangla font(i.e. SolaimanLipi.ttf, SutonnyOMJ.ttf, Siyamrupali.ttf, Nikosh.ttf and so on). I can see the bangla font on pdf but the font is not display correctly. Its misplaced the word.
By adding this font I see the on the /fonts/ directory there successfully created 3 file “solaimanlipi.ctg.z”,”solaimanlipi.php” and “solaimanlipi.z”. As well as I can see the bangla font on pdf, but this font is misplaced/scattered. I am attaching a picture what I actually see.
This is how it should look (From browser screenshot):
Here is the code to show above result:
<?php
$strData = file_get_contents('./data3.txt');
?>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<style>
#font-face
{
font-family: myUniFont;
src: url(./SolaimanLipi_22-02-2012.ttf);
}
</style>
</head>
<body>
<span style="font-family: myUniFont;"><?php echo $strData; ?></span>
</body>
</html>
I use below code to use that very same font in my pdf:
$strBNFont = TCPDF_FONTS::addTTFfont('./SolaimanLipi_22-02-2012.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($strBNFont, '', 8, '', 'false');
And It is how it look like in PDF: :(
Please advice me how can I display the bangla font correctly.
EDIT #1
wow! ;( OMG!
dear sir, problem is not on pdf/tcPDF library nor even in the font file it self.
please check the below php code:
<?php
header('Content-type: image/png');
$text = 'তোমাদের জন্য মুক্তিযুদ্ধের গল্প';
$font = './Fonts/SolaimanLipi_22-02-2012.ttf';
$im = imagecreatetruecolor(600, 300);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 599, 299, $bg_color);
imagettftext($im, 20, 0, 10, 50, $font_color, $font, $font);
imagettftext($im, 20, 0, 10, 120, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
?>
this how it export/render the png image on browser:
when i try to print the text on image file using imagettftext function it also broke the character :(
as i am sure it's not font issue because i have just tested with 60+ fonts and all get broken.. while browser (html code) shows them very correctly.
so, i thing this is very bigger than my brain can contain/handle ;(
so, expert like you may only way out :)
thanks again for your times

TCPDF by itself can't handle Brahmic scripts.
I've posted similar for other languages: How can I create Malayalam PDF using TCPDF in PHP?
I believe mPDF has support for your text based on this example file: http://mpdf1.com/examples/example_utf8.pdf
I would suggest trying mPDF out if you're not dead-set on TCPDF. It's definitely easier if you can get it to work than the method I'm about to outline.
Another alternative, though, in my opinion far more complicated is to use ImageMagick with Pango to render your text as images and then include it in the PDF. This is different from ImageMagick's normal font rendering which as you saw is just as broken for your use. I'm including this more out of academic interest, I wouldn't necessarily suggest doing this unless you find a compelling reason to do so.
I basically had to do this from the shell after installing ImageMagick with Pango support:
#Install font for my user.
cp /host/SolaimanLipi_22-02-2012.ttf .fonts
#update the font-config cache
fc-cache
#Render the text with pango
convert -background white -size 400x pango:#/host/bangali.txt /host/out.gif
Where /host/bangali.txt contained <span font='18.5'>তোমাদের জন্য মুক্তিযুদ্ধের গল্প</span> *
Which then renders output like this, which I think is at least mostly correct:
This is because Pango's shaping engine is capable of doing so. There are a number of caveats though to do it this way. Not the least of which is getting font-config to behave properly from CGI or mod_php, which is doable, but tricky in my experience.
I didn't have to specify a font name since I only have one Bangala font, so font-config found and used the one I had installed.

Related

PHP ImageMagick - Draw Indic text using font file, not from system fonts

I am trying to draw Devanagari text on image using PHP ImageMagick.
ImageMagick renders text correctly if I use font available in the system, but not when loading a font from a file.
For exmaple if use
$imagick->setFont('Lohit Marathi');// installed font
works correctly but if I use
$imagick->setFont("fonts/lohit_mr.ttf");//load from file
Font does not loads and it renders using default font.
code I am trying is as follows
<?php
$imagick = new Imagick();
$imagick->setFont('ANY_SYSTEM_FONT') // works;
$imagick->setFont('somefont.ttf') // does not work;
?>
Can someone point out anything I am missing?

Font color not change in php intervention image manipulation

I'm using PHP Intervention Image Library 2.x to put some text on an image. Everything works fine except for the font colour.
Font size, font, font angle, and the position change according to the given values, the only issue is it not takes the colour I'm setting in the callback function.
Image::configure(array('driver' => 'imagick'));
$img = Image::make(IMG . 'gold-full.jpg');
$font = new Intervention\Image\Imagick\Font();
$img->text('Y. C. Perera', 1061, 1298.4, function($font) {
$font->file('./img/ERASBD.TTF');
$font->size(88);
//$font->color('#fff');
//$font->color('white');
//$font->color(array(255,255,255));
$font->color('#ffffff');
});
$img->save('./uploads/new-gold-full.jpg');
$img->destroy();
As I mentioned with the comment I tried all those types of settings for the color. but no luck.

How to convert a php page in to jpeg file and to download

I am trying to convert a web page into jpeg image file. i had used following codes.
<?php //put your html code here
$html_code = "
<html>
<head>
<title>My test title</title>
<style>
body {
font-family:verdana;
font-size:11px;
color:black
}
</style>
</head>
<body>
this is the body
</body>
</html>";
// Create the image
$img = imagecreate("300", "600");
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,300,600,$c);
imageline($img,300,0,0,600,$c);
$white = imagecolorallocate($img, 255, 255, 255);
imagettftext($img, 9, 0, 1, 1, $white, "VERDANA.TTF", $html_code);
// Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
Question: Is there any library to convert html page into image ?
You will need an HTML renderer for this. There exists a few such renderers, but most of them requires an X server on your web server, so check out Xvfb to run a framebuffer device without a screen.
GD does not do this natively, you can render a picture from your screen though but that is as far as it goes. I had painty in my links with a description of html to jpg php conversion.
You need to internally render the page. You can find possible solutions here.
When I needed to 'screen-capture' an HTML table as an image a few months back, I couldn't find anything suitable.
I eventually settled for writing my own code using PHP's GD library with GIF output.
It was for a very specialised purpose and I'm sure it wouldn't suit an application outputting jpegs.

Print stylish text on image in php

Is it possible to print html div tag with style on image in PHP?. if not, then what is the alternative way?
Some hosts have ImageMagick for PHP. To add text to your image, take a look at the syntax of the commands here. The example given on that page should help some - it's pretty easy to get text on an image.
The benefits of using ImageMagick over a fixed image is that you can vary the content of the text, which is what you might want (you didn't mention needing a static text; for this, I'd use an image with a transparent background). For more comprehensive font commands, take a look here.
To put a transparent image on top of your base image, take a look at this very nicely designed site.
I'll also give the code presented on that site here:
$photo = imagecreatefromjpeg("original.jpg");
$watermark = imagecreatefrompng("watermark.png");
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($photo, true);
// Copy the watermark onto the master, $offset px from the bottom right corner.
$offset = 10;
imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
// Output to the browser
header("Content-Type: image/jpeg");
imagejpeg($photo);
To output the image to a file, please Google that and replace the last two lines of the example given above.
For ImageMagick stuff, take a look here
I hope this helps :-)
James
You can set the image as the background graphic of any div using CSS. Then the text within that div will appear on top of the image.
(CSS)
.mydiv {
background:url(/path/to/image.gif);
width:100px; /* set to width of the image */
height:100px; /* set to height of the image */
}
(HTML)
<div class='mydiv'>Some text here</div>
There is no easy way to print text on images using html/css on server side, because php can't parse html, so you'd better find another solution like php GD.

The image cannot be displayed because it contains errors

Why is this code not working ?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<?php
header('Content-type: image/png');
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
imagepng($myImage);
imagedestroy($myImage);
?>
</body>
</html>
I always get error The image cannot be displayed because it contains errors.. I've already enabled php_gd2.dll and memory_limit in php.ini is also 128M. If i remove header('Content-type: image/png'); i don't get the error but i don't see the image either. All i see is this :-
‰PNG ��� IHDR���È���d���ùHíH���PLTEÌÌÌ���Ó33d���MIDATH‰c£Àx�§” Nf*k²Ã)Ãø�§”•5}À)ÅS†ÚšpJUà”a§²¦œ2ÔŽw<špJ‚Q0 †;�� uTBúŸ����IEND®B‚ `
You must not output anything before header(). Just start your document with <?php (as the first file characters), followed by the code for displaying the image. Skip the HTML tags. Do not even write a single blankline before header().
If you want to display an image inside the html document of yours, you must do it in two files. One, call it for example image.php, containing only the PHP code including the header. The second file, call it show.php or show.html, includes the HTML code you like, including <img src="image.php" alt="Your generated image" />.
If you base64 encode the output, you could use the image directly with a Data URI scheme.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<?php
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
ob_start();
imagepng($myImage);
printf('<img src="data:image/png;base64,%s"/>',
base64_encode(ob_get_clean()));
imagedestroy($myImage);
?>
</body>
</html>
Note that Data URIs are not supported by all browsers (guess which).
You should output only the image. You are outputting a bunch of tags. Specifically
<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN">
<title></title>
</head>
<body>
and then the image as a binary. If you want to see this, wget the page from your server and try opening it in an editor. Your code should start at the <?php.
Removing the header gets rid of the notification to the client that this is an image so it will try out print it out as text.
While trying to output a jpg of a Facebook profile-picture from their Graph API with PHP,
I noticed that if the PHP file is saved with UTF-8 encoding - this error was returned,
but if the file was saved with ANSI encoding -
then it worked OK.
If you have this problem, try to delete any space character between the begin of the script and the php tag
> <?php
I spent several hours before realize this. And now it works OK. This happens because any character on the file alter the png format. This worked great for me.
Even if you think you have removed all the text from before header() your PHP file may contain a Byte Order Marker
This will be invisible to you in your editor, but your browser will see it and think the image is corrupt. You need to take the steps appropriate to your editor to remove any BOM.
This is all you need. You can not print anything else because it needs to look like it's own file. You could call this image.php and pass it a variable to define which image to output.
<?php
header('Content-type: image/png');
$myImage = imagecreate(200, 100);
$myGray = imagecolorallocate($myImage, 204, 204, 204);
$myBlack = imagecolorallocate($myImage, 0, 0, 0);
imageline($myImage, 15, 35, 120, 60, $myBlack);
imagepng($myImage);
imagedestroy($myImage);
?>
I isolated the PHP script into its own file (image.php) and it worked fine: I got a grey rectangle with a black slanted line. Your issue is trying to embed this within a HTML file.
You need the PHP in its own, separate file (as I did, call it image.php or something more description to your needs) and then create a HTML file as follows:
<html>
<head>
<title>Document Title</title>
</head>
<body>
<img src="image.php" alt="" />
</body>
</html>
You should get your desired output then.
I had the same problem and the solution was to change the charset of the code from UTF-8 to ansi or viceversa. If the server is set to UTF-8 and your code is in ansi this don't work and if your code is in ansi and the server is configured to UTF-8 neither.
If you are using Filezilla to upload your webpages make sure you select transfer type as 'Auto/Binary'.
Transfer -> Transfer type -> Auto/ Binary.

Categories