Please enable / Por favor activa JavaScript!
Por favor activa el Javascript![ ? ]

How to convert HTML file to PDF with Dompdf library  How to convert HTML file to PDF with Dompdf library

Valoración de éste post
4.71 / 5 de 24 votos



Mensajes: 1237


Puntos totales:

Enhorabuena!

32





Visita Exaforo.com


Download at: https://github.com/dompdf/dompdf

On every occasion we work on massive Hypertext Preprocessor application, then we generally require to generate PDF of invoice, facts, tips, subscription and so forth. if you use any personal home page framework like laravel, codeigniter and so on then you have choice to other kit for generate HTML file into PDF. But if you are working on Code php then you definately fetch problem to the way to do it. But in this tutorial i am going to provide you with very simple illustration to html to pdf convert using DomPDF library.

DomPDF library via we are able to comfortably render html layout into PDF file. DomPDF library through us will use write exterior stylesheets, inline style tags, font measurement, font color etc. DomPDF will aid to do personalize PDF file. Dompdf library is purchasable on composer kit so when you are working with composer then additionally that you can use it.

So, today I am going to share with you example of a way to generate PDF file from HTML layout using DomPDF library, if you do not know a way to implement it then no fret as a result of i am doing from scratch and you can also free down load whole script code from right here.

During this example i will be able to do main three issues like as bellow:

1) Setup dompdf library
2) index.Hypertext Preprocessor file
3) pdf_generate.Hypertext Preprocessor file

Ok, so that you have to just following bellow step and you'll get source code of html to pdf the use of php dompdf library. So let's follow bellow step.

Step 1: Setting up & setup

In first step we have to down load one library and two dependency of dompdf so observe bellow things.

1) Dompdf: we have to download dompdf library from GitHub, So first let's down load from right here: click on right here to download dompdf. After down load extract it to your root folder and rename it to "dompdf".

2) php-font-lib: good enough, now download Hypertext Preprocessor-font-lib from GitHub, So first let's download from right here: click on right here to download php-font-lib. After download extract it to "dompdf/lib/" folder and rename it to "personal home page-font-lib".

3) php-svg-lib: ultimate download personal home page-svg-lib from GitHub, So first let's down load from here: click on here to down load Hypertext Preprocessor-svg-lib. After download extract it to "dompdf/lib/" folder and rename it to "php-svg-lib".

Step 2: Create index.personal home page file

During this step i am going to create index.personal home page file to your root listing, during this file i created effortlessly form the use of bootstrap, this manner you need to just suppose this form and click to "Generate PDF" button. you will without difficulty download pdf with stuffed particulars.

So, let's create index.php file and put bellow code on it.

index.php:

<!DOCTYPE html><html><head>
<title>personal home page - Convert HTML to PDF the usage of DomPDF Library</title>
<hyperlink rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/three.3.7/css/bootstrap.min.css">
</head>
<physique>
<div classification="container">
<h2>information kind For Generate PDF</h2>
<kind action="pdf_generate.Hypertext Preprocessor" components="put up">
<div class="kind-neighborhood">
<label>name:</label>
<enter classification="text" name="name" class="kind-control" placeholder="Enter identify" required>
</div>
<div category="form-neighborhood">
<label>electronic mail:</label>
<enter classification="e-mail" name="email" category="form-handle" placeholder="Enter electronic mail" required>
</div>
<div classification="kind-community">
<label>website URL:</label>
<input category="url" identify="url" category="form-control" placeholder="Enter URL" required>
</div>
<div classification="kind-group">
<label>Say something:</label>
<textarea name="say" classification="form-handle" placeholder="Say whatever">
</textarea>
</div>
<div type="form-group">
<button category="btn btn-success">Generate PDF</button></div>
</kind>
</div>
</body>
</html>


Step 3: Create pdf_generate.php file

In final step we are able to create "pdf_generate.Hypertext Preprocessor" file, in this file i will be able to get put up records and generate pdf file for download. So, let's create "pdf_generate.personal home page" file and put bellow code on it.

pdf_generate.php:

<?personal home page/* include autoloader */require_once 'dompdf/autoload.inc.personal home page';
/* reference the Dompdf namespace */use Dompdf\Dompdf;
/* instantiate and use the dompdf type */$dompdf = new Dompdf();
$html = '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/three.three.7/css/bootstrap.min.css">
<h1>Welcome to ItSolutionStuff.com</h1>
<table classification="desk table-bordered">
<tr>
<th colspan="2">tips kind</th>
</tr>
<tr>
<th>identify</th>
<td>'.$_POST['name'].'</td>
</tr>
<tr>
<th>e-mail</th>
<td>'.$_POST['email'].'</td>
</tr>
<tr>
<th>site URL</th>
<td>'.$_POST['url'].'</td>
</tr>
<tr>
<th>Say some thing</th><td>'.nl2br($_POST['say']).'</td></tr></desk>';
$dompdf->loadHtml($html);
/* Render the HTML as PDF */$dompdf->render();
/* Output the generated PDF to Browser */$dompdf->circulation();?>


Good enough, now you are ready to use and determine it. In case you want to down load supply code script then that you can down load it.

I’m hoping might be it might aid you...





No te pierdas el tema anterior: Curso completo GRATIS de seguridad en PHP
Si has encontrado información útil en Exaforo, ayúdanos a seguir creciendo. Muchas gracias por confiar en nosotros!


Volver a PHP