'Set TCPDF document as INFINITE height to simulate print to a label roll paper

Is it possible to set a page in TCPDF to set their height to INFINITE? I need it to simulate a print to a roller printer with labels, with a width fixed to 62mm, but the height can be infinite until there are data to print...so...no pagination needed.

$pdf = new TCPDF('L', 'mm', array('62',$infiniteH), true, 'UTF-8', false);

I don't know if it possible to set $infiniteH to a value to simulate my needs

UPDATE: or maybe a method to calculate the height needed and update the TCPDF option?



Solution 1:[1]

You can sort of simulate it using your idea of calculating the needed height and working with that.

  1. Start a transaction.
  2. Add a very tall page.
  3. Run your rendering and calculate the necessary height.
  4. Rollback your transaction.
  5. Add your properly sized page.
  6. Re-run your rendering.

I have some example code of the idea below. I turn off automatic page breaking to prevent unexpected page breaks, but that does set the bottom margin to 0, so I included an extra bottom margin parameter to the function.

<?php
function add_roll_segment($pdf, $maxHeight, $render_callback, $extra_bottom_margin = 1) {
  $pdf->startTransaction();
  $pdf->addPage('P', [$pdf->getPageWidth(), $maxHeight]);
  $render_callback($pdf);
  $margins = $pdf->getMargins();
  $newheight = $pdf->GetY()  + $margins['bottom'] + $extra_bottom_margin;
  $pdf->rollbackTransaction(true);
  $pdf->addPage('P', [$pdf->getPageWidth(), $newheight]);
  $render_callback($pdf);
}


class TransactionRenderer {
  protected $details;

  public function __construct() {
    $this->details = [];
  }

  public function add_transaction_detail($item, $price) {
    $this->details[] = ['item' => $item, 'price' => intval($price)];
  }

  public function render($pdf) {
    $margins = $pdf->getMargins();
    $pdf->setFont('courier', 'B', 8);
    $pdf->SetLineStyle([
      'width' => 0.1,
      'dashed' => [0.5,0.2],
      'color' => [0,0,0,100],
    ]);
    $pdf->Cell(0,0,'Begin Transactions for '.date('Y-m-d H:i:s'),0,1);
    $pdf->SetY($pdf->GetY() + 0.6);
    $pdf->Line($margins['left'], $pdf->getY(), $pdf->getPageWidth() - $margins['right'], $pdf->getY());
    $pdf->SetY($pdf->GetY() + 0.6);
    foreach($this->details as $line) {
      $y = $pdf->getY();
      $pdf->Cell(0,0,$line['item'],0,1,'L');
      $pdf->setY($y);
      $pdf->Cell(0,0,sprintf('%d JPY', $line['price']),0,1,'R');
    }
  }
}

//Populate random data.
$pdf = new TCPDF('L', 'mm', array('62',100), true, 'UTF-8', false);
$renderer = new TransactionRenderer();
$words1 = ['Big', 'Fancy', 'Pretty', 'Cool', 'Awesome'];
$words2 = ['Pants', 'Panda Pot', 'Cup', 'Mug', 'Pie', 'Plate'];
$pdf->SetAutoPageBreak(false);
$count = rand(200, 400);
for($i = 0; $i < $count; $i++) {
  shuffle($words1);
  shuffle($words2);
  $renderer->add_transaction_detail("{$words1[0]} {$words2[0]}", rand(10,10000));
}
add_roll_segment($pdf,19000,array($renderer, 'render'), $pdf->getCellHeight(8));
$pdf->Output('test-59859076.pdf','I');

Edit: I neglected to include a screenshot of the result:

Screenshot of long pdf

Solution 2:[2]

First, you need to predict maximum page height with these 2 steps:

  1. Define details_height = count(details_array) * font_height_you_use_for_each_detail_line

  2. Define $maximum_page_height = top_margin_height + header_height + details_height + footer_height + bottom_margin_height

  3. Then your pdf will be:

    $pdf = new TCPDF('Portrait', 'mm', [62, $maximum_page_height], true, 'UTF-8', false);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Purnomo Fitra