Skip to main content

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"
}

Pdf

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.

Metadata

Release files for redocmx 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for redocmx 0.0.2
File Size Uploaded Provenance Actions
redocmx-0.0.2.tar.gz 5.8 kB Without provenance Details for redocmx-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for redocmx 0.0.2
File Interpreter ABI Platform Actions
redocmx-0.0.2-py3-none-any.whl Python 3 none any Details for redocmx-0.0.2-py3-none-any.whl

Total release size: 12.6 kB

Release files / redocmx-0.0.2.tar.gz

Download URL redocmx-0.0.2.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a51b3d3c6fbc7c0b0a452de969d8df8c1c11309fd157fb876e0d87582493610b
BLAKE2b-256 checksum
How to use checksums
11765d2c89f104f67e52671b0a2cafe39d656c51d07c0a96105fd3a57ba095eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / redocmx-0.0.2-py3-none-any.whl

Download URL redocmx-0.0.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6aa0e6b8e7817e4ac11623d9353e39cb4886d9ae661a7b4f94b9a08eaebe7e4d
BLAKE2b-256 checksum
How to use checksums
1c6edd054326aadb4be6d4d4038daa67932e228e0a41e45360cf412c8d792cb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Security for redocmx 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page