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]

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay