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 composercomposer 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 DompdfuseDompdf\Dompdf;useDompdf\Options;
Create an instance of the class Dompdf
$dompdf=newDompdf();
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);
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](/image.pl?url=https%3a%2f%2fmedia2.dev.to%2fdynamic%2fimage%2fwidth%3d800%252Cheight%3d%252Cfit%3dscale-down%252Cgravity%3dauto%252Cformat%3dauto%2fhttps%253A%252F%252Fthepracticaldev.s3.amazonaws.com%252Fi%252F99mvlsfu5tfj9m7ku25d.png&f=jpg&w=240)
[Deleted User]
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse