Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 750175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:31:15+00:00 2026-05-14T14:31:15+00:00

I am using PHP and FPDF to generate a PDF with a list of

  • 0

I am using PHP and FPDF to generate a PDF with a list of items. My problem is if the item list goes on to a second or third page, I want to keep the Item Name, Quantity and Description together. Right now, it will go to a second page, but it may split up all of the details for a particular item. PLEASE HELP!

<?php
require_once('auth.php');      
require_once('config.php');   
require_once('connect.php');  

$sqlitems="SELECT * FROM $tbl_items WHERE username = '" . $_SESSION['SESS_LOGIN'] . "'";
$resultitems=mysql_query($sqlitems);

require_once('pdf/fpdf.php');
require_once('pdf/fpdi.php');


$pdf =& new FPDI();
$pdf->AddPage('P', 'Letter');    
$pdf->setSourceFile('pdf/files/healthform/meds.pdf'); 
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);

$pdf->SetAutoPageBreak(on, 30);

$pdf->SetTextColor(0,0,0);
$pdf->Ln(10);

while($rowsitems=mysql_fetch_array($resultitems)){

$pdf->SetFont('Arial','B',10);
$pdf->Cell(50,4,'Item Name:',0,0,'L');
$pdf->SetFont('');
$pdf->Cell(100,4,$rowsitems['itemname'],0,0,'L');  

$pdf->SetFont('Arial','B',10);
$pdf->Cell(50,4,'Quantity:',0,0,'L');
$pdf->SetFont('');
$pdf->Cell(140,4,$rowsitems['itemqty'],0,1,'L');

$pdf->SetFont('Arial','B');
$pdf->Cell(50,4,'Description:',0,0,'L');
$pdf->SetFont('');
$pdf->Cell(140,4,$rowsitems['itemdesc'],0,1,'L');
}

$pdf->Output('Items.pdf', 'I');

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:31:16+00:00Added an answer on May 14, 2026 at 2:31 pm

    bmb is on the right track. Here is a little more detailed solution.

    There are a number of different ways to do this, but you’ll have to make some decisions based on what you want. If you have rows that may take up half the page, then this probably isn’t going to work the best for you, but if your rows are typically about 2 – 5 lines long then this is a good method.

    Because my first cell in the row is a multiple lined cell (MultiCell in FPDF speak) in my table, the rows are of dynamic height based on this first cell. So I figure out how high the row is going to be based on the string width and the width of the cell and then compare that with the room left on the page, based on the current Y position, the page height and the bottom margin:

    while ($row = mysql_fetch_array($result)) {
    
      // multicell content
      $description = $row['desciption'];
    
      // get column width from a column widths array
      $column_width = $column_widths['description'];
    
      $number_of_lines = ceil( $pdf->GetStringWidth($description) / ($column_width - 1) );
      // I subtracted one from column width as a kind of cell padding
    
      // height of resulting cell
      $cell_height = 5 + 1; // I have my cells at a height of five so set to six for a padding
      $height_of_cell = ceil( $number_of_lines * $cell_height ); 
    
      // set page constants
      $page_height = 279.4; // mm (portrait letter)
      $bottom_margin = 20; // mm
    
      // mm until end of page (less bottom margin of 20mm)
      $space_left = $page_height - $pdf.GetY(); // space left on page
      $space_left -= $bottom_margin; // less the bottom margin
    
      // test if height of cell is greater than space left
      if ( $height_of_cell >= $space_left) {
         $pdf->Ln();                        
    
         $pdf->AddPage(); // page break.
         $pdf->Cell(100,5,'','B',2); // this creates a blank row for formatting reasons
      }
    
      // ...
      // actual creation of pdf stuff
      // ...
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using FPDF (generate PDF from PHP). I want to show all the items
I am using PHP and fpdf to generate my view in pdf. My problem
My first php question... I'm using fpdf to generate pdf files. The original version
I'm using fpdf library to generate some pdf files with php. At this moment
I am using php and fpdf to generate a pdf. How can I scale
So I coded a PDF document using fpdf (PHP) However when we go to
I'm working with generating .pdf's from PHP using this library: http://www.fpdf.org/ I am currently
Using PHP's ReflectionClass. Is this possible? I want to get a list of methods
I am going to generate report by using FPDF . My Problem Like this
I want to email a PDF as an attachment that was created using FPDF.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.