SDK Snippets
Minimal client snippets for common languages.
JavaScript (Node.js)
const res = await fetch("https://api.html2pdf.online/v1/convert", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Basic " + Buffer.from("sk_live_xxx:").toString("base64")
},
body: JSON.stringify({ source: "<html><body><h1>Hello</h1></body></html>" })
});
const pdf = Buffer.from(await res.arrayBuffer());Python
import requests
r = requests.post(
"https://api.html2pdf.online/v1/convert",
auth=("sk_live_xxx", ""),
json={"source":"<html><body><h1>Hello</h1></body></html>"}
)
open("output.pdf", "wb").write(r.content)PHP
$ch = curl_init("https://api.html2pdf.online/v1/convert");
curl_setopt($ch, CURLOPT_USERPWD, "sk_live_xxx:");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["source" => "<html><body><h1>Hello</h1></body></html>"]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$pdf = curl_exec($ch);
file_put_contents("output.pdf", $pdf);