I found annoying error with TCPDF library. Hope you know how help me. I use obj->getNumLines method to calculate how many lines has my text. It works all good in most cases but unfortunatelly not always.I use UTF8 encoding and I use many local signs (I'm from poland and we have a lot of such signs). getNumLines sometimes returns i.e. 5 although multicell displays 6 lines of text.
In my research I found that:
$font = $pdf->addTTFfont('/fonts/calibri.ttf', '', '', 32);
$pdf->setFont($font, '', 13)
$string = 'zażółć gęślą jaźń'; (repeated many times)
echo $pdf->getNumLines($string, 0);
It prints to the screen different (smaller) number than multicell actually returns lines to the pdf file. I use custom fonts as you can see above, margins are not changed during the code.
Does anybody has idea what is wrong or how to fix it?
Related
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?
My code is as follows:
<?php
session_start();
$img=imagecreatetruecolor(150,50);
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$pink=imagecolorallocate($img,200,0,150);
$grey=imagecolorallocate($img,150,150,150);
$blue=imagecolorallocate($img,0,204,255);
$redd=imagecolorallocate($img, 153, 0,0);
function randomString($length){
$chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789";
srand((double)microtime()*1000000);
$str="";
while($i<=$length){
$num=rand() % 33;
$tmp=substr($chars,$num,1);
$str.=$tmp;
$i++;
}
return $str;
}
for($i=0;$i<=rand(1,5);$i++)
{
$color=(rand(1,2)==1)? $grey:$white;
imageline($img, rand(5,50),rand(5,50),rand(50,150) , rand(5,50), $color);
}
$ran=randomString(rand(3,6));
$_SESSION['captcha']=$ran;
imagefill($img,0,0,$redd);
imagettftext($img,14,7,23,27,$black,"fonts/times_new_yorker.ttf",$ran);
imagettftext($img,16,10,18,30,$white,"fonts/times_new_yorker.ttf",$ran);
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
?>
Yesterday this worked as expected. But now Firefox is showing a message:
This image cannot be displayed, because this contains error.
When I searched for any solutions, it seems everyone is saying something about enabling GD. But in my code GD is enabled, and this very code worked perfectly up until this morning.
Can anyone help me to get a solution for this?
The image cannot be displayed, because PHP reports an error, and the header('Content-Type: image/png') tells it to show the page as an image.
To see the error, you should remove the following part:
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
or better yet, surround it with if (!isset($_GET['debug'])) statement. That way you can append ?debug=1 to your URL and see all possible PHP errors, while the image stills display normally.
There are a few possible solutions why your code might have stopped working without changing it. My guess is that you tampered with environment somehow.
session_start() needs to store session data in a directory on your local drive. Does your PHP have access to that directory?
The font fonts/times_new_yorker.ttf could disappear.
You could have moved the script to Linux machine, where letter casing matters. Are you sure the path to the font shouldn't have uppercase characters anywhere in it?
Also, just a couple of tips:
You don't need to call srand(), it's initialized automatically. (I assume you come from C/C++ background).
Instead of using rand(), you should use mt_rand() as it's faster and provides better randomness.
Instead of using magic numbers, you should use meaningful expressions (for example, replace % 33 with % strlen($chars)).
Since you seem to display a captcha, consider matching 0 and O, 1 and l as the same "character", so that reasonable user mistakes are forgiven. (Pardon if you do it already.)
well, I have a very strange problem generating 2D-Barcodes (PDF417) with PHP using TCPDF (TCPDF-Website). This is my small code:
<?php
require_once ("tcpdf/tcpdf_barcodes_2d.php");
$type = "PDF417";
$code="123456789012";
$barcodeobj = new TCPDF2DBarcode($code, $type);
$barcodeobj->getBarcodePNG();
?>
This code works well and generates the barcode. But when I change the line with the code in
$code="1234567890123";
it does not generate any output. I tried several strings and found out, that everytime I try to use a string with more than 12 digits following one after the other I get no output. It does not depend on which position the digits were.
For example:
$code="abcdefghijklmnopqrstuvwxyz123456789012abcdefghijklmnopqrstuvwxyz";
works finde, but
$code="abcdefghijklmnopqrstuvwxyz1234567890123abcdefghijklmnopqrstuvwxyz";
fails.
I use tcpdf 6.0.037 and also tried to download it from annother source. I even tried Version 6.0.020 - no change.
Server is openSuSE 12.2 64bit , PHP 5.3.15
Edit:
It's getting really strange: I tried annother barcode generator - and I get the same error. This one provides a online demo. When I fill in 1234567890123 online, I get the appropreate barcode. But on my own server the same string does not work.
"123456-7890123" works
"1234567890123" does not work
"123456789012" works
"12e34567890123" works
"123456789012sometext123456789012" works
"123456789012sometext1234567890123" does not work
Every string with more than 12 numbers in a row does not work - no matter how long the string is.
U see what I mean with "strange" ?
Any help would be highly appreciated.
I too had this problem. We are using PDF417 & QR Code barcodes. I have not found a great solution for this, but I have found a solution that works for now. If anyone has a better solution, please advise.
Problem:
In our barcodes we have phone numbers that are stored and sometimes they are 13 digits or longer. The 13 digit phone number was causing the barcode to not print correctly.
Solution:
Since the barcode would not print due to this we just add a space every 10 digits and this keeps the barcode intact and our software can read in the phone number without spaces so we should be A-OK!
Example Phone Number:
123456789012345 (15 digits)
PHP Code to run on phone number:
$phone = chunk_split($phone, 10, ' ');
Example Phone Number after split:
1234567890 12345
The libraries for these barcodes (Google/TCPDF) don't like numbers longer than 12 so it's definitely strange, but maybe I"m missing something easy to see.
Thanks and hope this helps someone.
I am having a few issues, and have no idea how to work around it and it's been bugging me for days.
I have a script that creates demotivational posters.
(example: http://www.motifake.com/image/demotivational-poster/0901/hey-beetlejuice-doris-joker-funny-demotivational-poster-1231432214.jpg)
The issue I am faced with is the TEXT that is inputted with symbols like: !#£$%^&*(),.~'
They never show, or come back with different symbols to what they should be, Like a \
will be \\ and a ' will be '\.
I have no special formatting the way the code is used is as follow:
This is the input field for the text:
Motivational text: <input type="text" name="motivationaltext">
In my PHP file I use:
$motivationaltext = $_POST['motivationaltext'];
And later on I use this for the image string:
CenterImageString($im, $width, $motivationaltext, 16, ($top + $imgy + 74), $text_color);
Does anyone have a idea on how to go about this?
In your php.ini file, set magic_quotes_gpc = Off. This should take care of the extra \.
Alternate solution: if the setting is on and you cannot (or do not want to) turn it off, use the stripslashes() function:
$motivationaltext = stripslashes($_POST['motivationaltext']);
I am trying to convert a PDF to a JPG with a PHP exec() call, which looks like this:
convert page.pdf -resize 716x716 page.jpg
For some reason, the JPG comes out with janky text, despite the PDF looking just fine in Acrobat and Mac Preview. Here is the original PDF:
http://whit.info/dev/conversion/page.pdf
and here is the janktastic output:
http://whit.info/dev/conversion/page.jpg
The server is a LAMP stack with PHP 5 and ImageMagick 6.2.8.
Can you help this stumped Geek?
Thanks in advance,
Whit
ImageMagick is just going to call out to Ghostscript to convert this PDF to an image. If you run gs on the pdf, you get the same badly-spaced output.
I suspect Ghostscript isn't handling the PDF's embedded TrueType fonts very well. If you could change your output to either embed Type 1 fonts or use a "core" PostScript font, you'd get better results.
I suspect its an encoding/widths issue. Both are a tad off, though I can't put my finger on why.
Here are some suspects:
First
The text stream is defined in UTF-16 LE. charNULLcharNULL, using the normal string drawing command syntax:
(some text) Tj
There's a way to escape any old character value into a () string. You can also define strings in hex thusly:
<203245> Tj
Neither method are used, just the questionable inline nulls. That could cause an issue in GS if it's trying to work with pointers to char without lengths associated with them.
Second
The widths array is dumb. You can define widths in groups thusly:
[ 32 [450 525 500] 37 [600 250] 40 [0] ]
This defines
32: 450
33: 525
34: 500
37: 600
38: 250
40: 0
These fonts defines their consecutive widths in individual arrays. Not illegal, but definitely wasteful/stupid, and if GS were coded to EXPECT gaps between the arrays, it could induce a bug.
There's also some extremely fishy values in the array. 32 through 126 are defined consecutively, but then it starts jumping all over: ...126 [600] 8364 [500] 8216 [222] 402 [500] 8222 [389]. 8230 [1000] 8224 [444]... and then goes back to being consecutive from 160 to 255.
Just weird.
Third
I'm not even remotely sure, but the CIDToGIDMap stream contains an AWEFUL lot of nulls.
Bottom line
Those fonts are fishy. And I've never heard of "Bellflower Books" or "UFPDF 0.1"
That version number makes me cringe. It should make you cringe too.
Googleing for "UFPDF" I found this note from the author:
Note: I wrote UFPDF as an experiment, not as a finished product. If you have problems using it, don't bug me for support. Patches are welcome though, but I don't have much time to maintain this.
UFPDF is a PHP library that sits on top of FPDF. 0.1. Just run away.