PHP Classes
PHP Classes
elePHPant
Icontem

How to Use PHP to Create PDF documents from HTML in 2018 on a Linux based Web server - Laravel and PHP HTML to PDF converter package blog

Recommend this page to a friend!
  All package blogs All package blogs   Laravel and PHP HTML to PDF converter Laravel and PHP HTML to PDF converter   Blog Laravel and PHP HTML to PDF converter package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use PHP to Cre...  
  Post a comment Post a comment   See comments See comments (1)   Trackbacks (0)  

Author:

Updated on: 2018-05-18

Posted on:

Package: Laravel and PHP HTML to PDF converter

Many PHP applications need to generate PDF documents. One easy way to generate PDF is to use the wkhtmltopdf program on the server side. This program is available on many Linux distributions like for instance Ubuntu.

Read this article to learn how to use the Laravel and PHP HTML to PDF converter package to generate PDF documents from HTML even when you do not want to use the Laravel framework.




Contents

Introduction

Installation

Basic Usage

Other Kinds of Usage

Using this Package without Laravel

Conclusion


Introduction

PHP can be used for many purposes that go beyhond the capabilities of the functions built-in the language by using external programs available in the OS that runs on the server that executes the PHP code.

Given that, an easy way to write PHP applications that provide advanced features like for instance generating PDF documents, is to make a PHP script that generates HTML text string and then execute an external program to convert the HTML into a PDF file that can be easily downloaded with the Web browser.

This article tells how to use the Laravel and PHP HTML to PDF converter for this exact purpose, I mean generating a PDF document from an HTML string that any PHP script can generate easily.

Installation

First you need to install the wkhtmltopdf program on Linux. The following steps were tested on Ubuntu 14.04 and 16.04 x64 edition.

 sudo apt-get update
 sudo apt-get install xvfb libfontconfig wkhtmltopdf

You can also execute similar commands on a server managed as a docker container.

 RUN apt-get update && apt-get install xvfb libfontconfig wkhtmltopdf

You can install or update this package using Composer with the following command from packagist.

 composer require nahidulhasan/html2pdf

You can also use Composer to install the package from PHP Classes composer repository. You may find instructions to configure your  project to use PHP Classes composer repository here.

 composer require phpclasses/html2pdf

If you are using Laravel version before 5.5, you can use this package as a service provider to the list of providers array in your config/app.php file.

 NahidulHasan \ Html2pdf \ Html2pdfServiceProvider :: class,

You can optionally use shorter code by using these packages. Add this to your facades:

 'Pdf' => NahidulHasan \ Html2pdf \ Facades \ Pdf::class,

Basic Usage

To create a PDF file you can use the following code:

 use NahidulHasan \ Html2pdf \ Facades \ Pdf;

 $document = Pdf::generatePdf( '<h1>Test</h1>' );

You can also create PDF from directly using a Laravel Blade template file. Lets suppose you have a template for sending emails named greeting in view/mails folder and you want to customize a parameter. You have to call generatePdf method as described in below.

 <!-- mail template stored in resources/views/mails/greeting.blade.php -->
 $document = Pdf::generatePdf( view( 'mails.greeting', ['name' => 'James', 'testVar' => 'demo']));

Now if you want to send mail to your client attaching a PDF file then you can follow this code

 /**
  * Build the message.
  *
  * @return $this
  */
 public function build()
 {
  return $this->from('username@gmail.com')
   ->view('mails.demo')
   ->attachData($document, 'Invoice.pdf');
 }
 

Other Kinds of Usage

It is also possible to use the following methods:

pdf::download - Download the PDF file

pdf::stream - Open the PDF file in browser

Using this Package without Laravel

You can use this library without using Laravel. Lets see this example:

 use NahidulHasan \ Html2pdf \ Pdf;
 $obj = new Pdf();
 $document = $obj->generatePdf( '<h1>Test</h1>' );

Conclusion

As you may have seen, using this package is simple and straightforward. This tutorial should be all you need to get started to generate your PDF documents easily from PHP.

Post a comment below if you have questions or want make comments about this package and or tutorial.




You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

1. JasperReports - Agustin Farias Gonzalez (2018-05-18 01:49)
JasperReports generates greate paginated PDFs... - 0 replies
Read the whole comment and replies




  Post a comment Post a comment   See comments See comments (1)   Trackbacks (0)  
  All package blogs All package blogs   Laravel and PHP HTML to PDF converter Laravel and PHP HTML to PDF converter   Blog Laravel and PHP HTML to PDF converter package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Use PHP to Cre...