Вы находитесь на странице: 1из 12

ItSolutionStuff.

com

Toggle navigation

Home

PHP

PHP

MySQL

Mongo DB

Laravel

Codeigniter

Javascript

JQuery

Ajax

Angular JS

Node JS

Vue JS

JSON

Laravel

Laravel 5.8

Laravel 5.7

Laravel 5.6

Laravel 5.5

Laravel 5.4

Laravel 5.3

Laravel 5.2

Laravel 5

Laravel 4

Tags
Demo Post

Categories

Featured Post

Laravel 5.8

PHP - How to convert HTML file to PDF using Dompdf library?

By Hardik Savani | October 23, 2016 | | 103803 Viewer | Category : PHP Bootstrap HTML

Whenever we work on big PHP application, then we generally require to generate PDF of
invoice, data, information, subscription etc. If you use any PHP framework like laravel,
codeigniter etc then you have option to other package for generate HTML file into PDF. But if
you are working on Code PHP then you fetch problem to how to do it. But in this tutorial i am
going to give you very simple example to html to pdf convert using DomPDF library.

DomPDF library through we can simply render html layout into PDF file. DomPDF library
through we can use write external stylesheets, inline style tags, font size, font color etc.
DomPDF will help to do customize PDF file. Dompdf library is available on composer package
so if you are working with composer then also you can use it.

So, today i am going to share with you example of how to generate PDF file from HTML layout
using DomPDF library, If you don't know how to implement it then no worry because i am
doing from scratch and you can also free download whole script code from here.

In this example i will do main three things like as bellow:

1) Setup dompdf library

2) index.php file

3) pdf_generate.php file

Ok, So you have to just following bellow step and you will get source code of html to pdf using
PHP dompdf library. So let's follow bellow step.
Step 1: Installation & Setup

In first step we have to download one library and two dependency of dompdf so follow bellow
things.

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

2) php-font-lib: Ok, now Download php-font-lib from GitHub, So first let's download from here
: Click Here to download php-font-lib. After download extract it to "dompdf/lib/" folder and
rename it to "php-font-lib".

3) php-svg-lib: last Download php-svg-lib from GitHub, So first let's download from here : Click
Here to download php-svg-lib. After download extract it to "dompdf/lib/" folder and rename it
to "php-svg-lib".

Step 2: Create index.php file

In this step i am going to create index.php file in your root directory, in this file i created simply
form using bootstrap, this way you have to just feel this form and click to "Generate PDF"
button. You will simply download pdf with filled details.

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

index.php

<!DOCTYPE html>

<html>

<head>

<title>PHP - Convert HTML to PDF using DomPDF Library</title>

<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>

<div class="container">

<h2>Information Form For Generate PDF</h2>

<form action="pdf_generate.php" method="POST">

<div class="form-group">

<label>Name:</label>

<input type="text" name="name" class="form-control"


placeholder="Enter Name" required>

</div>

<div class="form-group">

<label>Email:</label>

<input type="email" name="email" class="form-control"


placeholder="Enter Email" required>

</div>

<div class="form-group">

<label>Website URL:</label>

<input type="url" name="url" class="form-control" placeholder="Enter


URL" required>

</div>

<div class="form-group">

<label>Say Something:</label>

<textarea name="say" class="form-control" placeholder="Say


Something"></textarea>

</div>

<div class="form-group">

<button class="btn btn-success">Generate PDF</button>

</div>

</form>

</div>
</body>

</html>

Read Also: PHP AngularJS CRUD with Search and Pagination Example From Scratch

Step 3: Create pdf_generate.php file

In last step we will create "pdf_generate.php" file, in this file i will get post data and generate
pdf file for download. So, let's create "pdf_generate.php" file and put bellow code on it.

pdf_generate.php

Read Also: Laravel 5.3 - import export csv and excel file into database

<?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();

$html = '<link rel="stylesheet"


href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<h1>Welcome to ItSolutionStuff.com</h1>

<table class="table table-bordered">

<tr>

<th colspan="2">Information Form</th>

</tr>

<tr>

<th>Name</th>

<td>'.$_POST['name'].'</td>

</tr>

<tr>

<th>Email</th>

<td>'.$_POST['email'].'</td>
</tr>

<tr>

<th>Website URL</th>

<td>'.$_POST['url'].'</td>

</tr>

<tr>

<th>Say Something</th>

<td>'.nl2br($_POST['say']).'</td>

</tr>

</table>';

$dompdf->loadHtml($html);

/* Render the HTML as PDF */

$dompdf->render();

/* Output the generated PDF to Browser */

$dompdf->stream();

?>

Ok, now you are ready to use and check it. If you want to download source code script then
you can download it.

I hope Maybe it can help you...


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman
Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am
a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from
the early stage.

Follow Me: Github LinkedIn Twitter Facebook

***Do you want me hire for your Project Work? Then Contact US.

Tags :

dompdfDownloadExampleHTMLHTML to PDFLibraryPHPScriptSource Code


Featured Post

We are Recommending you:

PHP - jquery ajax crop image before upload using croppie plugin

Laravel 5 - generate PDF from html view file and download using dompdf

PHP AngularJS CRUD with Search and Pagination Example From Scratch

Parte superior do formulário

Parte inferior do formulário

Popular Posts

How to stop setinterval() after sometimes in jquery?

Install Sublime Text 3 in Ubuntu 14.04 using Terminal

How to find deleted file in git repo history log?

Laravel 5 - where condition with two columns example code

How to create authentication(login and registration) in Laravel 5.2?

How to resolve - tokenmismatchexception in verifycsrftoken.php Laravel 5 Ajax ?

PHP - How to create zip file and download using ZipArchive ?

Vue Axios Post Request Example

Laravel 5 - login with username or email example

Laravel age calculate from date using carbon

Laravel 5.8 Email Verification Example

JQuery - Display loading image on ajax call example

Categories

Laravel

PHP

jQuery

Bootstrap

Javascript

MySql

Ajax
HTML

Codeigniter

AngularJS

Vue.JS

JSON

Installation

Ubuntu

Node JS

CSS

Git

Google Map

SQL

Google API

JQuery UI

Server

Typeahead JS

Elasticsearch

React JS

Axios

Wampserver

Ionic Framework

MSSQL

Socket.io

Latest Posts

Laravel Passport Access Token Expire Lifetime

How to remove multiple keys from PHP Array?

Jquery Toggle hide show div on click event example

Laravel Eager Loading with Selected Columns Example

Laravel Eager Loading with Condition Relationship Example

Advertisement
Subscribe Your Email address:

Parte superior do formulário

ItSolutionStuff en_US

Subscribe

Parte inferior do formulário

Top Links:

List Of Categories

Demo Posts

Latest Posts

Disclaimer

Contact US

About US

© 2016 All Rights Reserved • www.itsolutionstuff.com

Video Player is loading.

Play Video

Pause

Unmute

0:19

0:42

Loaded: 0%

0:19

Progress: 0%

Stream TypeLIVE
-0:23

Playback Rate

1x

Chapters

Chapters

Descriptions

descriptions off, selected

Captions

captions settings, opens captions settings dialog

captions off, selected

Audio Track

en, selected

Fullscreen

This is a modal window.

Beginning of dialog window. Escape will cancel and close the window.

White Opaque Black


TextColor Transparency BackgroundColor
Opaque Black
Transparency WindowColor Transparency
Transparent

100% None
Font Size Text Edge Style Font Family
Proportional Sans-Serif

Reset restore all settings to the default valuesDone

Close Modal Dialog

End of dialog window.

AddThis Sharing Sidebar

Share to Facebook

, Number of shares

Share to TwitterShare to Pinterest

, Number of shares

Share to LinkedInMore AddThis Share options


, Number of shares2

Hide

Show

Close

AddThis

AddThis Sharing

SHARES

Show

AddThis What's Next

Recommended for you

Build Admin Panel with Laravel 5.8

itsolutionstuff.com

AddThis

Hide

Вам также может понравиться