At 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:
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';
Step 3:
Use the main class to create functionalities
// Cargar paquete para usar el modulo de Dompdf use Dompdf\Dompdf; use Dompdf\Options;
Create an instance of the class Dompdf
$dompdf = new Dompdf();
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);
Step 4:
Create an HTML template, with a text string
$htmlTemplate = ' <style> //all styles for the custom template </style> <html> <head> <htmlcontent> ....... </html> ';
To pass dynamic vars using this format into the string
'.$contenidoDinamico.'
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);
Step 6:
Set a size for the page with the setPaper Method
$dompdf->setPaper('A4','landscape');
Step 7:
Server compiled and fusion HTML with a PDF format to create a pdf image:
$dompdf->render();
Step 8:
Write this information of saving in temporal server memory for sending a file to the client request
$dompdf->stream($fileName);
Siguemente en las redes como @syntaxter
![[deleted user] image](https://media2.dev.to/dynamic/image/width=800,height=,fit=scale-down,gravity=auto,format=auto/https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png)
Top comments (0)