How to Add Watermark Image Using Mpdf In Codeigniter?

4 minutes read

To add a watermark image using mPDF in CodeIgniter, you first need to create a new mPDF object and then set the watermarks using the SetWatermarkImage() and SetWatermarkText() functions provided by mPDF.


Here is an example of how you can add a watermark image using mPDF in CodeIgniter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Load mPDF library
$this->load->library('pdf');

// Create new mPDF object
$pdf = new mPDF();

// Set the watermark image
$pdf->SetWatermarkImage(base_url('path/to/watermark-image.png'));

// Add content to the PDF
$pdf->WriteHTML('<h1>Hello World</h1>');

// Output the PDF
$pdf->Output();


In the example above, we have loaded the mPDF library, created a new mPDF object, set the watermark image using the SetWatermarkImage() function, added some content to the PDF using the WriteHTML() function, and then outputted the PDF using the Output() function.


This will add a watermark image to each page of the PDF generated using mPDF in CodeIgniter.


How to customize the font style of the text watermark in mpdf?

To customize the font style of the text watermark in mPDF, you can use the SetWatermarkText() function along with the SetWatermarkFont() function. Here is an example code snippet:

1
2
3
4
5
6
7
8
$mpdf = new \Mpdf\Mpdf();

$mpdf->SetWatermarkText('SAMPLE WATERMARK');
$mpdf->SetWatermarkFont('helvetica', 100); // Set font style and size

$mpdf->WriteHTML('<p>This is a sample text with a watermark</p>');

$mpdf->Output();


In the code above, the SetWatermarkText() function is used to set the text for the watermark, and the SetWatermarkFont() function is used to set the font style (in this case 'helvetica') and font size (100 in this case) for the watermark text. You can adjust the font style and size according to your requirements.


How to add text watermark in mpdf using codeigniter?

To add a text watermark in mpdf using codeigniter, you can follow these steps:

  1. Install mPDF library in your codeigniter project. You can download the library from the mPDF website (https://github.com/mpdf/mpdf).
  2. Create a new controller in your codeigniter project and load the mPDF library and necessary dependencies. For example, you can create a controller named PdfController and load the mPDF library in the constructor as shown below:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require_once APPPATH.'third_party/mpdf/vendor/autoload.php';

class PdfController extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $mpdf = new \Mpdf\Mpdf();
        
        // Add text watermark
        $mpdf->SetWatermarkText('CONFIDENTIAL');
        $mpdf->showWatermarkText = true;
        
        // Generate the PDF file
        $mpdf->WriteHTML('<h1>Hello World!</h1>');
        $mpdf->Output('sample.pdf', 'D');
    }
}


  1. In the above code, we have set the watermark text to 'CONFIDENTIAL' and enabled the display of the watermark text on the PDF. You can customize the text and styling of the watermark as per your requirements.
  2. Finally, you can access the PdfController by visiting the corresponding URL in your browser, and it will generate and download a PDF file with the text watermark.


That's it! You have successfully added a text watermark in mPDF using codeigniter.


How to make the watermark image clickable in mpdf?

To make a watermark image clickable in mPDF, you can use the SetWatermarkImage function to add the image and then use the SetLink function to add a hyperlink to it. Here is an example code snippet to achieve this:

1
2
3
4
5
6
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->SetWatermarkImage('watermark.png');
$mpdf->SetLink('http://example.com');
$mpdf->Output();


In this code, we first load the mPDF library and create a new mPDF instance. We then set the watermark image using the SetWatermarkImage function and add a hyperlink to it using the SetLink function with the desired URL. Finally, we output the PDF document.


By adding a hyperlink to the watermark image, users can click on the image to open the specified URL in their browser.


What is the code snippet for adding watermark image in codeigniter using mpdf?

To add a watermark image in CodeIgniter using mPDF, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Load the mPDF library
$this->load->library('m_pdf');

// Define the path to the watermark image
$watermark_image = base_url('path/to/watermark_image.png');

// Set the watermarks image
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDefaultBodyCSS('background-image:url(' . $watermark_image . ');background-image-resize:6;opacity:0.2;');
$mpdf->WriteHTML($html);

// Output the PDF
$mpdf->Output('filename.pdf', 'D');


In the above code snippet, we first load the mPDF library in CodeIgniter. Then, we define the path to the watermark image that we want to use. Next, we create a new mPDF object and set the watermark image using the SetDefaultBodyCSS method. Finally, we output the PDF with the watermark image added to it.

Facebook Twitter LinkedIn Telegram

Related Posts:

To generate a PDF using CodeIgniter, you can use libraries like TCPDF or mPDF which allow you to create and format PDF files in your CodeIgniter application.First, you need to download and include the library files in your CodeIgniter project. Then, you can lo...
To upload a base64 image in CodeIgniter, you can follow these steps:Firstly, you need to decode the base64 string to convert it into an image file.Next, you can use the file_put_contents() function in PHP to save the decoded image file into a directory on the ...
To upload an image in CodeIgniter, you first need to create a file upload form in your view file. This form should include an input field of type &#34;file&#34; for selecting the image to upload.Next, you need to create a controller function that handles the i...
To add an image from your computer in Quill JS, you can first create an image tag in the Quill editor. Next, you can upload the image file from your computer to a desired location (such as a server or cloud storage). Once the image file is uploaded, you can us...
To share WordPress session data to CodeIgniter framework, you can use the following steps. First, you need to include WordPress functions in your CodeIgniter project by adding the wp-load.php file to the CodeIgniter controller. Next, you can access and manipul...