The redocmx/client-php
module is a PHP client for interacting with the redoc.mx REST API to convert CFDIs (Comprobante Fiscal Digital por Internet) to PDFs.
This client simplifies the process of sending XML data and retrieving the converted PDF, along with transaction details and metadata.
PHP 5.6.0 and later.
To install the module, run:
composer require redocmx/client-php
To use the bindings, use Composer's autoload:
require_once 'vendor/autoload.php';
First, import the module and create an instance of the Redoc client.
You can optionally pass your API key as an argument, or the client will attempt to load it from the REDOC_API_KEY environment variable.
use Redocmx\RedocmxClient;
# Create a Redoc instance
$redoc = new RedocmxClient('your_api_key_here');
The redocmx/client-php
provides two options for loading CFDI data: from a file or directly from a string.
$cfdi = $redoc->cfdi()->fromFile('./path/to/your/file.xml');
$cfdi = $redoc->cfdi()->fromString('<xml_content_string_here>');
To convert the loaded CFDI to a PDF:
try {
# Create the PDF
$pdf = $cfdi->toPdf();
echo("Transaction ID: " . $pdf->getTransactionId() . "\n");
echo("Total Pages: " . $pdf->getTotalPages() . "\n");
echo("Total Time: " .$pdf->getTotalTimeMs() . "\n");
print_r($pdf->getMetadata());
# Save in file system
file_put_contents('./result.pdf', $pdf->toBuffer());
} catch (Exception $e) {
echo "An error occurred during the conversion: " . $e->getMessage();
}
The $redoc
object is an instance of RedocmxClient
, created using new RedocmxClient(api_key)
.
Method | Description |
---|---|
$redoc->cfdi()->fromFile(filePath) | Returns: Cfdi - Instance Loads file content from the file system for converting a CFDI to PDF. The file should be valid XML for a CFDI. It returns an instance of the Cfdi class, which can be used to obtain the PDF. |
$redoc->cfdi()->fromString(fileContent) | Returns: Cfdi - Instance Uses a CFDI as a string for converting the CFDI to PDF. The string should be valid XML for a CFDI. It returns an instance of the Cfdi class, which can be used to obtain the PDF. |
The $cfdi
object is an instance of Cfdi
, created using $redoc->cfdi()->fromFile(filePath)
or $redoc->cfdi()->fromString(fileContent)
.
Method | Description |
---|---|
$cfdi()->setAddenda(str) | Params: String Allows the use of a redoc addenda for full control over the design of the final PDF. |
$cfdi()->toPdf(options) | Params: Object - PdfOptions Returns: Pdf - Instance An instance of the Pdf class, which, when invoked, converts the CFDI into a PDF and stores it, along with the generated data from the conversion request. |
[
"style_pdf"=>"John"
]
The $pdf
object is an instance of Pdf
, created from $cfdi->toPdf(options)
.
Method | Description |
---|---|
$pdf->toBuffer() | Returns: Buffer The PDF document as a buffer, ready for storage in the file system or to be sent back in an HTTP request. |
$pdf->getTransactionId() | Returns: String - UUID A unique ID for the transaction request to the redoc service. |
$pdf->getTotalPages() | Returns: Integer The total number of pages generated for the PDF file. |
$pdf->getTotalTimeMs() | Returns: Integer Time in milliseconds taken to convert the CFDI to PDF. |
$pdf->getMetadata() | Returns: Object - CfdiMetadata General information from the converted CFDI. |
[
TDB...
]
Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs, features, or improvements.
This project is licensed under the MIT License - see the LICENSE file for details.