5

我在有24 个标签的A4纸上打印一些文本时遇到问题。

基本上,每一行都有 3 个标签,其中包含一个人的姓名、姓氏和地址,该标签将用于邮件(它是贴在邮件上的粘性标签)。

70*36mm电池

这就是论文。其特点:

  1. 10 行
  2. 第一最后一行是最小的并且有.height:0.5mm;
  3. 第一行和最后一行没有单元格。
  4. 其余所有行都有height:36mm;.
  5. 所有单元格都有width:70mm;height:36mm;
  6. 在每个单元格中都有一个文本,即text-align:center;vertical-align:middle;

我将normalize.css用于css reset

CSS

html,body,table{
  width: 100%;
  height: 100%;
}

.first, .last{
  width: 100%;
  height: 5mm;
}

.row{
  width: 100%;
  height: 36mm;
}

.cell{
  width: 70mm;
  height: 36mm;
  text-align: center;
  vertical-align: middle;
}

我正在使用Chrome并关闭了打印边距

但是,最后两行仍然打印在下一页上。我需要同一页面上的所有 10 行,并且如果有多个页面,它们的位置是固定的(不会移动)。

如何解决/实现?还是有更简单的解决方案?

这是代码示例。

4

4 回答 4

2

我已经使用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<count($data);$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<count($data);$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<$nb){
            $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() 方法。网站中的示例对这些方法进行了很好的解释。

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

于 2013-04-17T15:12:17.780 回答
0

如果您不使用特定的尺寸,请减小单元格和行高的尺寸,即使在关闭边距后会发生什么,打印数据超出纸张长度并且打印机会自动将其移动到另一页。

那是我认为您可以尝试的最佳解决方案。

ps:这些类型的不干胶标签通常附带他们的设计软件光盘,您通常使用它来设置边距和预览。如果你没有它,你唯一能做的就是按照我上面所说的调整高度,在普通 A4 普通纸上打印,然后用粘性标签纸进行远程打印。我曾经也这样做过……虽然对我有用

于 2013-04-11T07:43:08.800 回答
0

如果您的打印机支持此功能,请尝试无边距打印。

于 2013-04-17T08:10:33.060 回答
0

尝试添加下面的样式并使用 @page 属性。@page 中的更多信息可以在这里找到:https ://developer.mozilla.org/en-US/docs/CSS/@page

我很幸运使用下面定义的样式在 8.5x11 文档上完美打印,我可以设置自己的边距并在浏览器中获得一致的结果。

<style type="text/css" media="print">
  @page {
    size: auto;   /* auto is the initial value */
    margin: 0mm;  /* this affects the margin in the printer settings */
  }
  body {
    background-color: #fff;
    border: solid 1px #000 ;
    margin: 0px;  /* this affects the margin on the content before sending to printer */
    padding: 0.25in 0.5in;
  }
</style>
于 2013-04-17T21:40:55.173 回答