views:

25

answers:

3

Hi All

I am working on one script in which I have to create one image based on given text.

Basically I have to place those texts on various places of the generating images.

Up to this my work done. Below is the code for that.

header ("Content-type: image/jpeg");
$handle = imagecreate(415, 588) or die ("Cannot Create image");

$inven_tory=imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'].'/thumb/editor/text_letter.jpg');
imagecopymerge($handle,$inven_tory,0,0,0,0,415,588,100);

$bg_color = imagecolorallocate ($handle, 255, 255, 255); // 0 -255
$txt_color = imagecolorallocate ($handle, 0, 0, 0);


// The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text.
imagettftext($handle, 15, 0, 10, 25, $txt_color, "fonts/" . $font, wordwrap($text1, 35, "\n", true));

// The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text.
imagettftext($handle, 20, 0, 40, 120, $txt_color, "fonts/" . $font, wordwrap($text2, 13, "\n", true));

// The first parameter is our handle, then font size, rotation, starting X, starting Y, text color, font, and finally our text.
imagettftext($handle, 15, 0, 10, 500, $txt_color, "fonts/" . $font, wordwrap($text3, 40, "\n", true));

imagejpeg($handle);

Image is creating properly from this page. Now I to place this generated image in PDF and give user to save that PDF. How can I do this? Let me know if you are not clear.

A: 

I would try wkhtmltodpf with exec().

Orbit
A: 

If you're on a Linux server with ImageMagick installed you can do something like:

exec("convert foo.jpg bar.pdf");
seengee
A: 

How you place your image in a PDF depends on how you're going to create the PDF. There are extensions and PHP classes that allow you to directly create a PDF (CPDF, FPDF, TCPDF). There are also classes and programs that allow you to create a PDF from an HTML document. The PDF classes give you more precise control, the HTML to PDF classes make it easier to work with since most PHP developers already know HTML to some degree. Your situation is simple enough I would be inclined to use a PDF-generating class.

You don't specify how you get the values of $text1, $text2, and $text3. Presumably they are provided by the user. So what you would want is an intermediate PHP that collects the user input and creates your PDF. You then reference your image-generating PHP using a full URL. Something like the following would work (using EZPDF+CPDF):

<?php
include ('class.ezpdf.php');
$pdf = new Cezpdf();
$pdf->ezImage('http://example.com/image.php?text1='.urlencode($text1));
$pdf->ezStream();
?>

I typically work with DOMPDF, which converts an HTML document to a PDF document. The same using this library would be something as simple as:

<?php
require_once('dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html('http://example.com/image.php?text1='.urlencode($text1));
$dompdf->render();
$dompdr->stream();
?>
BrianS