HTML to PDF API with the Fewest Lines of Code

The PDFmyURL HTML to PDF API is one of the simplest ways to convert web pages into high-quality PDF documents. Unlike other services that require installing SDKs, handling complex objects, or configuring rendering engines, PDFmyURL works with a single HTTP request. This page demonstrates how you can integrate our API in various programming languages using only a few lines of code.

Table of Contents


The core idea: one URL

At the heart of the PDFmyURL API is a simple concept: create a PDF by calling a single URL. This makes the API extremely easy to integrate into any system, automation workflow, backend, frontend, or command line tool.

https://pdfmyurl.com/api?license=yourlicensekey&url=https://www.example.com

The API accepts both GET and POST requests, and supports a wide range of rendering options that can be added as query parameters.


Command line examples

Using command-line tools like curl and wget is the quickest possible way to generate PDFs from URLs. These 1-line examples are perfect for cron jobs, shell scripts, automation pipelines, or integrations where you need a simple and reliable HTML to PDF conversion.

curl

This curl example converts a public URL into a PDF file with a single HTTP GET request.

curl "https://pdfmyurl.com/api?license=yourlicensekey&url=https://www.example.com" -o mypdf.pdf

wget

The wget version is equally simple and ideal for environments where curl is not installed.

wget "https://pdfmyurl.com/api?license=yourlicensekey&url=https://www.example.com" -O mypdf.pdf

PHP example

PHP developers can integrate the PDFmyURL API instantly without installing any libraries. The built-in functions file_get_contents and file_put_contents are enough to download the PDF bytes and store them in a file. This makes it ideal for CMS platforms, form processors, and backend scripts.

$url = urlencode('https://www.example.com');
$pdf = file_get_contents("https://pdfmyurl.com/api?license=yourlicensekey&url=$url");
file_put_contents('mypdf.pdf', $pdf);

Python example

Python is commonly used for automation, backend APIs, data pipelines, and serverless environments. Using the popular requests library, you can convert any URL to a PDF in just a few lines, making the integration lightweight and extremely easy to maintain.

import requests

url = 'https://www.example.com'
api = 'https://pdfmyurl.com/api?license=yourlicensekey&url=' + url
pdf = requests.get(api).content
open('mypdf.pdf', 'wb').write(pdf)

Node.js example

Node.js developers can integrate PDFmyURL using the native fetch API available in modern versions of Node. This approach requires no dependencies and is ideal for Express servers, serverless functions, and microservices that need a fast HTML-to-PDF conversion layer.

import fs from 'fs';

const url = 'https://www.example.com';
const api = 'https://pdfmyurl.com/api?license=yourlicensekey&url=' +
            encodeURIComponent(url);

const res = await fetch(api);
const pdf = Buffer.from(await res.arrayBuffer());
fs.writeFileSync('mypdf.pdf', pdf);

JavaScript in the browser

You can also call the API directly from JavaScript in the browser. This is useful for client-side web apps and dashboards. However, doing so exposes your license key. For some use cases this is acceptable, but if you want to avoid exposing it, you can configure domain-restricted usage in your PDFmyURL Members Area and omit the license key entirely.

Direct browser example (license exposed)

This example triggers a file download entirely in the browser after fetching the PDF from the API.

const api = "https://pdfmyurl.com/api?license=yourlicensekey&url=" +
            encodeURIComponent("https://www.example.com");

fetch(api)
  .then(res => res.blob())
  .then(blob => {
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = "mypdf.pdf";
    a.click();
  });

Browser example without exposing a license key

If you only convert URLs from your own domain, configure that domain in the PDFmyURL Members Area. Then you can generate PDFs client-side without the license parameter. This is perfect for SPAs, dashboards, and client-side reporting tools.

const api = "https://pdfmyurl.com/api?url=" +
            encodeURIComponent("https://yourdomain.com/page");

fetch(api)
  .then(res => res.blob())
  .then(blob => {
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = "mypdf.pdf";
    a.click();
  });

C# / .NET example

In .NET applications - whether running on Windows, Linux, Azure Functions, or ASP.NET - integrating PDF generation is straightforward with HttpClient. This snippet downloads the PDF and writes it directly to disk, making it suitable for enterprise apps, internal systems, and batch processes.

using var client = new HttpClient();
var url = "https://www.example.com";
var api = "https://pdfmyurl.com/api?license=yourlicensekey&url=" +
          Uri.EscapeDataString(url);
var bytes = await client.GetByteArrayAsync(api);
await File.WriteAllBytesAsync("mypdf.pdf", bytes);

Java example

Java developers can use the core JDK to call the API without adding external libraries. This makes the PDFmyURL API ideal for Spring Boot, Jakarta EE, Android server backends, legacy JVM systems, and high-security environments where additional dependencies are discouraged.

String url = "https://www.example.com";
String api = "https://pdfmyurl.com/api?license=yourlicensekey&url=" +
             java.net.URLEncoder.encode(url, "UTF-8");
try (InputStream in = new java.net.URL(api).openStream();
     OutputStream out = new java.io.FileOutputStream("mypdf.pdf")) {
    in.transferTo(out);
}

Ruby example

Ruby's concise syntax pairs nicely with PDFmyURL's minimal API. This example is ideal for Rails apps, background jobs, or any script that needs to turn URLs into PDF files on the fly.

require 'open-uri'
url = 'https://www.example.com'
api = "https://pdfmyurl.com/api?license=yourlicensekey&url=#{url}"
File.write('mypdf.pdf', URI.open(api).read)

Perl example

Even in Perl, generating PDFs from URLs requires almost no code. Using getstore, you can download and save the PDF in a single line, making PDFmyURL useful for legacy systems and server scripts alike.

use LWP::Simple;

my $url = 'https://www.example.com';
my $api = 'https://pdfmyurl.com/api?license=yourlicensekey&url=' . $url;
getstore($api, 'mypdf.pdf');

Why so little code is required

Most HTML-to-PDF solutions require complex SDKs, heavy libraries, render engines, and verbose configuration. PDFmyURL removes all that complexity by using a single endpoint that handles the rendering for you on our servers.

Because the API is URL-based, every code example in this article reduces to:

  • Build the API URL
  • Send an HTTP request
  • Save the PDF bytes

This simplicity makes the API ideal for any platform-web, backend, desktop, serverless, IoT, and command-line tools - while still supporting dozens of advanced PDF rendering options.

Explore all features on the full HTML to PDF API documentation.