I'm using TCPDF to print a Barcode sheet label.
Each label has a barcode and some text underneath.
Evreything seems to work fine, but somtimes the text is to long and 'invade' the next label/next line.
I'm trying to check the length of the string - and short it if needed:
$label_w = ($page_w-$right_mar-$left_mar)/$Col;
$text_width = $pdf->GetStringWidth($exploded_line[2]);
while ($text_width>$label_w-15) // "-15" because the text location
{
$exploded_line[2]=substr($exploded_line[2],0,-1);
$text_width = $pdf->GetStringWidth($exploded_line[2]);
}
The text that going into the loop just continue shrinking until only the first letter left...
At first I thought that the problem is that my While condition isn't stopping for some reason.
Then I tried changing it to simple if - BUT the problem isn't gone...
if ($text_width>$label_w-15)
{
$exploded_line[2]=substr($exploded_line[2],0,-1);
$text_width = $pdf->GetStringWidth($exploded_line[2]);
}
Any suggestions? Thanks.
OK, finally I got it.
The problem was really in the substr function. I'm using UTF-8, so I had to use mb_substr...
$exploded_line[2]=mb_substr($exploded_line[2],0,-1,"utf-8");
This is working as expected.
Thanks anyway.
Related
We have an application that takes text from a form (it's ultimately a spreadsheet) and pulls it from the database and creates a pdf document. Using fdpf and everything is working well. But there is a header portion of the doc that is currently plain text posting from a <texarea>, and we want to upgrade that portion to rich text using tinymce. I can easily use https://github.com/spipu/html2pdf to create a nice, full doc from the text, but it's only meant to be a small portion of the doc. This heading is not the "Header", which is taken from a jpg image.
I've tried several approaches including creating a separate doc with the rich text (html) portion and attempting to merge the two, which works great if the two are separate pages, but not so well when trying position one inside another when the one is of varying sizes, shapes, lines. Another thing I've tried is using imagemagick to convert the pdf taken from the rich text portion to a jpg and then inserting the image into the other pdf. This works great unless the heading contains mostly text(as it will).Images come through well but text becomes unreadable with my lack if image manipulation prowess.
using fpdf
class foo extends FPDF
{
public function fooBody ($bar) {
`````````````````````````````````
$data is obj containing db entities
current lines for the heading (having to remove html rn)
$data->heading = preg_replace("/ /",'',$data->heading);
$this->MultiCell(190, 3, trim(strip_tags($data->heading)), 0, 'L', 0);
$foo= new foo();
$foo->fooBody(bar);
add page...
output()..
etc.
wish I could just do something like below
$html2pdf->writeHTML($data34->heading);
$richTextHeading = $html2pdf->output('file.pdf', 'S');
foo->MultiCell(190, 3,$richTextHeading, 0, 'L', 0);
I want to use the same doc I have now with fpdf but somehow include some converted html for a small portion of it, but am currently only capable of having one (current doc) or the other (html doc).
If anyone comes across this thread, I ended up just using TCPDF, and re-writing my old code to work with TCPDF. Suprisingly I only need to change font names in a few places (Ariel->helvetica) and explicitly state cell height and margins. The old code worked mostly as is, and this
$this->MultiCell(190, 3, trim(strip_tags($data->heading)), 0, 'L', 0);
Is now simply this
$this->writeHTML( $data->heading);
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.)
What I want to do is have a new image generated when the page is loaded. I have the random string working, but the image won't generate. If I assign it a normal name, not a variable, it'll generate perfectly fine, permissions are set, etc.. Not exactly sure what to do to get it to create the image
Here's the code that works.
$chart->render("generated/chart.png");
// But it isn't dynamic, like I want it to be
print "<center><img src=generated/$char.png></center>";
Here's the code that doesn't work. So I had the idea of using a random string generator to make a random name for the image, and just do that.
// random string code
$length = 10;
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$chart->setTitle("Mini Mood Map");
// THIS is the line giving me problems, I've tried different quotes with no luck
$chart->render(generated/$randomString.png);
// Broken image
print "<center><img src=generated/$randomString.png></center>";
Sorry, I have a bad cold and my heads not on tight tonight. Thanks for all suggestions.
Note how the original is quoted:
$chart->render("generated/chart.png");
You need to do the same:
$chart->render("generated/" . $randomString . ".png");
I am using the PHP extension FPDF to create PDF documents. I believe I have the margins set to 1" all the way around with justified text, but when I create a PDF document, download it and open it in Acrobat, I see the left margin is 1" but the right margin is about 1.2". I've gone through fpdf.php looking for the issue and cannot find it.
$oPdf = new myPDF('P', 'mm', 'letter');
$oPdf->Open();
$oPdf->SetMargins(25.4,25.4,25.4,25.4);
//set default font/colors
$oPdf->SetFont('Times', '', 12);
$oPdf->SetTextColor(0,0,0);
$oPdf->SetFillColor(255, 255, 255);
//add the page
$oPdf->AddPage();
$oPdf->AliasNbPages();
If I change the top and left margin values, the document reflects the changes. If I change the bottom or right, there is no change. I've seen how the bottom can be changed wth set auto default page break, answered elsewhere. My question is how can I change the right margin and it actually take effect in my document? I have a feeling that this has to do with the measurement of the text when FPDF calculates whether the justified text will fit on the line. I've checked the width of the line and it outputs 165.1 (which is 6.5 inches x 25.4mm) Completely stumped.
Ok, I feel kind of dumb, but hopefully this will help others. I use the FpdfMulticell (See http://www.interpid.eu/fpdf-multicell) for more control over cells and other parts of displaying the content on the PDF document. This is great and all, except for the redundancies that are created. The components of this function require width and height for the cell to be created. This is where the problem was. For some reason I was passing 160 to it rather than 165.1. 6.5" x 25.4 = 165.1. When I go through the references to create the multiCell function within the class, it fixes my problem.
If you're going to use the multicell, I suggest declaring a variable at the beginning of your page such as $pw (i.e., page width) and then instead of passing a constant value, pass the variable. That way your values are uniform and if you want to change it up, then you only have 1 value to change. Make sure you comment so you know what the purpose of that variable is.
$oMulticell = new FpdfMulticell($oPdf);
$pw = 165.1
//Make sure that the content is long enough to go to another line or you won't
//notice the difference with the justified text
$content = "Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here Write some text here ";
$oMulticell->multiCell($pw,10,$content,0,"J",1,0,0,0,0);
This works!
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);