csv to pdf

CSV to PDF conversion with C# .NET.

The CSV (Comma-Separated Values) files serve as a fundamental format for storing tabular data, widely used across various industries and applications. However, when it comes to sharing, presenting, or archiving data, the CSV format may fall short in providing a structured and universally accessible solution. This is where the importance of converting CSV to PDF (Portable Document Format) becomes evident as PDF files offer a standardized and versatile format for documentation, report generation, and data presentation.

CSV to PDF Conversion API

Converting CSV to PDF using GroupDocs.Conversion Cloud SDK for .NET offers a streamlined and efficient solution for transforming tabular data into standardized PDF documents. With this SDK, users can easily convert CSV files to PDF format while maintaining data integrity, formatting, and structure.

Firstly, we need to install the SDK by searching GroupDocs.Conversion-Cloud in NuGet package manager and click the Install button. Another option is to execute the following command in package manager console.

NuGet\Install-Package GroupDocs.Conversion-Cloud -Version 24.4.0

The next important step is to obtain the personalized API credentials(i.e. Client ID and Client Secret). Please follow the instructions specified in this short tutorial explaining the details on how to get the API credentials.

Convert CSV to PDF in C#

Let’s explore the details on how this API handles all aspects of the conversion process, from reading the CSV input to generating the corresponding PDF output, with minimal coding effort required.

  1. Create an instance of Configuration class where we pass client credentials as arguments.
var configurations = new Configuration(clientId, clientSecret1);
  1. Initialize the ConvertApi where we pass Configuration object as an input argument.
var apiInstance = new ConvertApi(configurations);
  1. Upload the input CSV file to the cloud storage.
fileUpload.UploadFile(new UploadFileRequest("input.csv", stream));
  1. Create an instance ConvertSettings where we specify the name for input CSV, resultant format as pdf and the name for output PDF file as arguments.
var settings = new ConvertSettings{...}
  1. Call the ConvertDocumentRequest API to change CSV format to PDF and save the resultant PDF file to the cloud storage.
var response = apiInstance.ConvertDocument(new ConvertDocumentRequest(settings));
// More examples over https://github.com/groupdocs-conversion-cloud/groupdocs-conversion-cloud-dotnet
// Obtain your API credentials
string clientId = "4bdefca3-f08c-4088-9ca0-55c38f4b7f22";
string clientSecret1 = "a43c8b4365246a062688a259abe5b469";
// Create an instance of the Configuration class and initialize it with the Client ID & Client Secret.
var configurations = new GroupDocs.Conversion.Cloud.Sdk.Client.Configuration(clientId, clientSecret1);
// Define the value of ApiBaseUrl to set the base url of CSV to PDF conversion API.
configuration.ApiBaseUrl = "https://api.groupdocs.cloud";
// Initialize an instance of the ConvertApi class with the object of the Configuration class.
var apiInstance = new GroupDocs.Conversion.Cloud.Sdk.Api.ConvertApi(configurations);
// read the content of input Comma Separated Values file from local drive
using (var stream = System.IO.File.OpenRead("input.csv"))
{
// create an instance of FileApi
var fileUpload = new FileApi(configurations);
// upload the input CSV to cloud storage
fileUpload.UploadFile(new UploadFileRequest("input.cs", stream));
// create ConvertSettings where we define the input CSV file name and name of resultant PDF file
var settings = new ConvertSettings
{
StorageName = "internal",
FilePath = "input.cs",
Format = "pdf",
OutputPath = "myResultant.pdf"
};
// Invoke the ConvertDocument method to export CSV to PDF format
var response = apiInstance.ConvertDocument(new ConvertDocumentRequest(settings));
if (response != null && response.Equals("OK"))
{
// print success message
Console.WriteLine("The CSV to PDF conversion completed successfully !");
}
}
view raw csv-to-pdf.cs hosted with ❤ by GitHub

The sample CSV file and the resultant PDF documents can be downloaded from input.csv and myResultant.pdf.

csv to pdf

Image:- A preview of CSV to PDF conversion.

Change CSV to PDF with cURL Commands

Converting CSV files to PDF format using GroupDocs.Conversion Cloud via cURL commands is a seamless process. With just a few simple commands, users can initiate the conversion process and leverage the advanced capabilities of GroupDocs.Conversion Cloud.

Firstly, we need to generate the JWT access token based on client credentials. Once we have generated the JWT token, we need to execute the following cURL command to export CSV to PDF format. The following command saves the resultant PDF to cloud storage.

curl -v "https://api.groupdocs.cloud/v2.0/conversion" \
-X POST \
-H "accept: application/json" \
-H "authorization: Bearer {accessToken}" \
-H  "Content-Type: application/json" \
-d "{  \"FilePath\": \"{sourceFile}\",  \"Format\": \"pdf\",  \"LoadOptions\": {    \"Format\": \"csv\"  },  \"OutputPath\": \"{resultantFile}\"}"

Please replace sourceFile with the name of input CSV file, myResultantFile with the name of resultant PDF file and accessToken with personalized JWT access token.

  • If we have a requirement to save the resultant PDF on local drive, please try using the following cURL command:
curl -v "https://api.groupdocs.cloud/v2.0/conversion" \
-X POST \
-H "accept: application/json" \
-H "authorization: Bearer {accessToken}" \
-H  "Content-Type: application/json" \
-d "{  \"FilePath\": \"{sourceFile}\",  \"Format\": \"pdf\",  \"LoadOptions\": {    \"Format\": \"csv\"  }}" \
-o "myResultant.pdf"

Free CSV to PDF Converter

We also recommend using our lightweight and supper-efficient CSV to PDF Converter App. This App is built on top of GroupDocs.Conversion Cloud APIs and enables you to witness the amazing capabilities of CSV to PDF conversion API.

csv to pdf converter

Conclusion

In conclusion, converting CSV files to PDF format using GroupDocs.Conversion Cloud provides a powerful solution for streamlining data transformation workflows. With its robust capabilities and seamless integration options, the SDK empowers you to effortlessly generate professional-grade PDF documents from tabular data, ensuring data integrity, formatting consistency, and accessibility.

We highly recommend visiting the following links to learn more about: