FPDF生成后插入水印打印PDF
inserting the watermark and printing the PDF after generating by FPDF
我已经使用 FPDF 库生成了一份 PDF。现在我无法获得在 PDF 上插入水印和打印 PDF 的任何线索。我尝试了 ne 上可用的不同方法,但我无法实现它,非常感谢任何帮助。我的代码是:
<?php
require_once("includes/config.php");
//
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
class mypdf extends FPDF{
function Heeader()
{
function Header()
{
/* Put the watermark */
$this->SetFont('Arial','B',50);
$this->SetTextColor(255,192,203);
$this->RotatedText(35,190,'W a t e r m a r k d e m o',45);
}
function RotatedText($x, $y, $txt, $angle)
{
/* Text rotated around its origin */
$this->Rotate($angle,$x,$y);
$this->Text($x,$y,$txt);
$this->Rotate(0);
}
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(179);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Text color in gray
$this->SetTextColor(128);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
function headerTable(){
$this->setfont('Arial','BU',14);
$this->Ln();
$this->cell(270,14,'List Of Records' ,0,0,'C');
$this->Ln();
$currentDate = date("j/n/Y");
$this->cell(270,14,$currentDate,0,0,'R');
$this->Ln();
$this->Ln();
$this->setfont('Arial','B',12);
$this->cell(12,10,'Sl No',1,0,'L');
$this->cell(75,10,'USER NAME' ,1,0,'c');
$this->cell(80,10,'AFFILIATION',1,0,'c');
$this->cell(34,10,'SAMPLE TYPE' ,1,0,'c');
$this->cell(30,10,'SAMPLE ID',1,0,'c');
$this->cell(50,10,'DATE',1,0,'c');
$this->Ln();
}
function viewTable(){
$this->setfont('Arial','B',12);
$search=$_POST['search1'];
$option=$_POST['option1'];
$period=$_POST['period1'];
$datefrom=$_POST['datefrom1'];
$dateto=$_POST['dateto1'];
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
if($period ==null){
$sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date
FROM tblfacility
JOIN tbluser on tbluser.id=tblfacility.user
where ".$search." ='".$option."' ";}
else{
$sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
}
$query = $dbh->prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
//echo "<prep>";
//echo "this is the final";
//print_r($sql);
$i=1;
foreach($results as $result){
$this->cell(12,10,$i,1,0,'L');
$this->cell(75,10,$result->user,1,0,'L');
$this->cell(80,10,$result->affiliation,1,0,'L');
$this->cell(34,10,$result->type,1,0,'L');
$this->cell(30,10,$result->sampleid,1,0,'L');
$this->cell(50,10,$result->time,1,0,'L');
$this->Ln();
$i++;
}
}
}
$pdf = new mypdf();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->header();
$pdf->headerTable();
$pdf->viewTable();
$pdf->footer();
$pdf->output();
?>
我的代码可能有什么问题
我认为您正在尝试使用 http://www.fpdf.org/en/script/script9.php
中的示例
但是这个脚本正在使用另一个 class "PDF_Rotate",您的 "mypdf" class 应该继承它。
你的 Heeader() 函数也有问题,根本不应该存在。
我在下面的脚本中添加了 PDF_Rotate class 定义(来自 http://www.fpdf.org/en/script/script9.php )并删除了不必要的 Heeader 函数声明:
<?php
require_once("includes/config.php");
//
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
class PDF_Rotate extends FPDF
{
var $angle=0;
function Rotate($angle,$x=-1,$y=-1)
{
if ($x == - 1)
$x = $this->x;
if ($y == - 1)
$y = $this->y;
if ($this->angle != 0)
$this->_out('Q');
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI / 180;
$c = cos($angle);
$s = sin($angle);
$cx = $x * $this->k;
$cy = ($this->h - $y) * $this->k;
$this->_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));
}
}
function _endpage()
{
if($this->angle!=0)
{
$this->angle=0;
$this->_out('Q');
}
parent::_endpage();
}
}
class mypdf extends PDF_Rotate
{
function Header()
{
/* Put the watermark */
$this->SetFont('Arial', 'B', 50);
$this->SetTextColor(255, 192, 203);
$this->RotatedText(35, 190, 'W a t e r m a r k d e m o', 45);
}
function RotatedText($x, $y, $txt, $angle)
{
/* Text rotated around its origin */
$this->Rotate($angle, $x, $y);
$this->Text($x, $y, $txt);
$this->Rotate(0);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(179);
// Arial italic 8
$this->SetFont('Arial', 'I', 8);
// Text color in gray
$this->SetTextColor(128);
// Page number
$this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
}
function headerTable()
{
$this->setfont('Arial', 'BU', 14);
$this->Ln();
$this->cell(270, 14, 'List Of Records', 0, 0, 'C');
$this->Ln();
$currentDate = date("j/n/Y");
$this->cell(270, 14, $currentDate, 0, 0, 'R');
$this->Ln();
$this->Ln();
$this->setfont('Arial', 'B', 12);
$this->cell(12, 10, 'Sl No', 1, 0, 'L');
$this->cell(75, 10, 'USER NAME', 1, 0, 'c');
$this->cell(80, 10, 'AFFILIATION', 1, 0, 'c');
$this->cell(34, 10, 'SAMPLE TYPE', 1, 0, 'c');
$this->cell(30, 10, 'SAMPLE ID', 1, 0, 'c');
$this->cell(50, 10, 'DATE', 1, 0, 'c');
$this->Ln();
}
function viewTable()
{
$this->setfont('Arial', 'B', 12);
$search = $_POST['search1'];
$option = $_POST['option1'];
$period = $_POST['period1'];
$datefrom = $_POST['datefrom1'];
$dateto = $_POST['dateto1'];
$dbh = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
if ($period == null) {
$sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date
FROM tblfacility
JOIN tbluser on tbluser.id=tblfacility.user
where " . $search . " ='" . $option . "' ";
} else {
$sql = "SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where " . $search . " ='" . $option . "' AND time between '" . $datefrom . "' and '" . $dateto . "' ";
}
$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
// echo "<prep>";
// echo "this is the final";
// print_r($sql);
$i = 1;
foreach ($results as $result) {
$this->cell(12, 10, $i, 1, 0, 'L');
$this->cell(75, 10, $result->user, 1, 0, 'L');
$this->cell(80, 10, $result->affiliation, 1, 0, 'L');
$this->cell(34, 10, $result->type, 1, 0, 'L');
$this->cell(30, 10, $result->sampleid, 1, 0, 'L');
$this->cell(50, 10, $result->time, 1, 0, 'L');
$this->Ln();
$i++;
}
}
}
$pdf = new mypdf();
$pdf->AliasNbPages();
$pdf->AddPage('L', 'A4', 0);
$pdf->header();
$pdf->headerTable();
$pdf->viewTable();
$pdf->footer();
$pdf->output();
我已经使用 FPDF 库生成了一份 PDF。现在我无法获得在 PDF 上插入水印和打印 PDF 的任何线索。我尝试了 ne 上可用的不同方法,但我无法实现它,非常感谢任何帮助。我的代码是:
<?php
require_once("includes/config.php");
//
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
class mypdf extends FPDF{
function Heeader()
{
function Header()
{
/* Put the watermark */
$this->SetFont('Arial','B',50);
$this->SetTextColor(255,192,203);
$this->RotatedText(35,190,'W a t e r m a r k d e m o',45);
}
function RotatedText($x, $y, $txt, $angle)
{
/* Text rotated around its origin */
$this->Rotate($angle,$x,$y);
$this->Text($x,$y,$txt);
$this->Rotate(0);
}
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(179);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Text color in gray
$this->SetTextColor(128);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
function headerTable(){
$this->setfont('Arial','BU',14);
$this->Ln();
$this->cell(270,14,'List Of Records' ,0,0,'C');
$this->Ln();
$currentDate = date("j/n/Y");
$this->cell(270,14,$currentDate,0,0,'R');
$this->Ln();
$this->Ln();
$this->setfont('Arial','B',12);
$this->cell(12,10,'Sl No',1,0,'L');
$this->cell(75,10,'USER NAME' ,1,0,'c');
$this->cell(80,10,'AFFILIATION',1,0,'c');
$this->cell(34,10,'SAMPLE TYPE' ,1,0,'c');
$this->cell(30,10,'SAMPLE ID',1,0,'c');
$this->cell(50,10,'DATE',1,0,'c');
$this->Ln();
}
function viewTable(){
$this->setfont('Arial','B',12);
$search=$_POST['search1'];
$option=$_POST['option1'];
$period=$_POST['period1'];
$datefrom=$_POST['datefrom1'];
$dateto=$_POST['dateto1'];
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
if($period ==null){
$sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date
FROM tblfacility
JOIN tbluser on tbluser.id=tblfacility.user
where ".$search." ='".$option."' ";}
else{
$sql="SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where ".$search." ='".$option."' AND time between '".$datefrom."' and '".$dateto."' ";
}
$query = $dbh->prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
//echo "<prep>";
//echo "this is the final";
//print_r($sql);
$i=1;
foreach($results as $result){
$this->cell(12,10,$i,1,0,'L');
$this->cell(75,10,$result->user,1,0,'L');
$this->cell(80,10,$result->affiliation,1,0,'L');
$this->cell(34,10,$result->type,1,0,'L');
$this->cell(30,10,$result->sampleid,1,0,'L');
$this->cell(50,10,$result->time,1,0,'L');
$this->Ln();
$i++;
}
}
}
$pdf = new mypdf();
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->header();
$pdf->headerTable();
$pdf->viewTable();
$pdf->footer();
$pdf->output();
?>
我的代码可能有什么问题
我认为您正在尝试使用 http://www.fpdf.org/en/script/script9.php
中的示例但是这个脚本正在使用另一个 class "PDF_Rotate",您的 "mypdf" class 应该继承它。
你的 Heeader() 函数也有问题,根本不应该存在。
我在下面的脚本中添加了 PDF_Rotate class 定义(来自 http://www.fpdf.org/en/script/script9.php )并删除了不必要的 Heeader 函数声明:
<?php
require_once("includes/config.php");
//
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdf\fpdf.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\autoload.php');
require('C:\xampp\htdocs\geochronology\vendor\setasign\fpdi\src\fpdi.php');
class PDF_Rotate extends FPDF
{
var $angle=0;
function Rotate($angle,$x=-1,$y=-1)
{
if ($x == - 1)
$x = $this->x;
if ($y == - 1)
$y = $this->y;
if ($this->angle != 0)
$this->_out('Q');
$this->angle = $angle;
if ($angle != 0) {
$angle *= M_PI / 180;
$c = cos($angle);
$s = sin($angle);
$cx = $x * $this->k;
$cy = ($this->h - $y) * $this->k;
$this->_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));
}
}
function _endpage()
{
if($this->angle!=0)
{
$this->angle=0;
$this->_out('Q');
}
parent::_endpage();
}
}
class mypdf extends PDF_Rotate
{
function Header()
{
/* Put the watermark */
$this->SetFont('Arial', 'B', 50);
$this->SetTextColor(255, 192, 203);
$this->RotatedText(35, 190, 'W a t e r m a r k d e m o', 45);
}
function RotatedText($x, $y, $txt, $angle)
{
/* Text rotated around its origin */
$this->Rotate($angle, $x, $y);
$this->Text($x, $y, $txt);
$this->Rotate(0);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(179);
// Arial italic 8
$this->SetFont('Arial', 'I', 8);
// Text color in gray
$this->SetTextColor(128);
// Page number
$this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
}
function headerTable()
{
$this->setfont('Arial', 'BU', 14);
$this->Ln();
$this->cell(270, 14, 'List Of Records', 0, 0, 'C');
$this->Ln();
$currentDate = date("j/n/Y");
$this->cell(270, 14, $currentDate, 0, 0, 'R');
$this->Ln();
$this->Ln();
$this->setfont('Arial', 'B', 12);
$this->cell(12, 10, 'Sl No', 1, 0, 'L');
$this->cell(75, 10, 'USER NAME', 1, 0, 'c');
$this->cell(80, 10, 'AFFILIATION', 1, 0, 'c');
$this->cell(34, 10, 'SAMPLE TYPE', 1, 0, 'c');
$this->cell(30, 10, 'SAMPLE ID', 1, 0, 'c');
$this->cell(50, 10, 'DATE', 1, 0, 'c');
$this->Ln();
}
function viewTable()
{
$this->setfont('Arial', 'B', 12);
$search = $_POST['search1'];
$option = $_POST['option1'];
$period = $_POST['period1'];
$datefrom = $_POST['datefrom1'];
$dateto = $_POST['dateto1'];
$dbh = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
if ($period == null) {
$sql = "SELECT tbluser.user,tbluser.affiliation,tblfacility.type,tblfacility.sampleid,tblfacility.time,DATE_FORMAT(tblfacility.time, '%d-%m-%y') AS formatted_date
FROM tblfacility
JOIN tbluser on tbluser.id=tblfacility.user
where " . $search . " ='" . $option . "' ";
} else {
$sql = "SELECT * FROM tblfacility JOIN tbluser on tbluser.id=tblfacility.user where " . $search . " ='" . $option . "' AND time between '" . $datefrom . "' and '" . $dateto . "' ";
}
$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
// echo "<prep>";
// echo "this is the final";
// print_r($sql);
$i = 1;
foreach ($results as $result) {
$this->cell(12, 10, $i, 1, 0, 'L');
$this->cell(75, 10, $result->user, 1, 0, 'L');
$this->cell(80, 10, $result->affiliation, 1, 0, 'L');
$this->cell(34, 10, $result->type, 1, 0, 'L');
$this->cell(30, 10, $result->sampleid, 1, 0, 'L');
$this->cell(50, 10, $result->time, 1, 0, 'L');
$this->Ln();
$i++;
}
}
}
$pdf = new mypdf();
$pdf->AliasNbPages();
$pdf->AddPage('L', 'A4', 0);
$pdf->header();
$pdf->headerTable();
$pdf->viewTable();
$pdf->footer();
$pdf->output();