Skip to content

Instantly share code, notes, and snippets.

@Harmageddon
Last active May 17, 2019 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Harmageddon/3d95cc38ce4173d9be2119d7e20b156e to your computer and use it in GitHub Desktop.
Save Harmageddon/3d95cc38ce4173d9be2119d7e20b156e to your computer and use it in GitHub Desktop.
Example of scaled MultiCells in TCPDF
<?php
/**
* Adapted from https://github.com/tecnickcom/TCPDF/blob/master/examples/example_013.php
**/
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', 'B', 20);
// add a page
$pdf->AddPage();
$m = $pdf->getMargins();
$pdf->Rect($m['left'], $m['top'], $pdf->getPageWidth() - $m['left'] - $m['right'], $pdf->getPageHeight() - $m['top'] - $m['bottom']);
$pdf->Write(0, 'Transformation: Scale 50% at (50, 50)', '', 0, 'C', 1, 0, false, false, 0);
// set font
$pdf->SetFont('helvetica', '', 10);
// --- Scaling ---------------------------------------------
$pdf->SetDrawColor(0);
$pdf->SetTextColor(0);
$pdf->Circle(50, 50, 1, 0, 360, 'F', array(), array(0, 200, 0));
// Start Transformation
$pdf->StartTransform();
// Scale by 50% centered by (50,50) which is the center of the green dot.
$pdf->ScaleXY(50, 50, 50);
$text = 'Fuga eum tenetur quas quo corrupti. Accusamus laboriosam consectetur ab autem porro neque ab. Occaecati cupiditate repellendus rerum eveniet'
. 'id voluptatem porro inventore. Aliquam dicta rerum quae accusamus laudantium exercitationem magni..
';
$pdf->MultiCell(100, 50, "Box 100x50mm at (50, 50)\nPositioned using MultiCell parameters\n\n" . $text, 1, 'L', false, 0, 50, 50);
$pdf->MultiCell(100, 50, "Box 100x50mm at (230, 50)\nPositioned using MultiCell parameters\n\n" . $text, 1, 'L', false, 0, 230, 50);
$pdf->SetXY(110, 100);
$pdf->MultiCell(100, 50, "Box 100x50mm at (110, 100)\nPositioned using SetXY\n\n" . $text, 1, 'L', false, 0);
$pdf->MultiCell(100, 50, "Box 100x50mm at (210, 100)\nPositioned using cursor position without line break\n\n" . $text, 1, 'L', false, 0);
$pdf->SetXY(110, 150);
$pdf->MultiCell(120, 50, "Box 120x50mm at (110, 150)\nPositioned using SetXY\n\n" . $text, 1, 'L', false, 0);
$pdf->MultiCell(100, 50, "Box 100x50mm at (230, 150)\nPositioned using cursor position without line break\n\n" . $text, 1, 'L', false, 0);
$pdf->SetXY(110, 200);
$pdf->MultiCell(120, 50, "Box 120x50mm at (110, 200)\nPositioned using SetXY\n\n" . $text, 1, 'L', false, 0);
$pdf->SetXY(230, 200);
$pdf->MultiCell(100, 50, "Box 100x50mm at (230, 200)\nPositioned using SetXY\n\n" . $text, 1, 'L', false, 0);
// Stop Transformation
$pdf->StopTransform();
$pdf->MultiCell(100, 50, "100% Box 100x50mm at (150, 200)\n\n" . $text, 1, 'L', false, 1, 150, 200);
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_013.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment