3

我有一个分页符的小问题。Multicell 显示在第一页的页脚上,然后中断:如何设置页面的底部边距,以便中断发生在上面?这是示例 PDF:示例和这里的源代码:

<?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');
4

11 回答 11

8

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

你有没有尝试过:

      $pdf->SetAutoPageBreak(True, PDF_MARGIN_FOOTER);

或者

      $pdf->SetAutoPageBreak(True, where_I_want_break);
于 2010-02-08T14:11:43.680 回答
4

像杰森一样,

我还使用 checkPageBreak() 函数。我正在使用 tcpdf 版本 5.9.113。我不得不将 changePageBreak() 函数从受保护修改为公开。然后在使用 MultiCell() 和自定义宽度单元格从数据库表中输出每一行数据的循环中,我检查新页面,进行相应处理,它就像一个魅力。我不必使用事务方法来处理新页面。

这是处理这一切的循环代码的摘录:

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();
    }

添加新页面时,checkPageBreak() 函数返回 true。在这种情况下,您可以执行所需的任何其他任务,就像我对新行所做的那样。

希望这可以帮助其他人解决这个 MutliCell 和自定义宽度问题。

机会

于 2013-09-08T06:31:23.537 回答
2

同样的问题,使用库的 6.2.0 版本(很遗憾地报告说这个问题已经得到修复还为时过早)。我喜欢 dwayne towell 对 Ln 的方法,但对这样的系统性变化感到紧张 - 可能会有一个微妙的 MultiCell 行为被破坏......我也喜欢 Chance 在循环内调用 checkPageBreak 的想法。我在搜索 TCPDF 是否有“保持在一起”选项时发现了这个问题。

我有自己的从 TCPDF 派生的类(对修改库的多次偏见,我更喜欢重写),并添加了这个方法:

/**
 * 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);
}

我已经更新了我的打印代码以包含在 print-each-record 循环之上:

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

然后在循环的顶部

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

这对我有用。打印循环使用 MultiCell,因为我需要调整不同列中的字​​体大小。所以我不必为了获得更好的打印输出而切换到 HTML。

于 2015-07-03T15:57:06.347 回答
1

问题出在Ln(). 它不执行checkPageBreak(). 在末尾更改以下行Ln()

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

对这些:

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

我在上面开了一张补丁票

于 2014-10-17T18:30:44.247 回答
1

有同样的问题,但使用了一个 tcpdf 示例中的方法,该方法将 autopagebreak 设置为关闭,写出单元格,然后重新打开 autopagebreak:

    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);
    }
于 2011-10-11T16:25:02.483 回答
1

对于仍然对分页符 TCPDF 库有同样问题的人

  • 您可以使用<div style="page-break-before:always"></div><br pagebreak="true"/>

在您的 HTML 内容中手动分页。请记住,TCPDF 只接受"标签属性的双引号 ( )。不要'在标签中使用单引号 ( )。

用于$tcpdf->AddPage()在代码中手动分页。

  • 当你设置SetAutoPageBreak(TRUE, 10);这意味着:当文档的高度达到(底部 - 10)然后将光标移动到新页面。所以如果你想有更多的空间,只需将数字减少到 0。它会一直绘制到文档的末尾,从底部没有任何边距。
于 2019-11-28T16:05:47.977 回答
0
    $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
    }

例如,您有 10 个学生,您需要为每个学生创建简历。在考试中,一份简历有 3 页。因此,您将获得 30 页的 pdf,并带有正确的文本。

ps对不起我的英语。

于 2014-06-10T09:21:52.420 回答
0

请尝试 writeHTMLCell 函数而不是 MultiCell

改变

$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); 

$this->writeHTMLCell(0, 25, 25, 250,'Footer value', 0, 0, 0, true, 'C', true);
于 2013-05-22T06:38:59.373 回答
0

我通过使用 checkPageBreak 方法和 while 循环解决了这个问题。不过,我使用的是 PHP 4。在 PHP5 中,checkPageBreak 是一个私有方法。我认为我们要求将其公开是值得的,以便我们可以对页面进行更多控制。

我还使用了事务方法,以便在检查是否达到接近最大页面高度的点后,我回滚事务并执行 addPage()。

我很快就会在这里得到我的循环的副本。这是一个很棒的库,但是自动分页符对于多单元格功能来说是一个痛苦。

-杰森

于 2010-02-19T17:04:43.523 回答
0

或者,您可以覆盖 TCPDF 页脚方法,例如,

// 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);
于 2013-05-22T06:42:28.190 回答
0

空白页面并不是真正的新页面,因为当您添加页面时,您仍然在白页面上。所以以下解决了这个问题:

$this->pdf->AddPage('P', 'A4');
$this->pdf->deletePage($this->pdf->getNumPages());
于 2017-04-06T12:46:39.963 回答