How to Create html to pdf using php | Convert HTML to PDF using PHP

in html to pdf using php Tutorials, You are looking for an simply as well as best way to generate a PDF from HTMLs with PHP?

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Convert HTML To PDF Using PHP

THE MAIN ADVANTAGES OF PDFREACTOR IN CONVERTING HTMLs TO PDF WITH PHP SOURCE CODE.

  • EASY INTEGRATION
  • USE CASES AND AREAS OF APPLICATION
  • EXCELLENT RESULTS

very easy to use PDFreactor is a top HTML to PDF converter for converting HTML to PDF using PHP scripts. generate here how you can convert HTMLs to PDF using PHP step by step!

Step 1. Create a HTML file and define markup

I create a HTML file and save it with a name html_to_pdf.html

<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="pakainfo">

<div id="html_div">
 <form action="generate_pdf.php" method="post">
  <input type="text" name="name" placeholder="Enter Name">
  <br>
  <input type="text" name="email" placeholder="Enter Email">
  <br>
  <input type="text" name="age" placeholder="Enter Age">
  <br>
  <input type="text" name="country" placeholder="Enter Country">
  <br>
  <input type="submit" name="submit_val" value="GENERATE PDF">
 </form>
</div>

</div>
</body>
</html>

Step 2. Create a PHP file to generate PDF

I create a PHP file and save it with a name generate_pdf.php

<?php
namespace Dompdf;
require_once 'dompdf/autoload.inc.php';

if(isset($_POST['submit_val']))
{
    $dompdf = new Dompdf(); 
    $dompdf->loadHtml('
    <table border=1 align=center width=400>
    <tr><td>Name : </td><td>'.$_POST['name'].'</td></tr>
    <tr><td>Email : </td><td>'.$_POST['email'].'</td></tr>
    <tr><td>Age : </td><td>'.$_POST['age'].'</td></tr>
    <tr><td>Country : </td><td>'.$_POST['country'].'</td></tr>
    </table>
    ');
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream("",array("Attachment" => false));
    exit(0);
}
?>

Step 3. Create a CSS file and define styling

I create a CSS file and save it with a name style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
}
#pakainfo
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#pakainfo h1
{
 margin-top:50px;
 font-size:45px;
 color:#585858
}
#pakainfo h1 p
{
 font-size:18px;
}
#pakainfo a
{
 color:blue;
 font-size:20px;
}
#html_div input[type="text"]
{
 margin-top:5px;
 width:250px;
 height:35px;
 padding:10px;
}
#html_div input[type="submit"]
{
 background-color:#585858;
 width:250px;
 height:35px;
 border:none;
 margin-top:5px;
 color:white;
 margin-left:-5px;
}

There are many other ways to generate PDF from HTML below Tutorials.

EXAMPLE TO EXPORT HTML TO PDF WITH PHP

using PDFreactor class

  • Include PDFreactor class
  • You can download the PDFreactor Web Service PHP wrapper from:
  • http://www.pdfreactor.com/download/get/?product=pdfreactor&type=webservice_clients&jre=false
  • Create new PDFreactor instance
  • Specify the input document
  • Render document and save result to $result

<?php

    require_once("PDFreactor.class.php");
    use com\realobjects\pdfreactor\webservice\client\PDFreactor as PDFreactor;
    $pdfReactor = new PDFreactor("https://cloud.pdfreactor.com/service/rest");
    
    $config = array(
        "document"=> "http://www.pdfreactor.com/product/samples/textbook/textbook.html"
    );

    $result = null;
    try {
        $result = $pdfReactor->convertAsBinary($config);
        header("Content-Type: application/pdf");
        echo $result;
    } catch (Exception $e) {
        echo "<h1>An Error Has Occurred</h1>";
        echo "<h2>".$e->getMessage()."</h2>";
    }
?>

I hope you get an idea about html to pdf using php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Mongodb Insert Update Delete – CRUD Operation using PHP & Mongodb
  2.   Laravel GET POST PUT DELETE Methods
  3.   Angular 6 Send Email Example Tutorial From Scratch
  4.   php application global config and settings - php superglobals
  5.   ng-click Event using Angular Example
  6.   REST API Insert Update Delete using PHP
  7.   Angular 6 Services Tutorial with Examples
  8.   Bootstrap get active tabs index using jQuery Example
  9.   Dynamically Change Page Title Keywords Meta Description In AngularJS
  10.   Fixed Sticky Header scrolling in JQuery Example

Leave a Reply

Your email address will not be published. Required fields are marked *