DEV Community

José Antonio Fernández
José Antonio Fernández

Posted on

6 1

How use dompdf to generate a simple invoice pdf?

Invoice formato with PHPAt this moment to understand the basis in composer I have decided to create a simple pdf generator with Dompdf package.

This package is awesome and simple, to create a pdf invoice solution for download or create an automatic invoice delivery system.

The idea for monetization:
A create simple auth token system with API_KEY and token to receive JSON data for generating pdf in a simple PHP hosting.
Free hosting for testing this:

Free PHP Hosting

Ok, the code:

Step 1:

run this, for a run this you need to install composer
composer require dompdf/dompdf

The Dompdf documentation: :
Dompdf Documentation

Step 2:

Load vendor.autoload.php package

require 'vendor/autoload.php';
Enter fullscreen mode Exit fullscreen mode

Step 3:

Use the main class to create functionalities

// Cargar paquete para usar el modulo de Dompdf
use Dompdf\Dompdf;
use Dompdf\Options;
Enter fullscreen mode Exit fullscreen mode

Create an instance of the class Dompdf

$dompdf = new Dompdf();
Enter fullscreen mode Exit fullscreen mode

Enable remote origins (for download image)

If not enable this, the image not render in the pdf file and break the template format preset

$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
Enter fullscreen mode Exit fullscreen mode

Step 4:

Create an HTML template, with a text string

$htmlTemplate = '
<style>
//all styles for the custom template 
</style>

<html>
  <head>
    <htmlcontent>
    .......
</html>
';
Enter fullscreen mode Exit fullscreen mode

To pass dynamic vars using this format into the string

'.$contenidoDinamico.'
Enter fullscreen mode Exit fullscreen mode

For creating bucle with PHP array using the other var to generate a string with Concat operator (htmlinfo.= 'bucleGetInfo') or array method to push data

Step 5:

Load HTML with loadHtml method of the class instance previously named

$dompdf->loadHtml($content);
Enter fullscreen mode Exit fullscreen mode

Step 6:

Set a size for the page with the setPaper Method

$dompdf->setPaper('A4','landscape');
Enter fullscreen mode Exit fullscreen mode

Step 7:

Server compiled and fusion HTML with a PDF format to create a pdf image:

$dompdf->render();
Enter fullscreen mode Exit fullscreen mode

Step 8:

Write this information of saving in temporal server memory for sending a file to the client request

$dompdf->stream($fileName);
Enter fullscreen mode Exit fullscreen mode

Link al repo

Siguemente en las redes como @syntaxter

[deleted user] image

[Deleted User]

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs