Skip to main content

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redocmx-0.0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

redocmx-0.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file redocmx-0.0.1.tar.gz.

File metadata

  • Download URL: redocmx-0.0.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for redocmx-0.0.1.tar.gz
Algorithm Hash digest
SHA256 966cf7d53653a5c12b5254b14977c173c0c8b9fc8e2e968f7e9a1aa31613f300
MD5 121b869cbb267da9fa5691480d1ab092
BLAKE2b-256 c45bf44a10981bac7b0d7d320edb8029bc37dc9e5ad84c64f1f15944b69fef8a

See more details on using hashes here.

File details

Details for the file redocmx-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: redocmx-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for redocmx-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ab82de08a0a1f7b2c402006cde6b5067a7529ca51e73880b3897329411e1f21
MD5 9b2dacff288a4f939d6de3863853455b
BLAKE2b-256 a7b3d478cf9ed33c029250242856bd9d4300c23bf0073e3f8841793e3ba3dccc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page