fpdf 将 PDF 高度和方向设置为自动


fpdf set pdf height and oriention to auto

我正在使用fpdf向pdf添加文本。我可以添加文本,但pdf页面的大小始终为A4。如果上传的pdf方向是横向的,则变为纵向。我想使用与上传的 pdf 相同的宽度、高度和方向来写入 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();

我也尝试了以下代码:

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

我的 PDF 现在处于横向模式,但内容没有占据整个页面。内容与左上对齐。

您可以在构造函数中设置维度:

FPDF([string orientation [, string unit [, mixed size]]])

示例

$pagesize = array(107, 199);
$pdf234 = new FPDF( 'P', 'mm', $pagesize );

或与:

AddPage([string orientation [, mixed size]]);

例:

$pdf234->AddPage('P','A5');

描述

向文档添加新页面。

参数

取向

页面方向。可能的值为(不区分大小写):

  • P 或肖像
  • L 或横向

默认值是传递给构造函数的值。

大小

页面大小。它可以是以下值之一(不区分大小写):

  • 答3
  • 答4
  • 答5
  • 法律

或包含宽度和高度的数组(以用户单位表示)。

默认值是传递给构造函数的值。

我找到了一种方法来检测上传的pdf尺寸。我使用 getTemplateSize() 函数来获取宽度和高度以创建布局。这是我的代码:

    $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();