Rectangle 27 5

In FPDF there are internal variables w and h for width and height, respectively. However, these are represented in the user units. By default, this is millimeters. Images are always going to be presented in pixels, which can cause a problem. That said, FPDF also tracks wPt and hPt, which give you the dimensions in a manner that can be very easily compared with to pixel units. Finally, there is k, which is a scale unit that can be used to convert from the user units to pixels.

  • Get the image dimensions with getimagesize
  • Get the page dimensions in pixels with $pdf->w
  • Calculate the total page width and subtract the image width to get the left padding we need
  • Use $pdf->k to convert the left padding from pixels to user units
  • Print an empty boundary cell left of the image with $pdf->Cell
  • Print the image with $pdf->Image

We will not put the image in a cell. Don't do this. Cells weren't built for images, only for strings. You will not get intended results if you attempt this method. Instead, opt for the method above. Code as such:

//  Get image dimensions
$size = getimagesize('img.png');

if( $size===false )
    die('Image does not exist.');

$wImg = $size[0];
$hImg = $size[1];

//  Get PDF dimensions
$wPdf = $pdf->wPt;
$hPdf = $pdf->hPt;

//  Calculate width necessary for the cell
$width  = $wPdf - $wImg;

if( $width<0 )
{
    error_log('Image is larger than page we\'re trying to print on.');
    $width = 0;
}

//  Convert pixel units to user units
$width  /= $pdf->k;
$height /= $pdf->k;

//  Print a boundary cell
$pdf->Cell($width,$height);

//  Print image
$pdf->Image('img.png');

//  Force a new line
$pdf->Ln();

I would recommend putting this into a function or something of the like if you plan to use it a lot. Note that this only works for right alignment. You will do a similar method with different calculations for center alignment. The boundaries to the left and right on the page are defined by FPDF and can be set in the constructor with SetMargins

I removed this line $hPdf = $pdf->hPt; and added this line $height = $hImg; after $width = $wPdf - $wImg; to stop a variable undefined error. Once I did that, it worked a treat, thanks.

fpdf + php how to insert an image in a cell on right side in pdf docum...

php fpdf
Rectangle 27 2

I used this method to save google charts as an image by using base64 url to save in some folder from server side finally use in fpdf as an image from folder.

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script type="text/javascript">

    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawVisualization);

    function drawVisualization() {
    // Some raw data (not necessarily accurate)
    var data = google.visualization.arrayToDataTable([
    ['', ''],
    ['Numerical', 4],
    ['Verbal', 6],
    ['Mechanical', 4],  
    ['Reasoning', 5],  
    ['Spatial', 9]
    ]);

    var options = {
    title : '',
    vAxis: {title: "", viewWindowMode:'explicit',
          viewWindow:{
            max:12,
            min:0
          }},
    hAxis: {title: ""},
    seriesType: "bars",
    series: {5: {type: "line"}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    chart.draw(data, options);

    var app1=(chart.getImageURI());
        $.post("appt1.php",{postapp: app1},
        function(){});
    }
</script>
<div id="chart_div" style="display: none;"></div>


<!-- On the Server side  do this to store image appt1.php -->

$data =$_POST['postapp'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);

file_put_contents('exam_images/'."1".'.png', $data);

Not a bad solution, though I have to be able to generate them on the server side, which I cannot do with this solution

I finished up my project using the above method of generating reports. may be you are missing something :(

javascript - Using Google Charts API Generated Graphic in FPDF - Stack...

javascript php google-visualization fpdf
Rectangle 27 1

I didn't follow the code completely, but it seems you're using the Header and Footer methods to set Y and nothing more, expecting that to be enough to correctly position the MultiCells being output outside of the Header and Footer. Maybe so, but the interaction of positioning inside and outside the Header/Footer isn't well defined.

For example, the process may be something like this: Y is calculated for the MultiCell, that trips the footer, the footer changes Y, the MultiCell is output. Is this the original Y, the revised (by the footer Y), or some other value? Absent a precise definition of what happens, you've set up a complex sequence of things that would be very difficult to sort out.

I would suggest vastly simplifying the code. You may find that the automatic header/footer tripping isn't helpful at all. In that case, turn off the auto page break, get rid of the Footer/Header functions, and totally control each page yourself. That way at least you have a clear, reliable model of what's going on.

Regarding footer issue in pdf file creation using php and fpdf library...

php pdf pdf-generation fpdf
Rectangle 27 1

If you got struggled with fpdf, you can try to do it with Debenu Quick PDF. This C# code returns the exact result which is on your picture. It is done with merged cells, but also you can use zero border width - it depends on your needs. Of course you can change the width and the color of the borders.

DPL.LoadFromFile("blank.pdf", "");
DPL.SetOrigin(1); //the top left page corner will be used for the origin 
DPL.SetMeasurementUnits(0); //the units are approximately the same as a "point"

string content1, content2, content3;
content1 = "1<br>2<br>3<br>4<br>5<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>20";
content2 = "21<br>22<br>23<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>30";
content3 = "31<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>40";

int tableID1 = DPL.CreateTable(20, 3); //20 rows, 3 columns
DPL.SetTableColumnWidth(tableID1, 1, 1, 20); //width of the first column
DPL.SetTableColumnWidth(tableID1, 2, 3, 40); //width of the second and third column
DPL.SetTableRowHeight(tableID1, 1, 20, 10); //height of all rows
DPL.MergeTableCells(tableID1, 2, 1, 20, 1); //merge the cells from second row to 20. row and from 1. column to 1. column - merge the cells of the first column
DPL.MergeTableCells(tableID1, 2, 2, 20, 2); //merge the cells of the second column
DPL.MergeTableCells(tableID1, 2, 3, 20, 3); //merge the cells of the third column
DPL.SetTableCellContent(tableID1, 2, 1, content1); //add the content
DPL.DrawTableRows(tableID1, 50, 50, 700, 1, 0); //draw the table to the page

int tableID2 = DPL.CreateTable(20, 3);  // second table
DPL.SetTableColumnWidth(tableID2, 1, 1, 20);
DPL.SetTableColumnWidth(tableID2, 2, 3, 40);
DPL.SetTableRowHeight(tableID2, 1, 20, 10);
DPL.MergeTableCells(tableID2, 2, 2, 20, 2);
DPL.MergeTableCells(tableID2, 2, 3, 20, 3);
DPL.MergeTableCells(tableID2, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID2, 2, 1, content2); 
DPL.DrawTableRows(tableID2, 160, 50, 700, 1, 0);

int tableID3 = DPL.CreateTable(20, 3);   //third table
DPL.SetTableColumnWidth(tableID3, 1, 1, 20);
DPL.SetTableColumnWidth(tableID3, 2, 3, 40);
DPL.SetTableRowHeight(tableID3, 1, 20, 10);
DPL.MergeTableCells(tableID3, 2, 2, 20, 2);
DPL.MergeTableCells(tableID3, 2, 3, 20, 3);
DPL.MergeTableCells(tableID3, 2, 1, 20, 1);
DPL.SetTableCellContent(tableID3, 2, 1, content3); 
DPL.DrawTableRows(tableID3, 270, 50, 700, 1, 0);            

DPL.SaveToFile("tables.pdf");

You can find the description of the functions on this site:

php - How to create multi table side by side with fpdf - Stack Overflo...

php pdf pdf-generation fpdf
Rectangle 27 6

$pdf->setFillColor(230,230,230); 
$pdf->Cell(0,10,$text,0,1,'L',1); //your cell

The method can be called before the first page is created and the value is retained from page to page

Good answer @JuanSedano. It was helpful to understand that using a 1 is true and 0 is false option in Cell is how you can fill the background. Thank you for the answer.

php - Background Color inside a cell is not changing in fpdf - Stack O...

php css fpdf
Rectangle 27 3

Your cells are with different width. First Multicell is 40 second one inside loop is 80

$pdf->Multicell(40,4,"LAMPIRAN FOTO\nJAMINAN",1,"C");
...
$pdf->Multicell(80,4,'Debitur : '. $row['debitur'],1);
$pdf->Multicell(80,4,"LAMPIRAN FOTO\nJAMINAN",1,"C");
...
$pdf->Multicell(80,4,'Debitur : '. $row['debitur'],1);

Then if you don't want LAMPIRAN FOTO\nJAMINAN to go on new line remove \n from it.

Multicells

i have try to make all Multicell equal like your example. still not work

Did you also check the example page which I linked in my answer?

yes, but i cannot understand it. i am not interesting using array

php - new line in FPDF - Stack Overflow

php fpdf
Rectangle 27 3

Your cells are with different width. First Multicell is 40 second one inside loop is 80

$pdf->Multicell(40,4,"LAMPIRAN FOTO\nJAMINAN",1,"C");
...
$pdf->Multicell(80,4,'Debitur : '. $row['debitur'],1);
$pdf->Multicell(80,4,"LAMPIRAN FOTO\nJAMINAN",1,"C");
...
$pdf->Multicell(80,4,'Debitur : '. $row['debitur'],1);

Then if you don't want LAMPIRAN FOTO\nJAMINAN to go on new line remove \n from it.

Multicells

i have try to make all Multicell equal like your example. still not work

Did you also check the example page which I linked in my answer?

yes, but i cannot understand it. i am not interesting using array

php - new line in FPDF - Stack Overflow

php fpdf
Rectangle 27 4

I'm not sure because I've not used them in about five years, but don't you have to declare $date as being a global inside the Header() function?

function Header() {
    $date = $GLOBALS['date'];
    ...

Yes, that was :)

Problem in FPDF header with a php variable - Stack Overflow

php fpdf
Rectangle 27 3

mpdf->SetFillColor('RED');
$mpdf->WriteCell(38, 10, 'HELLO', 1, 0, 'C', TRUE);

php - Background Color inside a cell is not changing in fpdf - Stack O...

php css fpdf
Rectangle 27 1

issue resolved. I applied the left Margin

$pdf->SetLeftMargin(85);

php - How I set the FPDF Cell to Right Side - Stack Overflow

php fpdf
Rectangle 27 1

issue resolved. I applied the left Margin

$pdf->SetLeftMargin(85);

php - How I set the FPDF Cell to Right Side - Stack Overflow

php fpdf
Rectangle 27 1

You may want to try:

$pdf->Image(file_get_contents("http://yousite/mygraph.php"));

Make sure you set the correct header on mygraph.php to match the image mime type, i.e.:

header('Content-type: image/jpeg');

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

php - How to insert a graph of JpGraph inside a PDF from FPDF - Stack ...

php pdf report fpdf jpgraph
Rectangle 27 6

Here it is, this will display each image in the array on a new page, all inside one file:

$pdf=new PDF();
$images = array("image1.jpg", "image2.jpg", "image3.jpg"); 

reset($images);
while (list(, $image) = each($images)) {
    $pdf->AddPage();
    $pdf->Image($image,10,8,33);
}

$pdf->Output();

For the reference on how the Image() works, and what type of parameters it can take reference to this link

I would have done a foreach rather some some archaic while condition, but this should work.

@Phoenix you are right, but today I was not in the mood for a 'foreach' :D

php - how create a PDF with series of images each printed on new page ...

php fpdf
Rectangle 27 4

I needed that functionality too. This is the function I written to do a simple colored string:

function cellMultiColor($stringParts) {
    $currentPointerPosition = 0;
    foreach ($stringParts as $part) {
        // Set the pointer to the end of the previous string part
        $this->_pdf->SetX($currentPointerPosition);

        // Get the color from the string part
        $this->_pdf->SetTextColor($part['color'][0], $part['color'][1], $part['color'][2]);

        $this->_pdf->Cell($this->_pdf->GetStringWidth($part['text']), 10, $part['text']);

        // Update the pointer to the end of the current string part
        $currentPointerPosition += $this->_pdf->GetStringWidth($part['text']);
    }
cellMultiColor([
    [
        'text' => 'Colored string example: ',
        'color' => [0, 0, 0],
    ],
    [
        'text' => 'red',
        'color' => [255, 0, 0],
    ],
    [
        'text' => ', ',
        'color' => [0, 0, 0],
    ],
    [
        'text' => 'blue',
        'color' => [0, 0, 255],
    ],
]);

php - FPDF: Change text color while inside a Cell? - Stack Overflow

php text colors cell fpdf
Rectangle 27 4

I needed that functionality too. This is the function I written to do a simple colored string:

function cellMultiColor($stringParts) {
    $currentPointerPosition = 0;
    foreach ($stringParts as $part) {
        // Set the pointer to the end of the previous string part
        $this->_pdf->SetX($currentPointerPosition);

        // Get the color from the string part
        $this->_pdf->SetTextColor($part['color'][0], $part['color'][1], $part['color'][2]);

        $this->_pdf->Cell($this->_pdf->GetStringWidth($part['text']), 10, $part['text']);

        // Update the pointer to the end of the current string part
        $currentPointerPosition += $this->_pdf->GetStringWidth($part['text']);
    }
cellMultiColor([
    [
        'text' => 'Colored string example: ',
        'color' => [0, 0, 0],
    ],
    [
        'text' => 'red',
        'color' => [255, 0, 0],
    ],
    [
        'text' => ', ',
        'color' => [0, 0, 0],
    ],
    [
        'text' => 'blue',
        'color' => [0, 0, 255],
    ],
]);

php - FPDF: Change text color while inside a Cell? - Stack Overflow

php text colors cell fpdf
Rectangle 27 3

It is possible with a little trick. I just made it printing 2 Cells, one over the other, like this:

//Setting the text color to black
$pdf->SetTextColor(0,0,0);

//Printing my cell      
$pdf->SetFont('Arial','B');
$pdf->Cell(55,5,"Black Text ",1,0,'C');
$pdf->SetXY($coordXbase,$coordY);

//Setting the text color to red
$pdf->SetTextColor(194,8,8);

//Printing another cell, over the other
$pdf->SetFont('Arial','B');
//Give some space from the left border, and print the red text after the black text that is in the cell behind this one.
$pdf->Cell(55,5,"                        Red Text",0,0,'C');
$pdf->SetXY($coordXbase,$coordY);

//Setting the text color back to back, in the next cells.
$pdf->SetTextColor(0,0,0);

The result was this:

As I was a little rush, I had no time to create some function to help with this, but this would be a good starting point idea :)

P.S.: Tell if you guys find an easier way.

php - FPDF: Change text color while inside a Cell? - Stack Overflow

php text colors cell fpdf
Rectangle 27 3

It is possible with a little trick. I just made it printing 2 Cells, one over the other, like this:

//Setting the text color to black
$pdf->SetTextColor(0,0,0);

//Printing my cell      
$pdf->SetFont('Arial','B');
$pdf->Cell(55,5,"Black Text ",1,0,'C');
$pdf->SetXY($coordXbase,$coordY);

//Setting the text color to red
$pdf->SetTextColor(194,8,8);

//Printing another cell, over the other
$pdf->SetFont('Arial','B');
//Give some space from the left border, and print the red text after the black text that is in the cell behind this one.
$pdf->Cell(55,5,"                        Red Text",0,0,'C');
$pdf->SetXY($coordXbase,$coordY);

//Setting the text color back to back, in the next cells.
$pdf->SetTextColor(0,0,0);

The result was this:

As I was a little rush, I had no time to create some function to help with this, but this would be a good starting point idea :)

P.S.: Tell if you guys find an easier way.

php - FPDF: Change text color while inside a Cell? - Stack Overflow

php text colors cell fpdf
Rectangle 27 2

If you are satisfied with similar solution without using MultiCell(), then no need to use Diego's suggestion, because you need to pay at least 20 EUR for that solution.

I've already answered this with only a few lines of code in FPDF - Inline bold text

You can easily modify the Rect() width and height with math to contain your database values.

php - Any suggestion how to display the portion of a text bold inside ...

php fpdf bold
Rectangle 27 1

MultiCell

This method allows printing text with line breaks. They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other. Text can be aligned, centered or justified. The cell block can be framed and the background painted.

php - FPDF wrap text inside cell table - Stack Overflow

php fpdf