All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Generate PDF From HTML

Last Updated : Mar 11, 2024

PHP Generate PDF From HTML

In this article we will show you the solution of PHP generate pdf from HTML, to generate pdf from html we used a PHP library named DOMPDF. DOMPDF is an HTML-to-PDF converter that is used to easily convert html to pdf.

We have to download the library first. Then install it by the composer.

Step By Step Guide On PHP Generate PDF From HTML :-

Let us see the code for the simplest example to generate pdf of inline HTML using dompdf.

Example 1

<?php
require __DIR__."/vendor/autoload.php"
use Dompdf\Dompdf ;
$dompdf = new Dompdf;
$dompdf -> loadHTML("Welcome To TalkersCode") ;
$dompdf -> render() ;
$dompdf -> stream("demo.pdf", ["attachment" => 0]) ;
$output = $dompdf->output() ;
file_put_contents("file.pdf", $output) ;
?>
  1. At first <?php tag to write the php.
  2. Using require to include the DOMPDF library with _DIR_ .
  3. Use the function to Dompdf\DOMpdf
  4. loadHTML() to add the inline html
  5. now using the render() function
  6. stream() to add the name of the pdf with attachment of 0.
  7. To download the pdf include output() function
  8. Using file_put_contents() to add name of the pdf to be downloaded
  9. Close the php code with ?> tag

Example 2

In this example, we will see another example of taking input form an HTML form to generate a pdf. We will see the code for both the HTML file and the php file.

HTML code

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> php generate pdf from html </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> php generate pdf from html </h2>
    <form action="generate-pdf.pdf" method="post">
        <label for="name">Name</label>
        <input type="text" name="name" id="name">
        <label for="quantity">Quantity</label>
        <input type="text" name="quantity" id="quantity">
    </form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Creating a form using <form> tag with the action of the pdf file with the method POST.
  8. Two <input> tag for take the input for user of Name and Quantity.
  9. Using a external css file to style the form
  10. Close the HTML tag with </html>

Php code

<?php
require __DIR__."/vendor/autoload.php"
use Dompdf\Dompdf ;
use Dompdf\Options ;
$name = $_POST["name"] ;
$quantity = $_POST["quantity"] ;
$html = '<h1 style="color: pink">Example PDF</h1>' ;
$html.= "name : $name" ;
$html.= "quantity : $quantity" ;
$html.= '<img src = "bird.png">' ;
$options = new Options ;
$options ->setChroot(__DIR__) ;
$dompdf->setPaper("A4", "landscape") ;
$dompdf = new Dompdf($options);
$dompdf -> loadHTML($html) ;
$dompdf -> render() ;
$dompdf-> addInfo("Title", "this is a demo pdf") ;
$dompdf -> stream("demo.pdf", ["attachment" => 0]) ;
$output = $dompdf->output() ;
file_put_contents("file.pdf", $output) ;
?>
  1. At first <?php tag to write the php.
  2. Using require to include the DOMPDF library with _DIR_ .
  3. Use the function to Dompdf\DOMpdf and Dompdf\Options.
  4. Creating two variable for $name and $quantity with the $_POST method
  5. $html to add some heading to with some inline CSS.
  6. $html. To add name and quantity from the user’s input. Also adding an image
  7. To allow Dompdf to access the image file with any folder create a variable named $options. Using setChroot() with _DIR_.
  8. We can set the pdf size with setPaper() function. Here we set the size to A4 and landscape.
  9. now using the render() function
  10. we also can set the Title of the pdf using addInfo().
  11. stream() to add the name of the pdf with an attachment of 0.
  12. To download the pdf include output() function
  13. Using file_put_contents() to add name of the pdf to be downloaded
  14. Close the php code with ?> tag

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to generate pdf from HTML in php.

I hope this article on PHP generate pdf from HTML helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪