【问题标题】:How to get total pages of PDF with FPDF?如何使用 FPDF 获取 PDF 的总页数?
【发布时间】:2020-07-06 14:54:48
【问题描述】:

我尝试使用 AliasNbPage() 来获取 pdf 的总页数。我确实调用了“{nb}”来获取总页数,但它没有给出输出数字而不是 {nb}。还有另一种方法可以获得总页数吗?

    $tot = strval(explode('/', strval($this->pdf->PageNo().'/{nb}'))[1]);
    $this->pdf->SetX(44);
    $this->pdf->Cell(40,5,": ".$tot.' Pages',0,1);
    //while i'm tried to convert the number to the text, it doesn't show.
    $this->pdf->Cell(40,5,": ".numb_to_text(intval($tot)).' Pages',0,1);
function init_number($nilai) {
            $nilai = abs($nilai);
            $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
            $temp = "";
            if ($nilai < 12) {
                $temp = " ". $huruf[$nilai];
            } else if ($nilai <20) {
                $temp = $this->init_number($nilai - 10). " belas";
            } else if ($nilai < 100) {
                $temp = $this->init_number($nilai/10)." puluh". $this->init_number($nilai % 10);
            } else if ($nilai < 200) {
                $temp = " seratus" . $this->init_number($nilai - 100);
            } else if ($nilai < 1000) {
                $temp = $this->init_number($nilai/100) . " ratus" . $this->init_number($nilai % 100);
            } else if ($nilai < 2000) {
                $temp = " seribu" . $this->init_number($nilai - 1000);
            } else if ($nilai < 1000000) {
                $temp = $this->init_number($nilai/1000) . " ribu" . $this->init_number($nilai % 1000);
            } else if ($nilai < 1000000000) {
                $temp = $this->init_number($nilai/1000000) . " juta" . $this->init_number($nilai % 1000000);
            } else if ($nilai < 1000000000000) {
                $temp = $this->init_number($nilai/1000000000) . " milyar" . $this->init_number(fmod($nilai,1000000000));
            } else if ($nilai < 1000000000000000) {
                $temp = $this->init_number($nilai/1000000000000) . " trilyun" . $this->init_number(fmod($nilai,1000000000000));
            }     
            return $temp;
        }

    function numb_to_text($nilai) {
        $nilai = (int) $nilai;
        if($nilai<0) {
            $hasil = "minus ". trim($this->init_number($nilai));
        } else {
            $hasil = trim($this->init_number($nilai));
        }           
        return $hasil;
    }

【问题讨论】:

  • 请向我们展示您的代码。
  • AliasNbPage 返回的别名将替换为文档关闭时的实际页数。见FPDF documentation
  • 我尝试过这样做。 $tot = strval(explode('/', strval($this->pdf->PageNo().'/{nb}'))[1]);回声($tot);输出:2 当我输入我的函数以获取返回转换为文本时,它不会出现。我的函数将数字转换为文本没有问题,例如:conv(1) 输出:一个。

标签: php fpdf


【解决方案1】:

只有在文档完成之前才能知道总页数。例如:

$pdf = new FPDF();
$pdf->AddPage();
$pdf->AddPage();
$nb = $pdf->PageNo();
$pdf->Output();

$nb 包含 2。

【讨论】:

    猜你喜欢
    • 2023-02-12
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    相关资源
    最近更新 更多