Conversión CFDI a PDF
Project description
Converting CFDI to PDF
redocmx
The redocmx
module is a Python 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.
This package is synchronous.
Installation
To install the module, run:
pip install redocmx
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.
from redocmx import RedocmxClient
redoc = RedocmxClient('your_api_key_here')
Converting CFDI to PDF
The redocmx
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.from_file('./path/to/your/file.xml')
Option 2: Use an XML Content String
cfdi = redoc.cfdi.from_string('<xml_content_string_here>');
Generating the PDF
To convert the loaded CFDI to a PDF:
try:
pdf = cfdi.to_pdf()
with open('./nomina.pdf', 'wb') as pdf_file:
pdf_file.write(pdf.to_buffer())
print(f"Transaction ID: {pdf.get_transaction_id()}")
print(f"Total Pages: {pdf.get_total_pages()}")
print(f"Total Time: {pdf.get_total_time_ms()} ms")
print(f"Metadata: {pdf.get_metadata()}")
except:
print("An error occurred during the conversion")
Examples
API Reference
Redoc
The redoc
object is an instance of Redoc
, created using RedocmxClient(api_key)
.
Method | Description |
---|---|
redoc.cfdi.from_file(file_path) | 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.from_string(file_content) | 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. |
Cfdi
The cfdi
object is an instance of Cfdi
, created using redoc.cfdi.from_file(file_path)
or redoc.cfdi.from_string(file_content)
.
Method | Description |
---|---|
cfdi.set_addenda(str) | Params: String Allows the use of a redoc addenda for full control over the design of the final PDF. |
cfdi.to_pdf(options) | Params: Dictionary - 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. |
PdfOptions
{
"style_pdf": "John"
}
The pdf
object is an instance of Pdf
, created from cfdi.to_pdf(options)
.
Method | Description |
---|---|
pdf.to_buffer() | 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.get_transaction_id() | Returns: String - UUID A unique ID for the transaction request to the redoc service. |
pdf.get_total_pages() | Returns: Integer The total number of pages generated for the PDF file. |
pdf.get_total_time_ms() | Returns: Integer Time in milliseconds taken to convert the CFDI to PDF. |
pdf.get_metadata() | Returns: Dictionary - CfdiMetadata General information from the converted CFDI. |
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file redocmx-0.0.2.tar.gz
.
File metadata
- Download URL: redocmx-0.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a51b3d3c6fbc7c0b0a452de969d8df8c1c11309fd157fb876e0d87582493610b |
|
MD5 | 641a4a2c0a06e199dc6c863f3170cfff |
|
BLAKE2b-256 | 11765d2c89f104f67e52671b0a2cafe39d656c51d07c0a96105fd3a57ba095eb |
File details
Details for the file redocmx-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: redocmx-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6aa0e6b8e7817e4ac11623d9353e39cb4886d9ae661a7b4f94b9a08eaebe7e4d |
|
MD5 | 36a095b0a379d395956effa564988f3d |
|
BLAKE2b-256 | 1c6edd054326aadb4be6d4d4038daa67932e228e0a41e45360cf412c8d792cb0 |