redocmx / client-php
Converción de CFDI a PDF.
Requires
- php: >=5.6.0
This package is not auto-updated.
Last update: 2024-11-19 17:50:10 UTC
README
redocmx/client-php
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.
Requirements
PHP 5.6.0 and later.
Installation
To install the module, run:
composer require redocmx/client-php
To use the bindings, use Composer's autoload:
require_once 'vendor/autoload.php';
Usage
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');
Converting CFDI to PDF
The redocmx/client-php
provides two options for loading CFDI data: from a file or directly from a string.
Option 1: Load XML from the File System
$cfdi = $redoc->cfdi()->fromFile('./path/to/your/file.xml');
Option 2: Use an XML Content String
$cfdi = $redoc->cfdi()->fromString('<xml_content_string_here>');
Generating the PDF
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(); }
Examples
API Reference
RedocmxClient
The $redoc
object is an instance of RedocmxClient
, created using new RedocmxClient(api_key)
.
Cfdi
The $cfdi
object is an instance of Cfdi
, created using $redoc->cfdi()->fromFile(filePath)
or $redoc->cfdi()->fromString(fileContent)
.
PdfOptions
[ "style_pdf"=>"John" ]
The $pdf
object is an instance of Pdf
, created from $cfdi->toPdf(options)
.
CfdiMetadata
[ TDB... ]
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs, features, or improvements.
License
This project is licensed under the MIT License - see the LICENSE file for details.