Skip to content
Advertisement

TCPDF page size change is creating several pages when all I want is one

As you can see in my code; I am just trying to create a PDF document 4 inches wide and 3 inches tall.

// create new PDF document
$pagelayout = array(3, 4); //  or array($height, $width) 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, 'in', $pagelayout, true, 'UTF-8', false);

However, when I do this, my PDF renders with duplicate pages (full code follows):

enter image description here

What I am trying to achieve is this:

enter image description here

How can I make TCPDF create just one 4″ x 3″ image like I have done above?

Here is my full code for TCPDF:

<?php

// Include the main TCPDF library (search for installation path).
require_once('../library/tcpdf/config/tcpdf_config.php');
require_once('../library/tcpdf/tcpdf.php');

// create new PDF document
$pagelayout = array(3, 4); //  or array($height, $width) 
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, 'in', $pagelayout, true, 'UTF-8', false);

/*
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
*/
// set default header data
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// 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 some language-dependent strings (optional)
if (@file_exists('../library/tcpdf/examples/lang/eng.php')) {
    require_once('../library/tcpdf/examples/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

// Set some content to print
$html = <<<EOD
<p>test</p>
EOD;

// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_001.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

Advertisement

Answer

In fact, this was the answer:

$width = $pdf->pixelsToUnits(400); 
$height = $pdf->pixelsToUnits(300);

$resolution= array($width, $height);
$pdf->AddPage('L', $resolution);

Ref. TCPDF Qr code resize on small size page

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement