10

私の学校の課題のソリューションはすべて、PDFにユーザー名が記載されたウォータースタンプが付いています。

PHPを使ってそのようなことをする方法を皆さんが知っているかどうか疑問に思いましたか?ダウンロードプロセスの前にスクリプトを実行しますか?

ありがとう。

4

4 に答える 4

6

PHP 用の非常に優れた PDF ライブラリがいくつかありますが、そのようなプログラムを作成する場合、pdftkを実行するためにシェルアウトするだけですが、それでも透かしを生成する必要があります。

$tempfile=tempnam();
system("pdftk input_file.pdf background watermark.pdf output $tempfile dont_ask", $errcode);
if (!$errcode && $ih=fopen($tempfile, 'r')) {
    header('Content-Type: application/pdf');
    fpassthru($ih);
    fclose($ih);
} else {
    print "Whoops";
}
unlink($tempfile);
于 2010-10-21T12:43:07.377 に答える
5

昨日これを行う必要がありましたが、インストールする必要のある外部の非 PHP ライブラリを使用しない方法を次に示します。必要なライブラリは 2 つだけで、どちらも PHP ライブラリであり、簡単に入手できます。

これで、以下のクラスを使用して透かしを実現できます

/** MAKE SURE TO HAVE THE INCLUDES RUNNING PROPERLY */
require_once('FPDF/fpdf.php');
require_once('FPDI/fpdi.php');

class WaterMark
{
    public $pdf, $file, $newFile,
        $wmText = "STACKOVERFLOW";

    /** $file and $newFile have to include the full path. */
    public function __construct($file, $newFile)
    {
        $this->pdf =& new FPDI();
        $this->file = $file;
        $this->newFile = $newFile;
    }

    /** $file and $newFile have to include the full path. */
    public static function applyAndSpit($file, $newFile)
    {
        $wm = new WaterMark($file, $newFile);

        if($wm->isWaterMarked())
            return $wm->spitWaterMarked();
        else{
            $wm->doWaterMark();
            return $wm->spitWaterMarked();
        }
    }

    /** @todo Make the text nicer and add to all pages */
    public function doWaterMark()
    {
        $currentFile = $this->file;
        $newFile = $this->newFile;

        $pagecount = $this->pdf->setSourceFile($currentFile);

        for($i = 1; $i <= $pagecount; $i++){
                            $this->pdf->addPage();
            $tplidx = $this->pdf->importPage($i);
            $this->pdf->useTemplate($tplidx, 10, 10, 100);
            // now write some text above the imported page
            $this->pdf->SetFont('Arial', 'I', 40);
            $this->pdf->SetTextColor(255,0,0);
            $this->pdf->SetXY(25, 135);
            $this->_rotate(55);
            $this->pdf->Write(0, $this->wmText);
                            $this->_rotate(0);
        }

        $this->pdf->Output($newFile, 'F');
    }

    public function isWaterMarked()
    {
        return (file_exists($this->newFile));
    }

    public function spitWaterMarked()
    {
        return readfile($this->newFile);
    }

    protected function _rotate($angle,$x=-1,$y=-1) {

        if($x==-1)
            $x=$this->pdf->x;
        if($y==-1)
            $y=$this->pdf->y;
        if($this->pdf->angle!=0)
            $this->pdf->_out('Q');
        $this->pdf->angle=$angle;

        if($angle!=0){
            $angle*=M_PI/180;
            $c=cos($angle);
            $s=sin($angle);
            $cx=$x*$this->pdf->k;
            $cy=($this->pdf->h-$y)*$this->pdf->k;

            $this->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));
        }
    } 

}

これを次のように実行できます。

WaterMark::applyAndSpit($fileWithFullPath);
于 2011-02-14T17:17:45.290 に答える
2

優れたオープンソースの php ライブラリhttp://www.tcpdf.org/が あり、私はそれをすべての pdf 生成タスクに使用しています。

于 2010-10-21T05:02:05.770 に答える