Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on

     

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 modeExit fullscreen mode

Step 3:

Use the main class to create functionalities

// Cargar paquete para usar el modulo de DompdfuseDompdf\Dompdf;useDompdf\Options;
Enter fullscreen modeExit fullscreen mode

Create an instance of the class Dompdf

$dompdf=newDompdf();
Enter fullscreen modeExit 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=newOptions();$options->set('isRemoteEnabled',true);$dompdf=newDompdf($options);
Enter fullscreen modeExit 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 modeExit fullscreen mode

To pass dynamic vars using this format into the string

'.$contenidoDinamico.'
Enter fullscreen modeExit 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 modeExit fullscreen mode

Step 6:

Set a size for the page with the setPaper Method

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

Step 7:

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

$dompdf->render();
Enter fullscreen modeExit 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 modeExit fullscreen mode

Link al repo

Siguemente en las redes como @syntaxter

[deleted user] image

[Deleted User]

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Desarrollo #Software y #Aplicaciones para diferentes #empresas. 💖 Y mi trabajo, es hacer reír a mi madre todo los días
  • Location
    Ciudad de Colón, Panamá
  • Education
    University
  • Work
    Independent
  • Joined

More fromJosé Antonio Fernández

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp