我设法使用 url page 中使用的类来旋转我的 pdf 中的文本
基本上,我创建了一个类,其函数以 FPDI 作为父级,并导入到我的代码中,而不是
// Your code
$pdf = new FPDI('P','mm',array(225.37,261.719));
// Import changed
$pdf = new PDF_Rotate('P','mm',array(225.37,261.719));
那我就可以用了
$pdf->SetXY(2, -5);
$pdf->RotatedText(5,250,"Example of rotated text",90);
完整代码如下:
PDF_Rotate.php(从 FPDI 导入而不是 FPDF 作为示例)
class PDF_Rotate extends \FPDI
{
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();
}
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 RotatedImage($file,$x,$y,$w,$h,$angle)
{
//Image rotated around its upper-left corner
$this->Rotate($angle,$x,$y);
$this->Image($file,$x,$y,$w,$h);
$this->Rotate(0);
}
}
在另一个文件中,导入类。
$pdf = new PDF_Rotate();
$pdf->SetXY(2, -5);
$pdf->RotatedText(5,250,"Example of rotated text",90);