Search code examples
phpfpdf

fpdf set pdf height and oriention to auto


I am adding text to the pdf using fpdf.I am able to add the text but size of pdf page is always A4. If uploaded pdf orientation is landscape it becomes portrait. I want to use the same widht and height and orientation of the uploaded pdf for for writting in to the pdf.

$fullPathToFile = $target_path;

    class custom_PDF extends FPDI {

    var $_tplIdx;
    var $file;

    function Header() {
        if (is_null($this->_tplIdx)) {
        // THIS IS WHERE YOU GET THE NUMBER OF PAGES
        $this->numPages = $this->setSourceFile($this->file);
        $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx, 0, 0, 200);
    }

    function Footer() {

    }

    function setFile($param) {
        $this->file = $param;
    }

    }
    $pdf234 = new custom_PDF();
    $pdf234->setFile($fullPathToFile);
    $pdf234->AddPage();
    $pdf234->SetAutoPageBreak(TRUE, 0);
    $pdf234->SetY(280);
    $pdf234->SetFont("helvetica", "B", 8);
    $pdf234->SetTextColor(0, 0, 0);


    $utf8text = $current_user->user_login . "(" . str_ireplace('_', ' ', $current_user->roles[0]) . ")," . get_bloginfo('name') . "," . get_bloginfo('url') . "" . date("d M Y,h:i:s a");
    $pdf234->Write(5, $utf8text, get_bloginfo('url'));
    if ($pdf234->numPages > 1) {
    for ($i = 2; $i <= $pdf234->numPages; $i++) {
        //$pdf->endPage();
        $pdf234->_tplIdx = $pdf234->importPage($i);
        $pdf234->AddPage();
          $pdf234->SetAutoPageBreak(TRUE, 0);
        $pdf234->SetY(280);

        $pdf234->Write(5, $utf8text, get_bloginfo('url'));
    }
    }
    $pdf234->Output($line['name'], 'D');
    die();

I have tried the following code too:

$specs = $pdf234->getTemplateSize($pdf234->_tplIdx);
 $pdf234->AddPage('L',$specs); 

my pdf in now in landscape mode but content is not occupying whole page.content is aligned to left top.


Solution

  • I found a way to detect uploaded pdf dimension.I used getTemplateSize() function to get width and height to create a layout. Here is my code:

        $fullPathToFile = $target_path;
    
        class custom_PDF extends AlphaPDF {
    
        var $_tplIdx;
        var $file;
    
        function Header() {
            if (is_null($this->_tplIdx)) {
            // THIS IS WHERE YOU GET THE NUMBER OF PAGES
            $this->numPages = $this->setSourceFile($this->file);
            $this->_tplIdx = $this->importPage(1);
            }
            $this->useTemplate($this->_tplIdx, 0, 0);
        }
    
        function Footer() {
    
        }
    
        function setFile($param) {
            $this->file = $param;
        }
    
        function RotatedText($x, $y, $txt, $angle) {
            //Text rotated around its origin
            $this->Rotate($angle, $x, $y);
            $this->Text($x, $y, $txt);
            $this->Rotate(0);
        }
    
        }
    
        $pdf234 = new custom_PDF();
        $pdf2345 = new custom_PDF(); //detect size of uploaded pdf
        $pdf234->setFile($fullPathToFile);
        $pdf2345->setFile($fullPathToFile);
        $pdf2345->AddPage();
        $specs = $pdf2345->getTemplateSize($pdf2345->_tplIdx);
        if ($specs['w'] > $specs['h']) {
        $pdf234->AddPage('L', array($specs['w'], $specs['h']));
        } else {
        $pdf234->AddPage();
        }
        $pdf234->SetAutoPageBreak(TRUE, 0);
        $pdf234->SetAlpha(0.7);
        $pdf234->SetFont('courier', '', 10);
        $pdf234->SetTextColor(0, 0, 0);
        $pdf234->SetDisplayMode('fullpage');
        $utf8text1 = $current_user->user_login . "(" . str_ireplace('_', ' ', $current_user->roles[0]) . ")," . get_bloginfo('name') . "," . get_bloginfo('url');
        $utf8text2 = date("d M Y,h:i:s a") . "," . $current_user->data->user_email;
        $pdf234->SetFont('helvetica', '', 13);
        $pdf234->SetTextColor(128, 128, 128);
        if ($specs['w'] > $specs['h']) {
    
        $pdf234->RotatedText(($specs['w'] / 2) - 50, ($specs['h'] / 2) + 45, $utf8text1, 40);
        $pdf234->RotatedText(($specs['w'] / 2) - 40, ($specs['h'] / 2) + 45, $utf8text2, 40);
        } else {
        $pdf234->RotatedText(45, 180, $utf8text1, 40);
        $pdf234->RotatedText(55, 180, $utf8text2, 40);
        }
        if ($pdf234->numPages > 1) {
        for ($i = 2; $i <= $pdf234->numPages; $i++) {
            //$pdf->endPage();
            $pdf234->_tplIdx = $pdf234->importPage($i);
            $pdf234->AddPage();
            $pdf234->SetAutoPageBreak(TRUE, 0);
            $pdf234->SetY(280);
            $pdf234->SetAlpha(0.7);
            $pdf234->SetFont('helvetica', '', 13);
            $pdf234->SetTextColor(128, 128, 128);
            $pdf234->RotatedText(45, 180, $utf8text1, 40);
            $pdf234->RotatedText(55, 180, $utf8text2, 40);
        }
        }
        $pdf234->Output($line['name'], 'D');
        die();