Jump to content

[SOLVED] PDF - embed image.


shamuntoha

Recommended Posts

I have a php code running, i can create pdf file with text informations for the moment only.

But how can i embed a A4 background image gif, jpg, etc.

 

And in front of the background apply the following text ? Any reference?

 

<?php
/*
* This is an example of my first PDF test
* Worked by NuSphere PHPed
* Manualy downloaded library from 
* http://pecl4win.php.net/ext.php/php_pdf.dll
*/

/*
* Stage - 1
* Resource create for PDF library
*/
$pdf = PDF_new();


/*
* Stage - 2
* Save where? Path to create the file.
*/
pdf_open_file($pdf, "d:\heloworld.pdf");


/*
* Stage - 3
* Page diemention (A4)
*/
pdf_begin_page($pdf, 595, 842);


/*
* Stage - 4
* path of your TTF font directory 
* can also use pdf_load_font();
* http://uk3.php.net/manual/en/function.pdf-findfont.php
*/
$fontdir = "C:\WINDOWS\Fonts"; 
pdf_set_parameter($pdf, "FontOutline", "arialMyName=$fontdir\arial.ttf");
$arial = PDF_findfont($pdf,"arialMyName","host",0 );


/*
* Stage - 5
* Set font size and font name
*/
pdf_setfont($pdf, $arial, 10);


/*
* Stage - 6
* print text
*/
pdf_show_xy($pdf, "Hellow World? ",50, 750); 
pdf_show_xy($pdf, "Test 1, 2, 3, 4 working. ", 50,730);


/*
* Stage - 7
* end page 
*/
pdf_end_page($pdf);


/*
* Stage - 8
* close and save file
*/
pdf_close($pdf);




?>

Link to comment
Share on other sites

Any idea how to fix it? $image is having the problem.

 

PHP: 5.0.3:

 

Fatal error: Uncaught exception 'PDFlibException' with message 'Handle parameter 'image' has bad value 0' in C:\Inetpub\wwwroot\tst.php:35 Stack trace: #0 C:\Inetpub\wwwroot\tst.php(35): pdf_fit_image(Resource id #2, 0, 10, 10, 'scale 0.5') #1 {main} thrown in C:\Inetpub\wwwroot\tst.php on line 35

 

Code:

 

if (($image = PDF_load_image($pdf, "jpeg", "C:\Inetpub\wwwroot\img\addcallback.JPG", 0)) == -1) {

fprintf(stderr,"Error: Couldn't read image file.\n");

} else {

PDF_fit_image($pdf, $image, 10, 10, "scale 0.5");

PDF_close_image($pdf, $image);

}

 

Link to comment
Share on other sites

Where should be the fatal error ?

C/C++ Manual Function prototype:

 

int PDF_load_image(PDF *p, const char *imagetype, const char *filename, int reserved, const char *optlist)

void PDF_fit_image(PDF *p, int im, float x, float y, const char *optlist)

void PDF_close_image(PDF *p, int image)

 

PHP Trying :

//PDF *p;

$pdf = PDF_new();

 

$image = PDF_load_image($pdf, "jpeg", "c:\picture.jpg", 0);

PDF_fit_image($pdf, $image, 1.5, 1.5, "scale 0.5");

PDF_close_image($pdf, $image);

 

Link to comment
Share on other sites

Working with this: http://www.fpdf.org/

Great job....

 

1. GD enable.

2. php_pdf.dll

 

and following code....

 

<?php

require('../fpdf.php');

 

class PDF extends FPDF

{

    //Page header

    function Header()

    {

    //Logo

    $this->Image('logo_pb.png',10,8,300, 500);

       

    //Arial bold 15

    $this->SetFont('Arial','B',15);

    //Move to the right

    $this->Cell(80);

    //Title

    $this->Cell(30,10,'Title',1,0,'C');

    //Line break

    $this->Ln(20);

    }

 

    //Page footer

    function Footer()

    {

    //Position at 1.5 cm from bottom

    $this->SetY(-15);

    //Arial italic 8

    $this->SetFont('Arial','I',8);

    //Page number

    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');

    }

}

 

//Instanciation of inherited class

$pdf=new PDF();

$pdf->AliasNbPages();

$pdf->AddPage();

$pdf->SetFont('Times','',12);

for($i=1;$i<=40;$i++)

$pdf->Cell(0,10,'Printing line number '.$i,0,1);

$pdf->Output();

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.