Image to PDF with tcpdf - php

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

Related

PHP imagettftext Is Not Showing Text

Website:
https://bimmr6696.000webhostapp.com/signs/Sign.php?Line1=&Line2=asd&Line3=&Line4=
PhpInfo:
https://bimmr6696.000webhostapp.com/signs/test.php
Currently I have the following code:
$img = LoadPNG('sign.png');
//$font = imageloadfont('minecraft.ttf');
// Add the text
imagettftext($img, 20, 0, 5,5, $black, 5, "Test Text");
imagestring($img, 5, 5, 50, 'Test Text', $text_color);
// Create image instances
$dest = imagecreatetruecolor(80, 40);
// Copy
imagecopy($dest, $img, 0, 0, 20, 13, 80, 40);
// Set the content type header - in this case image/png
header('Content-type: image/png');
// Output the image
imagejpeg($img);
imagejpeg($dest);
imagedestroy($dest);
imagedestroy($img);
When I use imagestring I'm able to get the text to show, but the text is too small which causes me to need imagettftext, although nothing shows when I use this. I've just about run out of ideas to try and solve this and so any help would be very appreciated.
TLDR: I need to either find out why imagettftext doesn't work or find a way to change font size with imagestring
Deleted everything then retyped it all out and for some reason that seemed to fix it... Because this isn't an actual answer I'm not going to post it below...

Add padding to a cell in TCPDF

I have to make invoices with TCPDF. I have a logo on top of the pdf and the title of the company has to come right next to it.
Now i'm doing this:
$pdf->Image(__DIR__.'/../../../assets/img/logo.png', 15, 10, 10, 0);
$pdf->Cell(110, 0, 'Company', 0, 0, 'L', 0, '', 3);
But "Company" is on top of the logo now. Is there a way to add padding left on that cell?
Try using:
$pdf->Image(__DIR__.'/../../../assets/img/logo.png', 15, 10, 10, 0, null, null,'T');
documentation

mPDF importing page from another document

I'm trying to create PHP document in PHP using mPDF library. I want to import the last page from another PDF document. According to document I use, the imported page is too small (it's on 1/3 of the page) or too big (I see only part of the page). Is there any way to make it fit?
My code:
$mpdf = new mPDF('', '', 0, '', 0, 0, 0, 0, 0, 0, 'L');
$mpdf->WriteHTML($html);
$mpdf->AddPage();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile('application/views/default/pdf/nabidka-design.pdf');
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->Output();
Thanks for help
You have to set page format for output PDF file like this:
$mpdf = new mPDF('utf-8', 'A4', '8', '', 10, 10, 7, 7, 10, 10);
Change 'A4' on your PDF source file format.

Display only certain part of an image and resize it with GD

I currently have a script ready that resizes a whole image with GD but I need to get a specific part of an image to display and resize only that specific part.
This is the image:
http://craffy.gdscei.com/enjikaka.png
This is what needs to be displayed, took out the rest with Photoshop:
http://craffy.gdscei.com/enjikakap.png
The final image needs to be 150x150.
This is the script i tried:
<?php
$srcp = imagecreatefrompng("enjikaka.png");
$destp = imagecreate(150, 150);
imagecopyresampled($destp, $srcp, 0, 0, -8, -8, 150, 150, 64, 32);
header('Content-type: image/png');
imagepng($destp);
?>
But this one does not pick the correct part of the image. Can anyone help me here?
Why the (-8, -8)? Those should be the upper left corner of your area to copy. It should be 8, 8. And the last two parameters: (64, 32) are the width and height of your source area. Those should be 8, 8 too.
imagecopyresampled ($destp, $srcp, 0, 0, 8, 8, 150, 150, 8, 8);
I assume here that your source image is built up by 8x8 units. You should check the coordinates in photosop.
I suggest you read the documentation of the function. That shopuld be the first thing you do when things do not go as you expected.
$srcp = imagecreatefrompng("enjikaka.png");
$destp = imagecreate(150, 150);
imagecopy($despt, $srcp, $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h);
I think you should include this call to imagecopy in your script, which should handle the cropping of the image.

Inserting an image with PHP and FPDF

I'm trying to insert an image but do not want to specify the x and y coordinates. Is that possible?
$pdf->Image($image1, 5, 70, 33.78);
I want to be able to specify the size (33.78) but not the x and y so that it moves based on the content.
$pdf->Write( 70, $reportTitle );
$pdf->Ln( 45 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $reportSubtitle );
/**
Create product 1
**/
$pdf->Ln( 10 );
$pdf->SetFont( 'Arial', '', 12 );
$pdf->Write( 6, $prod1title );
$pdf->Ln( 30 );
$pdf->SetFont( 'Arial', '', 10 );
$pdf->Write( 5, $prod1sub );
$pdf->Ln( 30 );
$pdf->Image($image1, 5, 70, 33.78);
The above is the code I use. If $reportSubtitle is two or three lines, it pushes $prod1title and $$prod1sub down, and inevitably under the image that is fixed. Is there no way to have the image act like the product title and subtitle and be pushed down too while still declaring the size?
I figured it out, and it's actually pretty straight forward.
Set your variable:
$image1 = "img/products/image1.jpg";
Then ceate a cell, position it, then rather than setting where the image is, use the variable you created above with the following:
$this->Cell( 40, 40, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 33.78), 0, 0, 'L', false );
Now the cell will move up and down with content if other cells around it move.
Hope this helps others in the same boat.
You can use $pdf->GetX() and $pdf->GetY() to get current cooridnates and use them to insert image.
$pdf->Image($image1, 5, $pdf->GetY(), 33.78);
or even
$pdf->Image($image1, 5, null, 33.78);
(ALthough in first case you can add a number to create a bit of a space)
$pdf->Image($image1, 5, $pdf->GetY() + 5, 33.78);
$image="img_name.jpg";
$pdf =new FPDF();
$pdf-> AddPage();
$pdf-> SetFont("Arial","B",10);
$pdf-> Image('profileimage/'.$image,100,15,35,35);
Please note that you should not use any png when you are testing this , first work with jpg .
$myImage = "images/logos/mylogo.jpg"; // this is where you get your Image
$pdf->Image($myImage, 5, $pdf->GetY(), 33.78);
// Image URL
$url = 'img/img.png';
// Place the image in the pdf document
$pdf->Cell(30, 30, $pdf => Image($url, 5, $pdf => GetY(), 93.78), 0, 0, 'L', false);
The 93.78 is the size of the image.
5 is the position from the left side.
You can't treat a PDF like an HTML document. Images can't "float" within a document and have things flow around them, or flow with surrounding text. FPDF allows you to embed html in a text block, but only because it parses the tags and replaces <i> and <b> and so on with Postscript equivalent commands. It's not smart enough to dynamically place an image.
In other words, you have to specify coordinates (and if you don't, the current location's coordinates will be used anyways).

Categories