The Redocmx library is a C# client designed to interact with the redoc.mx REST API for converting CFDIs (Comprobante Fiscal Digital por Internet) into PDFs. This library simplifies the process of sending XML data to the redoc.mx service and retrieving the converted PDF, along with transaction details and metadata, making it easy to integrate into .NET projects.
To install the Redocmx library, you can use NuGet Package Manager. Run the following command in the Package Manager Console:
Install-Package Redocmx -Version <VERSION>Replace <VERSION> with the specific version of the Redocmx library you wish to install.
Start by including Redocmx in your project:
using Redocmx;Then, instantiate the Redoc class with your API token:
class Program
{
static async Task Main(string[] args)
{
var redoc = new Redoc("api_token");You can load the CFDI data from a file or directly from a string. Below is an example of loading from a file and converting it to PDF:
try
{
var cfdi = redoc.Cfdi.FromFile("./test.xml");
var pdf = await cfdi.ToPdfAsync();
await File.WriteAllBytesAsync("./result.pdf", pdf.Buffer);
Console.WriteLine($"Transaction ID: {pdf.TransactionId}");
Console.WriteLine($"Total time MS: {pdf.TotalTimeMs}");
Console.WriteLine($"Total pages: {pdf.TotalPages}");
Console.WriteLine($"Metadata:");
foreach (var pair in pdf.Metadata)
{
Console.WriteLine($" {pair.Key}: {pair.Value}");
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"An error occurred during the conversion: {ex.Message}");
}
}
}For more detailed examples on how to use the library, please refer to the following resources:
Instantiate the Redoc class to interact with the API. The constructor requires your API token.
Cfdi.FromFile(string path): Loads CFDI data from a file.Cfdi.FromString(string xmlContent): Loads CFDI data directly from a string.ToPdfAsync(): Converts the loaded CFDI to a PDF and returns metadata about the conversion.
We welcome contributions! Feel free to submit pull requests or open issues for bugs, features, or improvements.
This project is licensed under the MIT License. See the LICENSE file in the repository for more information.