This guide shows how to use PDFCrowd's HTML to PDF API from C with libcurl. Your program sends an authenticated HTTP request containing a webpage URL, an HTML file, or an HTML string. PDFCrowd performs the conversion and returns the PDF in the response body.
Set up the example
To run the example, you need a C compiler and the libcurl development files. Download convert.c, which includes the request, response callback, error checks, and resource cleanup.
On a system with pkg-config configured for libcurl, compile it with:
cc -std=c11 -Wall -Wextra convert.c -o convert $(pkg-config --cflags --libs libcurl)
Set your API credentials in the API_USERNAME
and API_KEY environment variables. Both can be demo for testing. For example, in a POSIX shell:
export API_USERNAME=demo export API_KEY=demo
Convert a webpage to PDF
Run the downloaded program with a URL and an output filename:
./convert url https://example.com/ output.pdf
The request goes to https://api.pdfcrowd.com/convert/24.04/ over HTTPS, using your API username and API key
for HTTP Basic authentication. The example sends multipart form fields; libcurl supplies the
request's content type and boundary.
The following excerpt shows the request construction. In the complete program, curl is a
libcurl handle, form is its multipart form, and response holds the returned bytes.
add_text() adds a text field, receive_bytes() collects the response, and CHECK_CURL()
handles libcurl failures:
CHECK_CURL(curl_easy_setopt(curl, CURLOPT_URL, "https://api.pdfcrowd.com/convert/24.04/")); CHECK_CURL(curl_easy_setopt(curl, CURLOPT_USERNAME, username)); CHECK_CURL(curl_easy_setopt(curl, CURLOPT_PASSWORD, api_key)); CHECK_CURL(curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC)); CHECK_CURL(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receive_bytes)); CHECK_CURL(curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response)); CHECK_CURL(add_text(form, "input_format", "html")); CHECK_CURL(add_text(form, "output_format", "pdf")); CHECK_CURL(add_text(form, "content_viewport_width", "balanced")); CHECK_CURL(add_text(form, "page_size", "A4")); CHECK_CURL(add_text(form, "url", "https://example.com/")); CHECK_CURL(curl_easy_setopt(curl, CURLOPT_MIMEPOST, form)); CHECK_CURL(curl_easy_perform(curl));
input_format and output_format select HTML to PDF conversion. The url field specifies the
page to convert; it is separate from the API endpoint. content_viewport_width=balanced lets
the converter choose a viewport width for the page. After checking for HTTP status 200, the
program writes the binary response to output.pdf.
The webpage and its resources must be reachable by PDFCrowd. A URL on localhost refers to
the conversion server, not the computer running this program.
Convert an HTML file or string
Use one input field per request: url, file, or text. The authentication and response
handling stay the same.
Upload an HTML file
For a local file such as report.html, replace the url field with a file part:
curl_mimepart *part = curl_mime_addpart(form); if (!part) { fputs("Cannot allocate the file upload.\n", stderr); goto cleanup; } CHECK_CURL(curl_mime_name(part, "file")); CHECK_CURL(curl_mime_filedata(part, "report.html"));
curl_mime_filedata() reads and uploads the local file. The downloadable program exposes this as:
./convert file report.html report.pdf
If the document depends on local images or stylesheets, package them with the HTML in an archive
and upload the archive as file. Uploading the HTML alone does not include neighboring files.
See the archive input example for supported formats
and choosing the main HTML file.
Send an HTML string
For HTML produced by your application, replace the url field with text:
CHECK_CURL(add_text(form, "text", "<h1>Monthly report</h1><p>Revenue increased.</p>"));
You can also try a short string with the downloaded program:
./convert text '<h1>Monthly report</h1><p>Revenue increased.</p>' report.pdf
Use UTF-8 for HTML text. Images and stylesheets in uploaded or string HTML can use absolute,
publicly reachable URLs. To resolve relative URLs against a public location, include a
<base href="https://example.com/assets/"> element in the HTML document's <head>.
Customize the PDF
Conversion options are additional form fields. For example, add these before sending the request to set the top and bottom margins and use print stylesheets:
CHECK_CURL(add_text(form, "margin_top", "20mm")); CHECK_CURL(add_text(form, "margin_bottom", "20mm")); CHECK_CURL(add_text(form, "use_print_media", "true"));
The main example already sets page_size to A4; replace that value to change the page format.
Use the HTTP parameter names and string values from the
parameter reference. Add each option once to the form.
Handle errors
A completed HTTP transfer can still contain an API error. The program checks the HTTP status before opening the output file:
long status = 0; CHECK_CURL(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status)); if (status != 200) { fprintf(stderr, "PDFCrowd returned HTTP %ld:\n", status); if (response.length) fwrite(response.data, 1, response.length, stderr); fputc('\n', stderr); goto cleanup; }
The response buffer contains bytes with an explicit length, so both PDFs and error bodies are handled without assuming a terminating zero byte. The HTTP response documentation explains API errors.
The complete program also checks allocation, connection, upload, and file-writing failures. It returns a nonzero exit status on failure and releases its libcurl handles and response buffer. It uses a 30-second connection timeout and a 120-second total request timeout; adjust these for your application's expected conversion time.
This example buffers the response in memory. For large outputs, a streaming implementation can write to a temporary file and keep it only after the transfer and HTTP status checks succeed.