showFooterText = $value; } public function Header() { global $token, $customFont, $date; $watermarkText = $token; $this->SetTextColor(225, 225, 225); // 半透明的灰色 $this->SetFont($customFont, '', 12); $width = $this->getPageWidth(); $height = $this->getPageHeight(); // 計算文字寬度、高度 $textWidth = $this->GetStringWidth($watermarkText, 'dejavusans', '', 12); $textHeight = 8.5; // 字體大小 // 計算需要的行和列以填滿頁面 $cols = intval($width / $textWidth)/0.85 + 1; $rows = intval($height / $textHeight)/0.75 ; for ($i = 0; $i < $rows; $i++) { for ($j = 0; $j < $cols; $j++) { $x = $j * $textWidth *0.83; //每段文字間距 $y = $i * $textHeight * 0.75; // 行高 $this->StartTransform(); $this->Rotate(8, $x + ($textWidth / 2), $y + ($textHeight / 2)); // 在文本的中心点处旋转 $this->Text($x, $y, $watermarkText); $this->StopTransform(); } } } public function Footer() { // 設置字體 global $customFont; $this->SetFont($customFont, '', 12); // 設置字體12pt,不使用斜體 if ($this->showFooterText) { $footerText = '中   華   民   國                     年               月               日'; $this->SetY(-30); $this->writeHTMLCell(0, 10, '', '', $footerText, 0, 1, 0, true, 'C', true); } // 添加頁碼 $pageNumText = '頁 '.$this->getAliasNumPage().' 之 '.$this->getAliasNbPages(); $this->SetY(-15); $this->SetX(48); $this->Cell(0, 10, $pageNumText, 0, false, 'C', 0, '', 0, false, 'T', 'M'); } } $customFont = TCPDF_FONTS::addTTFfont('kaiu.ttf'); $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->setPrintHeader(true); $pdf->setPrintFooter(true); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->setFontSubsetting(true); // 開啟字型 subset $pdf->SetFont($customFont, '', 12); $pdf->setCellHeightRatio(2); $pdf->SetTitle('電梯買賣合約書'); $pdf->AddPage(); // 獲取頁面寬度及高度 $width = $pdf->getPageWidth(); $height = $pdf->getPageHeight(); // 加入 HTML 和 CSS $html = $_POST['css'] . $_POST['html']; // 使用正則表達式找到 xxxx $footer = $_POST['footer']; // 寫入 HTML 到 PDF $pdf->writeHTMLCell($html, true, false, true, false, ''); // $pdf->AddPage(); // $pdf->setShowFooterText(true); // $pdf->writeHTML($footer, true, false, true, false, ''); // $pdf->setPrintFooter(true); $pdf->lastPage(); $totalPages = $pdf->getNumPages(); //重新生成 $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->setPrintHeader(true); $pdf->setPrintFooter(true); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->setFontSubsetting(true); // 開啟字型 subset $pdf->SetFont($customFont, '', 12); $pdf->setCellHeightRatio(2); $pdf->SetTitle('電梯買賣合約書'); $pdf->AddPage(); // 獲取頁面寬度及高度 $width = $pdf->getPageWidth(); $height = $pdf->getPageHeight(); // 加入 HTML 和 CSS $footer = $_POST['footer']; // 寫入 HTML 到 PDF $pdf->writeHTML($html.$footer, true, false, true, false, ''); // $pdf->AddPage(); // $pdf->setShowFooterText(true); $pdf->lastPage(); $newTotalPages = $pdf->getNumPages(); // $pdf->writeHTML($footer.$totalPages.$newTotalPages, true, false, true, false, ''); // $pdf->setPrintFooter(true); if($newTotalPages === $totalPages){ $pdf->setPrintFooter(true); $pdf->setShowFooterText(true); }else{ $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->setPrintHeader(true); $pdf->setPrintFooter(true); $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->setFontSubsetting(true); // 開啟字型 subset $pdf->SetFont($customFont, '', 12); $pdf->setCellHeightRatio(2); $pdf->SetTitle('電梯買賣合約書'); $pdf->AddPage(); // 獲取頁面寬度及高度 $width = $pdf->getPageWidth(); $height = $pdf->getPageHeight(); // 加入 HTML 和 CSS $footer = $_POST['footer']; // 寫入 HTML 到 PDF $pdf->writeHTML($html, true, false, true, false, ''); $pdf->AddPage(); $pdf->setShowFooterText(true); $pdf->lastPage(); $totalPages = $pdf->getNumPages(); $pdf->writeHTML($footer, true, false, true, false, ''); $pdf->setPrintFooter(true); } $pdf->AddPage(); $pdf->setCellHeightRatio(1.8); $pdf->setShowFooterText(false); $pdf->writeHTML($_POST['affix1'], true, false, true, false, ''); $pdf->setPrintFooter(false); // 設置 HTTP header以輸出 PDF 文件 header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="tcpdf10.pdf"'); // 輸出 PDF 文件内容 // $pdf->Output('tcpdf10.pdf', 'I'); $scriptPath = __DIR__; // 設置要保存的 PDF 檔名 $pdfFilename = "output.pdf"; // 完整的文件路径 $fullPath = $scriptPath . '/pdfs/' . $formattedDate . '.pdf'; $pdf->Output($fullPath, 'F'); // 保存 PDF 文件到當前所在的目錄 $pdf->Output('output.pdf', 'I'); }