How to Convert Common Document Formats to PDF using C/C++

Cloudmersive
2 min readAug 1, 2023

--

Exporting files as PDFs is a common and extremely repetitive task. Rather than point, click or even drag files around to make conversions, why not build a free PDF conversion API into your file processing applications instead?

Using the ready-to-run C/C++ code examples below, you can quickly and easily incorporate a service that automatically detects input files and converts them to PDF format, returning the PDF file encoding in the API response. This supports every major Office format (DOCX, XLSX, etc.) along with more than 100 image formats, and it even includes HTML files.

To take advantage of this API, start by installing libcurl in your C/C++ project:

libcurl/7.75.0

After that, copy the below code examples and provide a free-tier API key to authenticate:

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/autodetect/to/pdf");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);

You can get a free-tier API key on the Cloudmersive website, and this allows up to 800 API calls per month with no additional commitment. That’s all there is to it — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet