How to Convert HTML to PDF with cURL

Learn how to use cURL to convert HTML to PDF with the pdfRest Convert to PDF API
Share this page

Why Convert HTML to PDF with cURL?

The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to programmatically convert HTML files into PDF documents. This tutorial will guide you through the process of sending an API call to the Convert to PDF endpoint using cURL, a command-line tool for transferring data with URLs. By understanding how to use this tool, you can automate the conversion of HTML to PDF format, integrating seamlessly into your applications or workflows.

Imagine you are managing a web application that generates dynamic HTML reports or invoices. To maintain consistency and ensure compatibility across different platforms, you might want to convert all generated HTML files into PDFs. Using the Convert to PDF API, you can automate this process, enhancing the user experience by providing a unified document format that is widely accepted and easily shareable.

Convert HTML to PDF with cURL Code Example

curl -X POST "https://api.pdfrest.com/pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
  -F "file=@/path/to/file.html;type=text/html" \
  -F "output=example_out"

Source: GitHub Repository

Breaking Down the Code

The provided cURL command is a POST request to the pdfRest API endpoint for converting HTML documents to PDF. Let's break down each part:

curl -X POST "https://api.pdfrest.com/pdf"

This line specifies that a POST request is being made to the URL https://api.pdfrest.com/pdf, which is the endpoint for the Convert to PDF API.

-H "Accept: application/json"

This header indicates that the client expects the server to respond with JSON-formatted data.

-H "Content-Type: multipart/form-data"

This header specifies that the request body will be sent as multipart form data, which is necessary for file uploads.

-H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

The API key is a unique identifier used to authenticate the request. Replace the placeholder with your actual API key.

-F "file=@/path/to/file.html;type=text/html"

This form field uploads the HTML file you want to convert. Replace /path/to/file.html with the path to your actual HTML file, and specify the content type as text/html.

-F "output=example_out"

This form field specifies the output file name or identifier for the converted PDF. In this example, the output is named example_out.

Beyond the Tutorial

In this tutorial, you've learned how to use cURL to send a request to the pdfRest Convert to PDF API, enabling you to convert HTML files to PDFs programmatically. To further explore the capabilities of pdfRest, try out all the API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.

Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit this GitHub page.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.