create pdf file from html using php, Create a pdf file using dompdf in PHP, generate pdf using dompdf, html to pdf,

Create a pdf file using dompdf in PHP

A pdf file is considered as a document that is read-only meaning it cannot be edited or tampered with. It is a safe way of presenting or sending data to other users therefore the need to convert data to pdf files while exporting data.

There are various ways in which one can create or generate a pdf file from either HTML code or any other format.

In this article, we shall discuss the comprehensive and steps required to create or generate a pdf file using dompdf in PHP.

dompdf is a library supported by PHP language which acts as an HTML to pdf converter.

dompdf allows one to create any size of pdf document depending on the measurements that are supported in the library.

Dompdf works perfectly with PHP versions of above 7.1

To create a pdf file using dompdf;

You can either clone or download as zip file depending on how you prefer. Also, you can install dompdf using composer

After downloading mostly as zip, extract the zip file and load the dompdf folder in your working directory

  • Include the autoloader file which is found in the dompdf folder that ensures that the dompdf components are imported to your file

Ensure you reference the location of dompdf correctly

// Include autoloader

require_once 'dompdf/autoload.inc.php';

  • Reference the dompdf namespace to your file

// Reference the Dompdf namespace

use Dompdf\\Dompdf;

  • Instantiate and use dompdf class as follows

// Instantiate and use the dompdf class

$dompdf = new Dompdf();

 

  • You can include the HTML code that you want to convert to pdf direct in the load html function. There are options of either including a file from an external link or define the HTML code as a variable and include it in the load html function

To start with defining the code direct to the load html function, we will have a sample code as below

// define html code

$dompdf->loadHtml('this is a pdf file');

  • Define the orientation of the paper either as landscape or portrait and also the paper size. This is an optional field, when it is not defined, the paper is set to portrait by default

// (Optional) Setup the paper size and orientation

$dompdf->setPaper('A4', 'landscape');

  • To generate the PDF file from the defined HTML code, you use the render function as shown below

// Render the HTML as PDF

$dompdf->render();

  • Define how you want to display the pdf document, you can either preview the pdf document or allow the file to download and save to your computer instantly. Option 1 downloads instantly while 0 displays the document for preview

Also, you define the name of the document by setting your preferred name without the .pdf extension

// Output the generated PDF (1 = download and 0 = preview)

$dompdf->stream("pdfdocumentname", array("Attachment" => 1));

The full code to create a pdf file using dompdf in PHP is as follows

<?php

// Include autoloader

require_once 'dompdf/autoload.inc.php';

 

// Reference the Dompdf namespace

use Dompdf\\Dompdf;

 

// Instantiate and use the dompdf class

$dompdf = new Dompdf();

 

// define html code

$dompdf->loadHtml('this is a pdf file');

 

 

// (Optional) Setup the paper size and orientation

$dompdf->setPaper('A4', 'landscape');

 

// Render the HTML as PDF

$dompdf->render();

 

// Output the generated PDF (1 = download and 0 = preview)

$dompdf->stream("pdfdocumentname", array("Attachment" => 1));

?>

To generate pdf document using dompdf in PHP by defining html code as a variable

The variable is defined as follows

//define code as a variable

$htmlcode='this is a pdf file from a variable';

The full code for creating a pdf file using dompdf after defining the variable is as follows

<?php

// Include autoloader

require_once 'dompdf/autoload.inc.php';

 

// Reference the Dompdf namespace

use Dompdf\\Dompdf;

 

// Instantiate and use the dompdf class

$dompdf = new Dompdf;

 

//define code as a variable

$htmlcode='this is a pdf file from a variable';

 

// load html code

$dompdf->loadHtml($htmlcode);

 

// (Optional) Setup the paper size and orientation

$dompdf->setPaper('A4', 'landscape');

 

// Render the HTML as PDF

$dompdf->render();

 

// Output the generated PDF (1 = download and 0 = preview)

$dompdf->stream("pdfdocumentname", array("Attachment" => 1));

?>

Create a pdf file using dompdf in PHP by loading data from a different file is as follows

<?php

// Include autoloader

require_once 'dompdf/autoload.inc.php';

 

// Reference the Dompdf namespace

use Dompdf\\Dompdf;

 

// Instantiate and use the dompdf class

$dompdf = new Dompdf;

 

// Load content from html file

$html1 = file_get_contents("https://www.example.com/pdf/htmlcocument.php");

 

$dompdf->loadHtml($html1);

 

// (Optional) Setup the paper size and orientation

$dompdf->setPaper('A4', 'landscape');

 

// Render the HTML as PDF

$dompdf->render();

 

// Output the generated PDF (1 = download and 0 = preview)

$dompdf->stream("pdfdocumentname", array("Attachment" => 1));

?>

 

With the above illustrations, you can create and generate pdf files using the dompdf PHP library