How to Validate a PDF Document using C/C++
Before we process and store PDF files in a database (or perform any programmatic action on PDF documents whatsoever), it’s important that we check that the file contents match the extension & don’t contain errors and warnings which might disrupt our workflow.
With the below code, we can take advantage of a free API specifically designed to validate PDF file contents and report on any issues which may lie within them. We can find out exactly what errors and warning might be associated with an invalid PDF file, and we can even determine if the file contains password protection measures preventing unauthorized access.
We can structure our API call in two quick steps. We first need to install libcurl in our C/C++ project:
libcurl/7.75.0
After that, we can copy the below code into our file and supply a free-tier API key to authorize our request (this will allow up to 800 API calls per month, with zero commitment upon reaching that limit):
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/validate/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);
Now we can easily identify problems with our PDF documents and resolve them before committing error-stricken files to a sensitive storage location. Super easy!