2014-02-27 84 views
-1

我正在使用字符串生成动态多单元。像A,B,C等。我想把那些多单元放在页面上。我需要制作$ pdf-> SetMargins()动态,这意味着它会在内容加载时自动居中。这是我试过的代码,但有些错误。将PDF的内容设置为中心

$pdfWidth = 210; 
col = 9; // This is dynamic so it can be any value from 5-20; 
    $mar = (($pdfWidth + ($col * 8)) /2)/2; 
    $pdf->SetMargins($mar,0,0); 
+0

“* Something * is wrong。”不知何故,你忘了说什么。 – usr2564301

回答

1

我不确定你为什么要除以2两次。如果您将页面的总宽度减去内容并将其除以2,则您已经拥有了所需的边距。另外,不要忘记将SetMargins()中的override参数设置为'true'。

$pdfWidth = 210; 
$col = 9; 
$mar = (($pdfWidth - ($col*8)) /2); //Only one division by 2 is required 
$pdf->SetMargins($mar,0,0, true); //the 'true' is necessary or it won't override the default margins 

    //VERY IMPORTANT that you set all the above BEFORE adding the page! 

$pdf->AddPage(); 

    //Content of page 

现在任何单元格宽度为8的MultiCell(如您在示例中提供的)应该完全位于页面的中心。