使用setasign/FPDF和setasign/FPDI庫,我需要在任何PDF文件上添加浮水印和自訂頁腳疊加(每個頁面都需要加浮水印)。原始的PDF文件儲存在伺服器上,並且該過程必須在文件請求時進行(因為它們包含請求的日期和時間)。
我成功地添加了所需的頁腳,包括請求的日期和時間,感謝以下程式碼。該程式碼還在頁面上對角線上列印了水印的單一出現,但是我真的希望有另一種行為:以對角線方式重複字串,就像在這個圖像上所示(例如,我的字串是動態生成的)。字串是否從頁面的"外部"開始或結束並不重要,只要它在多行上重複出現,並且這些行必須等距離排列。
您有任何關於從哪裡開始的線索嗎?
截至今天的工作代碼:
SetY(-10); $this->SetFont('Arial', false, 8); $this->SetTextColor(28, 28, 28); $this->Cell(0, 15, 'File requested on : ' . date('r'), 0, 0, 'C'); } } function addWatermark($x, $y, $watermarkText, $angle, $pdf) { $angle = $angle * M_PI / 180; $c = cos($angle); $s = sin($angle); $cx = $x * 1; $cy = (300 - $y) * 1; $pdf->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, - $s, $c, $cx, $cy, -$cx, -$cy)); $pdf->Text($x, $y, $watermarkText); $pdf->_out('Q'); } $pdf = new Watermarked_PDF(); $file_Path = 'documents/'; $file_Name = '13825_2023-07-04'; $pages_count = $pdf->setSourceFile($file_Path . $file_Name . '.pdf'); for ($i = 1; $i <= $pages_count; $i ) { $pdf->AddPage(); $tplIdx = $pdf->importPage($i); $pdf->useTemplate($tplIdx, 0, 0); $pdf->SetFont('Arial', 'B', 15); $pdf->SetTextColor(175, 175, 175); $watermarkText = 'file #' . $file_Name . ' - propery of company'; addWatermark(120, 220, $watermarkText, 45, $pdf); $pdf->SetXY(25, 25); } $pdf->Output();
來自這裡的水印程式碼:https://phppot.com/php/php-watermark-pdf/ 並進行了適應。
測試的基本composer.json檔案:
< /p>
{ "require": { "setasign/fpdf": "1.8.*", "setasign/fpdi": "^2.3" } }
多謝了
我實際上找到了一個解決辦法,但是它相當醜陋:
即使這個更新的循環並不是最佳化的,它也能完成工作。我現在會按照這種方式繼續工作,並根據我的需求進行調整。