开发者

Problem with tcpdf Pagebreak

开发者 https://www.devze.com 2022-12-17 07:25 出处:网络
I have a little problem with a pagebreak. The Multicell is displayed over the footer of the first page and then it breaks: How can I se开发者_开发技巧t the bottom margin of the page so that the break

I have a little problem with a pagebreak. The Multicell is displayed over the footer of the first page and then it breaks: How can I se开发者_开发技巧t the bottom margin of the page so that the break happened earlier above? Here is the example PDF: Example and here the sourcecode:

<?php require_once('../tcpdf/config/lang/eng.php'); 
  require_once('../tcpdf//tcpdf.php'); 

  class MYPDF extends TCPDF {
    public function Header() { 
      $auto_page_break = $this->AutoPageBreak;
      $this->SetAutoPageBreak(false,0); 
      $this->setJPEGQuality(100); $img_file = 'images/mandanten/ce_background.jpg';       
      $this->Image($img_file, $x=160, $y=72, $w=36, $h=200, $type='', $link='', $align='', $resize=true, $dpi=150, $palign='', $ismask=false, $imgmask=false, $border=0);        
      $this->SetAutoPageBreak($auto_page_break); } 
   }

 $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
 $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('tmpAutor');  
 $pdf->SetTitle('tmpTitle'); $pdf->SetSubject('tmpSubject'); 
 $pdf->SetKeywords('tmp');   $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
 $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));                
 $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 
 $pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);                                   
 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);  
 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);             
 $pdf->SetAutoPageBreak(True, PDF_MARGIN_BOTTOM); 
 //set image scale factor 
 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 
 //set some language-dependent strings 
 $pdf->setLanguageArray($l); $pdf->AddPage(); 
 $pdf->SetFont('freesans', '', 16); 
 $pdf->Cell(0, 10, 'Headline', 0, 1, 'L'); 
 $pdf->SetFont('freesans', '', 11); 
 // Some Dummy Unicode content 
 $tmp = 'Lorèm ìpsum dolor sìt åmèt, čonsètètur sådìpsčìng èlìtr, sèd dìåm nonumy èìrmod tèmpor ìnvìdunt ut låborè èt dolorè mågnå ålìquyåm èråt, sèd dìåm voluptuå. åt vèro èos èt åččusåm èt justo duo dolorès èt èå rèbum. Stèt člìtå kåsd gubèrgrèn, no sèå tåkìmåtå sånčtus èst Lorèm ìpsum dolor sìt åmèt. Lorèm ìpsum dolor sìt åmèt, čonsètètur sådìpsčìng èlìtr, sèd dìåm nonumy èìrmod tèmpor ìnvìdunt ut låborè èt dolorè mågnå ålìquyåm èråt, sèd dìåm voluptuå. åt vèro èos èt åččusåm èt justo duo dolorès èt èå rèbum. Stèt člìtå kåsd gubèrgrèn, no sèå tåkìmåtå sånčtus èst Lorèm ìpsum dolor sìt åmèt.wåèdr';

$pdf->MultiCell(140, 0, $tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp, 0, 'J', 0, 0, '', '', true, 0,true);    
$pdf->Output('example_051.pdf', 'I');


$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $pdf->SetAutoPageBreak(True, PDF_MARGIN_BOTTOM);

Have you tried:

      $pdf->SetAutoPageBreak(True, PDF_MARGIN_FOOTER);

or

      $pdf->SetAutoPageBreak(True, where_I_want_break);


Like Jason,

I also use the checkPageBreak() function. I am using version 5.9.113 of tcpdf. I had to modify the changePageBreak() function from protected to public. Then in a loop that output each row of data from a database table using MultiCell() with custom width cells, I check for new page, handle accordingly, and it works like a charm. I do not have to use transaction methods to handle new pages.

Here is an excerpt of the looping code that handles it all:

while($data = mysql_fetch_assoc($result))
{
    $pdf->MultiCell(20,6,$data['date'],0,'C',0,0);
    $pdf->MultiCell(40,6,$data['refnumber'],0,'C',0,0);
    $pdf->MultiCell(66,6,$data['memo'],0,'L',0,0);
    $pdf->MultiCell(20,6,$data['linetotal']),0,'R',0,0);
    $pdf->Ln();
    if($pdf->checkPageBreak()) $pdf->Ln();
    }

The checkPageBreak() function returns true when a new page is added. When this is the case, you can perform any additional tasks needed as I did with a new line.

Hope this helps someone else with this MutliCell and custom width issue.

Chance


Same issue here, using version 6.2.0 of the library (so sad to report that rumors that this has been fixed are premature). I liked dwayne towell's approach with Ln, but am nervous about such a systemic change - there may be a subtle MultiCell behavior that gets broken... I also liked Chance's idea of just calling checkPageBreak inside the loop. I found this question while searching to see if TCPDF had a "keep together" option.

I have my own class derived from TCPDF (a burned-too-many-times bias against modifying libraries, I prefer to override), and added this method:

/**
 * Keep the next segment together on one page
 * @param $height (float) the height of the lines to print together
 */
public function keepTogether($height) {
    $this->checkPageBreak($height);
}

I've updated my printing code to include above the print-each-record loop:

// getStringHeight ($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0)
$lineHeight = $this->pdf_printer->getStringHeight(0, 'Test', false, true, '', 1);

and then at the top of the loop

$this->pdf_printer->keepTogether($lineHeight);

This works for me. The print loop uses MultiCell because I need to tweak font sizes in different columns. So I don't have to switch to HTML just to get a better printout.


Had the same issue, but used a method from one of the tcpdf examples that sets the autopagebreak off, writes out the cell, and then turns autopagebreak back on:

    public function WriteCellText( $CellValue, $x, $y,$w, $h){

            $bMargin = $this->getBreakMargin();
            $auto_page_break = $this->AutoPageBreak;
            $this->SetAutoPageBreak(false, 0);
            $this->SetXY($x, $y, true);
            $this->Cell($w,$h, $CellValue, 0, 0, 'L', 0, '', 0, true);
            $this->SetAutoPageBreak($auto_page_break, $bMargin);
    }


The problem is in Ln(). It does not perform a checkPageBreak(). Change the following lines at the end of Ln():

    if (is_string($h)) {
        $this->y += $this->lasth;
    } else {
        $this->y += $h;
    }

to these:

    if (is_string($h))
        $h = $this->lasth;
    if (!$this->checkPageBreak($h))
       $this->y += $h;

I opened a patch ticket on it.


For someone who still has the same problem with page-break TCPDF library

  • You can use <div style="page-break-before:always"></div> OR <br pagebreak="true"/>

to break page manually in your HTML content. Remember that TCPDF only accept the double quote (") for attributes of tags. Don't use single quote (') for your tag.

Use $tcpdf->AddPage() to break page manually in your code.

  • When you set SetAutoPageBreak(TRUE, 10); that means: when the height of document reach to (bottom - 10) then move the cursor to new page. So if you want to have more space, just reduce the number into 0. It will draw until the end of the document without any margin from bottom.


I got around this problem by using the checkPageBreak method, using a while loop. I'm using PHP 4, though. In PHP5, checkPageBreak is a private method. I think its worthwhile that we request it be made public, so that we can get more conttrol over the pages.

I also used the transaction methods so that after checking if I reached a point close to the max page height, I rolled back the transaction and did addPage().

I'll get a copy of my loop here sometime soon. This is a great library, but the auto page break is a pain with the multi-cell features.

-Jason


Pleas try the writeHTMLCell function instead of MultiCell

change

$pdf->MultiCell(140, 0, $tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp."<br><br>".$tmp, 0, 'J', 0, 0, '', '', true, 0,true); 

to

$this->writeHTMLCell(0, 25, 25, 250,'Footer value', 0, 0, 0, true, 'C', true);


Or else you can override the TCPDF footer method like,

// Position at 15 mm from bottom
    $this->SetY(-15);
    // Set font
    $this->SetFont('helvetica', 'B', 8);
//Userdefind Function -Testing

$pg_num = $this->getPage();
$page_height = $this->getPageHeight();
$ph = $page_height;
$this->writeHTMLCell(0, 25, 25, $ph-19.4,'footer data', 0, 0, 0, true, 'C', true);
$this->writeHTMLCell(0, 25, 25, $ph-9.4,'footer data', 0, 0, 0, true, 'L', true);


    $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
    ...
    $pdf->SetMargins(10, 10, 10);
    $pdf->SetAutoPageBreak(true, 10);
    foreach($array as $item)
    {
        $pdf->AddPage(); //add new page for new item
        $txt = some_long_long_text;
        $pdf->Write(0, $txt, '', 0, 'C', true);
        $pdf->endPage(); //do end of page
        $pdf->lastPage(); //set cursor at last page, because autopagebreak not do it
    }

In example, you have 10 pupils in array, and you need create resume for each. In exam, one resume have 3 pages. So in out u get pdf with 30 pages, with correct text.

p.s Sry for my English.


The blank page is not really a real new page because when you add a page, you're still on the white page. So following fixes the problem :

$this->pdf->AddPage('P', 'A4');
$this->pdf->deletePage($this->pdf->getNumPages());
0

精彩评论

暂无评论...
验证码 换一张
取 消