How to Reduce PDF File Size, Optimize for Storage using C/C++
Looking for an easy way to slim down your PDF documents? Using the below code, you can trim your PDF file size with a free-to-use API service. This will reduce the file size and allow you to control the output quality for any images stored within the PDF file (ranging from 0.0 to 0.3; low quality to high quality respectively).
Before we copy C/C++ examples into our file, let’s make sure we have libcurl installed:
libcurl/7.75.0
After that, let’s structure our API call and supply a free-tier Cloudmersive API key to authorize our request:
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/edit/pdf/optimize/reduce-file-size");
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, "quality: <number>");
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);
With a free-tier API key, you’ll be able to perform this operation up to 800 times per month (with no commitment upon hitting that limit).
That’s all there is to it — no additional code required!