css打印适应纸张_css – 如何在特定纸张上打印

我已经使用FPDF类为我的标签创建了一个pdf.

require_once ABSPATH . '/path/to/fpdf.php';

class PDF_MC_Table extends FPDF{

var $widths;

var $aligns;

function SetWidths($w){

//Set the array of column widths

$this->widths=$w;

}

function SetAligns($a){

//Set the array of column alignments

$this->aligns=$a;

}

function Row($data){

//Calculate the height of the row

$nb=0;

for($i=0;$i

$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));

$h = 36;// again trial and error until you fnd the desired height of your label

//Issue a page break first if needed

$this->CheckPageBreak($h);

//Draw the cells of the row

for($i=0;$i

$w=$this->widths[$i];

$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';

//Save the current position

$x=$this->GetX();

$y=$this->GetY();

//Draw the border. reset the parameters of the function below as you desire.

$this->Rect($x,$y,$w,$h);

//Print the text. reset the parameters of the function below as you desire. changing the values will resize the boxs.

$this->MultiCell($w,3,$data[$i],0,$a);

//Put the position to the right of the cell. reset the parameters of the function below as you desire. changing the $x and $y will shift the cells.

$this->SetXY($x+$w,$y);

}

//Go to the next line

$this->Ln($h+3);

}

function CheckPageBreak($h){

//If the height h would cause an overflow, add a new page immediately

if($this->GetY()+$h>$this->PageBreakTrigger)

$this->AddPage($this->CurOrientation);

}

function NbLines($w,$txt){

//Computes the number of lines a MultiCell of width w will take

$cw=&$this->CurrentFont['cw'];

if($w==0)

$w=$this->w-$this->rMargin-$this->x;

$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;

$s=str_replace("\r",'',$txt);

$nb=strlen($s);

if($nb>0 and $s[$nb-1]=="\n")

$nb--;

$sep=-1;

$i=0;

$j=0;

$l=0;

$nl=1;

while($i

$c=$s[$i];

if($c=="\n"){

$i++;

$sep=-1;

$j=$i;

$l=0;

$nl++;

continue;

}

if($c==' ')

$sep=$i;

$l+=$cw[$c];

if($l>$wmax){

if($sep==-1){

if($i==$j)

$i++;

}

else

$i=$sep+1;

$sep=-1;

$j=$i;

$l=0;

$nl++;

}

else

$i++;

}

return $nl;

}

}

$pdf=new PDF_MC_Table();

$pdf->SetMargins(4, 2);

$pdf->AddPage();

$pdf->SetFont('Arial','',8);

// displays the empty row in the top

$pdf->SetRightMargin(2);

$pdf->SetLeftMargin(4);

$pdf->Cell(0,10,'',1);

$pdf->Ln(10);

$pdf->SetWidths(array(50,50,50));// these are the widths of your cells. this is a trial and error process. increase the values until you find the suitable ones.

$count = 0;

$lables = array(); // your labels array

$l = array();

$j = 0;

// i used this foreach for breaking my plain array into a 2dimentional array- an array of arrays consisting 3 labels in each.

foreach($lables as $i=>$lbl ){

$l[$j][] = $lbl;

if($i%3==2){$j++;} // $i=0,1,2 > $j=0; $i=3,4,5 > $j=2 etc this will break the main labels array as 2D array.

}

// displays the empty row in the bottom.

$pdf->Ln(1);

$pdf->Cell(0,10,'',1);

$pdf->Output();

有关类和方法的更多信息,请参阅http://www.fpdf.org/最好您需要了解三种方法 – Multicell(),Cell()和Rect()方法.这些方法有很好的解释,网站上有例子.

在这里,我发布了我的解决方案,并根据您的问题更改了一些代码.大多数事情都是自我解释的.如果您需要进一步的帮助,请随时发表评论.谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值