Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

PHP 7 TCPDF Example to Add Multiple Images inside PDF Document Using Multiple Form Input Fields in Browser

Posted on February 11, 2023

 

 

Welcome folks today in this blog post we will be adding multiple images inside pdf document using multiple form input fields in php. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

index.php

 

 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
    <label for="image1">Image 1:</label>
    <input type="file" name="image1" id="image1">
    <br>
    <label for="image2">Image 2:</label>
    <input type="file" name="image2" id="image2">
    <br>
    <label for="image3">Image 3:</label>
    <input type="file" name="image3" id="image3">
    <br>
    <input type="submit" value="Generate PDF">
  </form>

 

 

As you can see we have a simple user html5 form where we have multiple input fields where we allow the users to select multiple image files. And then we have the simple submit button to add the images inside the pdf document.

 

 

 

 

And now we need to copy paste the below php code to add the images inside the pdf document using the tcpdf library.

 

 

Installing TCPDF Library Using Composer

 

 

Now guys first of all create the composer.json file and copy paste the following code to it

 

 

composer.json

 

 

1
2
3
4
5
{
  "require": {
    "tecnickcom/tcpdf": "^6.2.13"
  }
}

 

 

And now you need to execute the below command to install the dependencies from composer as shown below

 

 

composer update

 

 

After executing this command this will create the vendor folder inside the root directory and it will contain all the libraries of tcpdf as shown below

 

 

 

 

 

index.php

 

 

PHP
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
26
27
28
29
30
31
32
33
34
35
36
37
<?php
 
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Include the TCPDF library
  require_once('./vendor/autoload.php');
  // Create a new PDF document
  $pdf = new TCPDF();
  // Set the document title
  $pdf->SetTitle('My PDF Document');
  
  // Set the font
  $pdf->SetFont('helvetica', '', 12);
  // Loop through the uploaded images
  foreach ($_FILES as $file) {
    // Check if the file is an image
    if ($file['type'] == 'image/jpeg' || $file['type'] == 'image/png') {
      // Add the image to the PDF document
      $pdf->AddPage();
      $pdf->Image($file['tmp_name'], 0, 0, 0, 0, '', '', '', true, 900, '', false, false, 1, false, false, false);
    }
  }
  // Output the PDF document
  $pdf->Output('my_pdf_document.pdf', 'I');
}
 
?>

 

 

As you can see we are importing the autoload.php file of the tcpdf library which is stored inside the vendor folder and then we are checking if the submit button is clicked by the user of the form. And then we are getting the temporary path of the image file without uploading it we are getting the temp path using the $_FILES[] array and then we are adding new page in pdf document using AddPage() method of TCPDF library and then we are using the Image() method of tcpdf library to add the image we are passing three arguments. Now we are repeating the same process for all the images selected by the user using the foreach loop. First is the path of the image and then we are passing the x and y coordinates. And lastly we are showing the output of the pdf document inside the browser as an attachment using the Output() method.

 

 

 

Recent Posts

  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme