PHP TCPDF Font Size in MM - php

in this moment i spend many hours to find a solution to scale a Text in TCPDF in mm.
Is it possible to scale a Text to 190mm width and automatic high?
I need only one Word that is every time scaled to 190mm width.
I dont found something that solve my problem. Maybe you have a idea?
Thank you and have a nice holiday :)
I test several things that i found in google
Maybe i am searching for the wrong things, or maybe i dont have the right keywords?
$pdf->setPageUnit('mm');
$document_width = $pdf->pixelsToUnits('100');
$document_height = $pdf->pixelsToUnits('100');
$x = $pdf->pixelsToUnits('190');
$y = $pdf->pixelsToUnits('190');
$font_size = $pdf->pixelsToUnits('190');
$txt = 'AAAg';
$pdf->SetFont ('helvetica', '', $font_size , '', 'default', true );
$pdf->Text ( $x, $y, $txt, false, false, true, 0, 0, '', false, '', 0, false, 'T', 'M', false );

Related

Imagick annotateImage: how to set text position from the top left

I'm trying to create a wrapper function around annotateImage to be able to set the exact top and left positions of a given text. The default method sets the y-position from the baseline, which means there has to be a lot of experimentation involved if one wants to draw a text at an exact spot on an image. This is what I mean...
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox');
In the above code, the text is invisible because the y position is 0. So to fix this, I've started with the following function where I add an offset of 40 to y...
function addText($image, $draw, $x, $y, $text) {
$y = $y + 40;
$image->annotateImage($draw, $x, $y, 0, $text);
}
addText($image, $draw, 0, 0, 'The quick brown fox'); // draw at 0, 0
But it's not very reliable because it doesn't take into account any factors such as font size, etc...
What's the best way to achieve this?
I've found a solution that works well. If the gravity is set to 'NorthWest', the text tends to keep its x y position from the top left.
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
$image->annotateImage($draw, 0, 0, 0, 'The quick brown fox');

FPDF align text LEFT, Center and Right

I've have three cells and i'am trying to align the text to Left, Center and Right.
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->Cell(0,10,'Center text:',0,0,'C');
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
When I ouput my PDF file, center text get automatically aligned right.
This is how it looks:
Can somebody tell me what I'm doing wrong here and how I can fix this issue?
The new position after a Cell call will be set to the right of each cell if you set the ln-parameter of the Cell method to 0. You have to reset the x-coordinate before the last 2 Cell calls:
class Pdf extends FPDF {
...
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0,10,'Left text',0,0,'L');
$this->SetX($this->lMargin);
$this->Cell(0,10,'Center text:',0,0,'C');
$this->SetX($this->lMargin);
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
}
Though Jan Slabon's answer was really good I still had issues with the center not being exactly centered on my page, maybe we have different versions of the library and that's what accounts for the slight differences, for instance he uses lMargin and on some versions that's not available. Anyway, the way it worked for me is this:
$pdf = new tFPDF\PDF();
//it helps out to add margin to the document first
$pdf->setMargins(23, 44, 11.7);
$pdf->AddPage();
//this was a special font I used
$pdf->AddFont('FuturaMed','','AIGFutura-Medium.ttf',true);
$pdf->SetFont('FuturaMed','',16);
$nombre = "NAME OF PERSON";
$apellido = "LASTNAME OF PERSON";
$pos = 10;
//adding XY as well helped me, for some reaons without it again it wasn't entirely centered
$pdf->SetXY(0, 10);
//with SetX I use numbers instead of lMargin, and I also use half of the size I added as margin for the page when I did SetMargins
$pdf->SetX(11.5);
$pdf->Cell(0,$pos,$nombre,0,0,'C');
$pdf->SetX(11.5);
//$pdf->SetFont('FuturaMed','',12);
$pos = $pos + 10;
$pdf->Cell(0,$pos,$apellido,0,0,'C');
$pdf->Output('example.pdf', 'F');
Seems that there is a parameter for this, so ('R' for right-aligning, 'C' for centering):
$pdf->Cell(0, 10, "Some text", 0, true, 'R');
will right align text.
Also, notice that the first parameter ('width') is zero, so cell has 100% width.

Image to PDF with tcpdf

Hy
I'm trying to convert an image (both png or jpeg) to pdf and then adding crop marks using tcpdf (no limitation on the library, it just seemed the best to me, no problems in changing if would solve the problem).
the best case for me is: I have an input image, I give it to some function of tcpdf, that converts it into a pdf, add the bookmarks in the angles (top-left, top-right, bottom-left, bottom-right) and save the pdf without nowing anything about the sizes of the image...
I tried looking around but coudn't find anything without passing the size of the images to tcpdf, this is what I came up with:
$pagelayout = array('706', '606'); // or array($height, $width)
$pdf = new TCPDF('', 'pt', $pagelayout, true, 'UTF-8', false);
// set document information
$pdf->SetAuthor('JOIN SRL');
// add a page
$pdf->AddPage();
$pdf->Image($input, 0, 0, '', '', 'JPG', 'http://www.tcpdf.org', '', true, 300, '', false, false, 1, false, false, false);
$pdf->Image($input);
$pdf->cropMark('', '', 10, 10, 'TL');
$pdf->cropMark('', '', 10, 10, 'TR');
$pdf->cropMark('', '', 10, 10, 'BL');
$pdf->cropMark('', '', 10, 10, 'BR');
$pdf->Output($output, 'F');
as you can see I had to pass the image size to tcpdf, but this is just a test, I would like to get rid of this informations in this peace of code...
And this dosn't work either because the pdf page comes otu bigger than the image, and hte crop marks don't get show( I think this is quite obvious, I tried not giving coordinates hoping that they would automatically set in the angles of the page, but no luck) .
Does someone have any ideas?
thank's
EDIT
I managed to solve nearly everything. could be fine like this( the code follows) but I wanted to know if there is a way to do the same thing without knowing the image size.
$pdf->setMargins(0, 0, 0);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
$pdf->Image($input);
$width = $pdf->getPageWidth();
$height = $pdf->getPageHeight();
$pdf->cropMark(5, 5, 5, 5, 'TL', array(255, 0, 0));
$pdf->cropMark($width - 5, 5, 5, 5, 'TR', array(255, 0, 0));
$pdf->cropMark(5, $height - 5, 5, 5, 'BL', array(255, 0, 0));
$pdf->cropMark($width - 5, $height - 5, 5, 5, 'BR', array(255, 0, 0));
$pdf->Output($output, 'F');
thank's

Putting padding between string and border in TCPDF

How would I put padding between a string and the border to said string in TCPDF. I'm not seeing any way to do that in the Text() method.
Do I have to use another method? Is there anyway to accomplish this?
This would be an example of code I'm using:
$pdf->Text(130, $margintop, $fieldkey, false, false, true, 'B', 0, '', false, '', 0, false, 'B', 'T', false);
TCPDF assigned default-values to the cell-paddings, which can be overwritten:
$pdf->setCellPaddings( $left = '', $top = '', $right = '', $bottom = '');
Or, with using the same padding for all sides:
$pdf->setCellPadding( $padding );
See the documentation here: http://www.tcpdf.org/doc/code/classTCPDF.html#a374b24751bf76e4ca5ba7694f87ec2c1
Update 22.04.2020: https://tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_SetCellPadding
Update 20.08.2020:
https://tcpdf.org/docs/srcdoc-copy/TCPDF/class-TCPDF/#_SetCellPadding

Unusual result when merging two transparent images in php

Im wondering if I'm doing something wrong, or if this is as good a result as im going to get. Both PNGs on the left are 95x111. The image of the robot has a 5px or so padding of transparent pixels around it, but it seems to be causing problems when they merge?
$avatar = imagecreatefrompng("../guy.png");
$borderImg = imagecreatefrompng("../frame.png");
imagealphablending( $borderImg, false );
imagesavealpha( $borderImg, true );
imagecopyresampled($avatar,$borderImg, 0, 0, 0, 0, 95, 111,95, 111);
imagepng($avatar, $newfilenameBig);
Ive tried every combo of imagealphablending and imagesavealpha I can think of. When I set $avatar to imagesavealpa= true, then it doesnt even show the image as all, just the frame. Doesn't that seem strange?
Is this as far as i'm gonna get using PHP GD?
UPDATE: The desired result can be achieved when both images are created manually in PS using 24 bit mode. Is there a way to do this using imagecopy or similar?
Try the following code its working fine.
$width = 95;
$height = 111;
$base_image = imagecreatefrompng("../guy.png");
$top_image = imagecreatefrompng("../frame.png");
imagesavealpha($top_image, false);
imagealphablending($top_image, false);
imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
imagepng($base_image, "merged.png");

Categories