How to Convert a URL to a PDF Document using C/C++
Viewing the contents of any given URL page typically involves entering that URL into our web browser and retrieving its associate files. Using the below code, you can take advantage of a free API that accesses URL contents, captures and image of those contents, and returns that image in PDF file format. This allows you to programmatically store current iterations of websites in development, view insecure URL contents, and much more, making it easy to store or share those contents with peers and colleagues alike.
To complete your API request, you’ll first need to grab a free-tier API key (which allows a limit of 800 API calls per month with no additional commitments). You can get one by registering a free account on the Cloudmersive website.
After that, you can begin structuring your API call by installing libcurl in your C/C++ project:
libcurl/7.75.0
And then copying the following ready-to-run code examples into your file:
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/web/url/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: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
const char *data = "Url=%3Cstring%3E&ExtraLoadingWait=%3Cinteger%3E&IncludeBackgroundGraphics=%3Cboolean%3E&ScaleFactor=%3Cinteger%3E";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
All done — no more code required! Now you can easily take advantage of a useful value-add service in your applications.