Install mPDF without Composer – Reports in PDF with PHP

The mPDF generates PDF from HTML/CSS with low learning curve. The idea here is to make the mPDF works without having to install the Composer on the server.

Introduction

Today I haven't seen myself programming without using the Composer. It allows you to consistently manage numerous modules/scripts created by third parties and are available at https://packagist.org/.

With the Composer You can not only install and update packages easily, but also keep synchronized automatically any dependencies to other packages used in the application. In addition, already have ready the AutoLoad, allowing you to carry your PHP classes without having to include/require.

But this is not a Post about Composer :P let's see how to install and run the mPDF, a PHP library for generating PDF using HTML code. The mPDF has your cons, but the interesting thing is that you basically just need to mount your HTML and with minimal effort already will be able to convert it to PDF.

Problem

It seems, in the new versions, It takes the Composer to install the mPDF. So, the recommended is that you actually spend using the Composer in your development activity.

Hence the question: What if I don't have how to install Composer on the server? In this case, We will use a gimmick: install the Composer locally and then climb (upload) to the server folder “Vendor“. Under these circumstances your Composer will be partially inoperative, but still will make possible the use of the mPDF.

Solution

The installation of the mPDF by Composer is very simple. Go to https site://getcomposer.org/download/, Download and install the Composer.

Once you have the Composer installed, simply open the Terminal (command) and access the directory of your application (within www/htdocs or/, for example), and then run the following command:

$ composer require mpdf/mpdf

Note that at the root of your application directory will be created some files, as composer .JSON and composer. lock, as well as the directory “Vendor/“. Now it's easy! You already have everything you need for your mPDF work.

Take the folder Vendor and send it to your Server. Preferably, place it in the root and create a file index.php (also in the root) to test the mPDF containing the following code:

1
2
3
4
5
<?PHP
require_once _ _ DIR _ _ . '/vendor/autoload.php ';
$mpdf = new mPDF();
$mpdf->WriteHTML('<H1>Hello world!</H1>');
$mpdf->Output();

Done that, If all goes well, will appear a PDF written “Hello World!“.

Reports in PDF with PHP

Report is not the purpose of this Post, but I take to record something more. If you need to generate reports, one option is to embed your CSS/HTML in the file PHP and then pass the code to the mPDF do the hard work, so for example (View output in Figure 1 below):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?PHP
require_once _ _ DIR _ _ . '/vendor/autoload.php ';
ob_start();
?>
<style>
    table, th, TD {
        border: 1PX solid black;
        border-collapse: collapse;
        padding: 5PX;
    }               
    table tr:nth-child(odd) {
      background-color: #Eee;
    }  
    table tr:nth-child(even) {
      background-color: #FFF;
    }   
    table thead th {
      background-color: #CCC;
    } 
    table tfoot td {
      background-color: #CCC;
    } 
</style> 
<H1>My Report</H1>
<table align="center">
    <THEAD>
        <tr>
            <th>Name</th>
            <th>Age</th>
            <th>Email</th>
        </tr>
    </THEAD>
    <TFOOT>
        <tr>
            <td colspan="3" align="center">Copyright ® 2017</TD>
        </tr>
    </TFOOT>>    
    <TBODY>
        <tr>
            <TD>Taylor</TD>
            <td align="center">43</TD>
            <TD >taylor@amail.com</TD>
        </tr>
        <tr>
            <TD>Portela</TD>
            <td align="center">47</TD>
            <TD>portela@bmail.com</TD>
        </tr>        
    </TBODY>
</table>
<?PHP
$HTML = ob_get_contents();
$mpdf = new mPDF;
$mpdf->WriteHTML($HTML);
$mpdf->Output();

Fig.. 1 – PDF output file for the CSS/HTML code injection (above) processed by the mPDF

Obviously you can organize better the code by placing the CSS/HTML in a separate template file and load it into the call of mPDF.

Anyway, now it's up to you! Take a read on The manual mPDF and stay within the form of use and compatibilities.

Total hits: 1900

Leave a reply

The your email address will not be published. Required fields are marked with *