Cara menambahkan Image dan menambahkan text (watermark) di pdf existing pada codeigniter

    Berikut saya ingin membuat cara add watermark text dan image ke dalam sebuah pdf di codeigniter dengan versi php 5 ke atas

 

Pertama download dahulu library fpdf.php dan  FPDI.php jika sudah buat file untuk code php

 

INDEX.PHP

<?php
require('rotation.php');

class PDF extends PDF_Rotate
{
    protected $_outerText1; // dynamic text
    protected $_outerText2;

    function setWaterText($txt1 = "", $txt2 = "")
    {
        $this->_outerText1 = $txt1;
        $this->_outerText2 = $txt2;
    }

    function Header()
    {
        //Put the watermark
        $this->SetFont('Arial', 'B', 70);
        $this->SetTextColor(0, 50, 50);
        $this->SetAlpha(0.5);
        $this->RotatedText(55, 180, $this->_outerText1, 45);
        $this->RotatedText(75, 190, $this->_outerText2, 45);
        // $this->Image('fpdf/myoldfhoto.jpg', 0, 0);
        $this->Image('fpdf/myoldfhoto.jpg', 160, 240, 30, 40, 'JPG', '');
    }

    function RotatedText($x, $y, $txt, $angle)
    {
        //Text rotated around its origin
        $this->Rotate($angle, $x, $y);
        $this->Text($x, $y, $txt);
        $this->Rotate(0);
    }

    // Page footer

}

class PDFF extends FPDF
{
    // Page header

}

//$file = "files/$fil";
$file = "fpdf/SPK_Pengadaan_Server1.pdf"; // path: file name
$pdf = new PDF();

//$pdf->AddPage();
if (file_exists($file)) {
    $pagecount = $pdf->setSourceFile($file);
} else {
    return FALSE;
}

$pdf->setWaterText("DO NOT COPY", "");

/* loop for multipage pdf */
for ($i = 1; $i <= $pagecount; $i++) {
    $tpl = $pdf->importPage($i);
    $pdf->addPage();
    $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
}

$pdf->Output(); //specify path filename to save or keep as it is to view in browser

 

 

ROTATION.PHP

<?php
require('fpdf/fpdf.php');
require('fpdf/fpdi.php');
class PDF_Rotate extends FPDI
{
    var $angle = 0;
    var $extgstates = array();

    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 SetAlpha($alpha, $bm = 'Normal')
    {
        // set alpha for stroking (CA) and non-stroking (ca) operations
        $gs = $this->AddExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/' . $bm));
        $this->SetExtGState($gs);
    }

    function AddExtGState($parms)
    {
        $n = count($this->extgstates) + 1;
        $this->extgstates[$n]['parms'] = $parms;
        return $n;
    }

    function SetExtGState($gs)
    {
        $this->_out(sprintf('/GS%d gs', $gs));
    }

    function _enddoc()
    {
        if (!empty($this->extgstates) && $this->PDFVersion < '1.4')
            $this->PDFVersion = '1.4';
        parent::_enddoc();
    }

    function _putextgstates()
    {
        for ($i = 1; $i <= count($this->extgstates); $i++) {
            $this->_newobj();
            $this->extgstates[$i]['n'] = $this->n;
            $this->_out('<</Type /ExtGState');
            foreach ($this->extgstates[$i]['parms'] as $k => $v)
                $this->_out('/' . $k . ' ' . $v);
            $this->_out('>>');
            $this->_out('endobj');
        }
    }

    function _putresourcedict()
    {
        parent::_putresourcedict();
        $this->_out('/ExtGState <<');
        foreach ($this->extgstates as $k => $extgstate)
            $this->_out('/GS' . $k . ' ' . $extgstate['n'] . ' 0 R');
        $this->_out('>>');
    }

    function _putresources()
    {
        $this->_putextgstates();
        parent::_putresources();
    }
}

kemudian lanjut untuk penyesuaian folder pada file file yang telah disiapkan

abaikan folder temp ,untuk fpdf dan fpdi semuanya digabung kan saja dalam 1 folder dan disimpan di dalam folder fpdf jika sudah taruh folder yang telah disusun

kemudian taruh folder yang telah disusun di htdoc pada xampp kita atau tempat menjalankan phpmyadmin

dan test trala watermark telah jadi sekian,terima kasih,jika ada kesulitan comment ya







Komentar

belajar ngoding

Code Java Matrisk And Vektor

Membuat Barcode di Laravel

Cara Simple Gampang Git Clone Project