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 Image in PDF Document Using HTML5 Form in Browser

Posted on February 11, 2023

 

 

Welcome folks today in this blog post we will be adding image in pdf document using html5 form in browser using 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.html

 

 

1
2
3
4
5
<!-- HTML form for uploading an image -->
<form method="post" action="/insertimage.php" enctype="multipart/form-data">
  <input type="file" name="image">
  <input type="submit" value="Add Image">
</form>

 

 

As you can see we have a simple user html5 form where we have the input field where we allow the user to select any image file which needs to be inserted inside the pdf document using the tcpdf library. And then we have the submit button to submit the form. And we have given the method to be post and action attribute to be /insertimage.php

 

 

 

 

And now we need to create the insertimage.php file inside the same directory and copy paste the below code

 

 

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

 

 

 

 

 

insertimage.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
// Include the TCPDF library
require_once('./vendor/autoload.php');
 
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Get the uploaded image file
  $image = $_FILES['image']['tmp_name'];
 
  // Move the uploaded file to a desired location
  move_uploaded_file($image, 'image.jpg');
 
  // Create a new instance of the TCPDF class
  $pdf = new TCPDF();
 
  // Add a new page to the PDF
  $pdf->AddPage();
 
  // Add the image to the PDF
  $pdf->Image('image.jpg', 10, 10);
 
  // Generate the PDF and send it to the browser for download
  $pdf->Output();
}

 

 

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. 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