Getting started overview

Getting started with apilayer pdflayer involves a direct process of account creation, API key retrieval, and making an initial HTTP GET request. The API is designed for converting web content, specifically HTML or URLs, into PDF documents programmatically. It operates through a RESTful interface, where authentication is handled by appending an API access key to the request URL. This guide outlines the steps to register, obtain necessary credentials, and execute a basic PDF conversion call.

Before making any API calls, developers must register for an apilayer account. Upon successful registration, an API access key is provided, which serves as the primary authentication credential. This key is used in all API requests to ensure proper authorization and track usage against the user's plan limits. The process is consistent across different programming languages and environments, primarily relying on standard HTTP request mechanisms.

The core functionality of pdflayer revolves around two main endpoints: one for converting raw HTML into PDF, and another for converting a specified URL into PDF. Both endpoints accept various parameters to customize the output PDF, such as page size, orientation, margins, and more. Understanding these parameters and how to include them in the request URL is key to leveraging the API's full capabilities. The official pdflayer API documentation details all available options.

Here's a quick reference table for the getting started steps:

Step What to Do Where
1. Sign Up Create a free apilayer pdflayer account. pdflayer Pricing & Signup
2. Get API Key Locate your unique API access key in your account dashboard. Your pdflayer Dashboard
3. Make Request Construct an HTTP GET request to the API endpoint with your key and content. Code editor, Postman, or cURL
4. Handle Response Process the binary PDF output or JSON error response. Your application logic

Create an account and get keys

To begin using apilayer pdflayer, you must first create an account. This process establishes your identity with the service and provides you with the necessary credentials to authenticate your API requests. apilayer offers a free tier that includes 100 API requests per month, which is suitable for initial testing and development.

Account Registration

  1. Navigate to the apilayer pdflayer pricing page.
  2. Select the 'Free' plan or a suitable paid plan based on your anticipated usage.
  3. Complete the registration form by providing your email address and creating a password.
  4. After registration, you may receive an email to verify your account. Follow the instructions in the email to complete the verification process.

Retrieving Your API Access Key

Once your account is active, your unique API access key will be available in your personal dashboard. This key is crucial for authenticating every request you send to the pdflayer API.

  1. Log in to your apilayer pdflayer dashboard.
  2. On the dashboard, locate the section that displays your 'API Access Key' or 'API Key'.
  3. Copy this key. It is a hexadecimal string (e.g., YOUR_API_ACCESS_KEY) that you will append to your API request URLs.

It is important to keep your API key secure and not expose it in client-side code or public repositories. Treat it like a password, as it grants access to your account's API usage.

Your first request

With your API access key in hand, you can now make your first request to the apilayer pdflayer API. The API supports both converting raw HTML content and converting a specified URL into a PDF document. For simplicity, we'll demonstrate a URL to PDF conversion using cURL, which is a common command-line tool for making HTTP requests and is often pre-installed on Unix-like operating systems (cURL man page).

API Endpoint Structure

The base URL for the pdflayer API is http://api.pdflayer.com/api/convert. All requests require your access_key and either the document_url (for URL to PDF) or document_html (for HTML to PDF) parameter. The output is a binary PDF file.

Example: Converting a URL to PDF with cURL

Replace YOUR_API_ACCESS_KEY with the actual key copied from your dashboard, and https://example.com with the URL you wish to convert.

curl -o output.pdf "http://api.pdflayer.com/api/convert?access_key=YOUR_API_ACCESS_KEY&document_url=https://www.apispine.com"

Explanation:

  • curl: The command-line tool for transferring data with URLs.
  • -o output.pdf: This option tells cURL to save the output of the request to a file named output.pdf. Since the API returns a binary PDF, saving it directly to a file is the typical usage.
  • "http://api.pdflayer.com/api/convert?...": This is the API endpoint URL with query parameters.
  • access_key=YOUR_API_ACCESS_KEY: Your unique API authentication key.
  • document_url=https://www.apispine.com: The URL of the webpage you want to convert to PDF.

After executing this command, a file named output.pdf will be created in your current directory, containing the PDF version of the specified URL.

Example: Converting HTML to PDF with Python

For converting raw HTML, you would use the document_html parameter. Here's an example using Python's requests library (Requests library documentation):

import requests

API_ACCESS_KEY = "YOUR_API_ACCESS_KEY"
HTML_CONTENT = "<h1>Hello, pdflayer!</h1><p>This is a test HTML content.</p>"

params = {
    "access_key": API_ACCESS_KEY,
    "document_html": HTML_CONTENT
}

response = requests.get("http://api.pdflayer.com/api/convert", params=params)

if response.status_code == 200:
    with open("output_html.pdf", "wb") as f:
        f.write(response.content)
    print("PDF from HTML generated successfully!")
else:
    print(f"Error: {response.status_code} - {response.text}")

This Python script sends the HTML content as a parameter and saves the resulting PDF to output_html.pdf.

Common next steps

After successfully making your first API call and generating a PDF, you might consider the following steps to further integrate and optimize your use of apilayer pdflayer:

  1. Explore API Parameters: The pdflayer API offers numerous parameters to customize PDF output, such as page_size, orientation, margin_*, landscape, full_page, and delay. Review the official pdflayer documentation to understand how to control the appearance and behavior of your generated PDFs.

  2. Error Handling: Implement robust error handling in your application. The API returns JSON error objects for invalid requests or issues. Check the HTTP status code and parse the JSON response to provide informative feedback to users or for debugging purposes. Common errors include invalid API keys, missing required parameters, or issues with the target URL/HTML content.

  3. Integrate into Your Application: Incorporate the API calls into your specific application workflow. This might involve generating invoices, reports, converting user-generated content, or archiving web pages. Consider using one of the pdflayer language examples for PHP, Python, Ruby, jQuery, or Go as a starting point.

  4. Monitor Usage: Regularly check your API usage in your apilayer pdflayer dashboard to ensure you stay within your plan limits. If your application's demand grows, consider upgrading your plan to avoid service interruptions. Information on available plans and pricing is provided on their website.

  5. Security Considerations: Ensure that your API key is not exposed client-side. If you are converting user-provided URLs or HTML, implement input validation and sanitization to prevent potential security vulnerabilities, such as Server-Side Request Forgery (SSRF) or injection attacks. The OAuth 2.0 specification, while not directly applicable to pdflayer's API key model, outlines general principles for API security.

Troubleshooting the first call

When making your first API call to apilayer pdflayer, you might encounter some common issues. Here are troubleshooting steps:

1. Invalid API Access Key

  • Symptom: The API returns an error message indicating an invalid API key, or a 401 Unauthorized HTTP status code.
  • Solution: Double-check that the access_key parameter in your request URL exactly matches the key displayed in your apilayer pdflayer dashboard. Ensure there are no leading or trailing spaces, or incorrect characters.

2. Missing Required Parameters

  • Symptom: The API returns an error indicating a missing parameter, such as document_url or document_html.
  • Solution: Verify that you have included either document_url with a valid URL or document_html with the HTML content in your request. One of these is mandatory for conversion. Refer to the pdflayer API documentation for required parameters.

3. Incorrect URL Encoding

  • Symptom: The API returns an error or produces an unexpected PDF output, especially when the URL or HTML content contains special characters (e.g., &, ?, =).
  • Solution: Ensure that all URL parameters, especially the document_url and document_html values, are properly URL-encoded. Most HTTP client libraries handle this automatically, but if you are constructing URLs manually, you may need to use a URL encoding function (e.g., urllib.parse.quote in Python, encodeURIComponent in JavaScript). The MDN Web Docs on URL encoding provides further details.

4. Network Connectivity Issues

  • Symptom: Your request times out, or you receive a network-related error.
  • Solution: Check your internet connection. If operating within a corporate network, ensure that firewalls or proxies are not blocking outbound requests to api.pdflayer.com.

5. Exceeded API Limits

  • Symptom: The API returns a 429 Too Many Requests status code or an error indicating that your monthly request limit has been reached.
  • Solution: Check your apilayer pdflayer dashboard to monitor your API usage. If you have exceeded your free tier limit, consider upgrading to a paid plan.

6. Issues with Target URL/HTML Content

  • Symptom: The PDF is blank, incomplete, or contains unexpected rendering issues, even though the API request was successful.
  • Solution:
    1. Verify that the document_url is publicly accessible and loads correctly in a standard web browser.
    2. Ensure that the HTML provided in document_html is well-formed and valid.
    3. Consider using the delay parameter to give the target page more time to load resources (JavaScript, images) before conversion.
    4. Experiment with other rendering options like full_page or custom CSS to address specific rendering challenges.