Code Examples
Below you will find examples that convert a web page or raw HTML to a PDF with the PDFLayer API. Replace YOUR_ACCESS_KEY with your API Access Key. A successful request returns the PDF file; an error returns JSON.
Code Example - cURL (URL to PDF)
curl -o invoice.pdf "https://api.apilayer.net/pdflayer/api/convert?access_key=YOUR_ACCESS_KEY&document_url=https://pdflayer.com/downloads/invoice.html"Code Example - cURL (HTML to PDF, HTTP POST)
curl -o hello.pdf -X POST "https://api.apilayer.net/pdflayer/api/convert?access_key=YOUR_ACCESS_KEY" \
--data-urlencode "document_html=<h1>Hello world</h1>" \
--data-urlencode "page_size=A4"Code Example - PHP
$query = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'document_url' => 'https://pdflayer.com/downloads/invoice.html',
]);
$ch = curl_init('https://api.apilayer.net/pdflayer/api/convert?' . $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$pdf = curl_exec($ch);
curl_close($ch);
file_put_contents('invoice.pdf', $pdf);Code Example - Python
import requests
params = {
'access_key': 'YOUR_ACCESS_KEY',
}
data = {
'document_html': '<h1>Hello world</h1>',
'page_size': 'A4',
}
response = requests.post('https://api.apilayer.net/pdflayer/api/convert', params=params, data=data)
with open('hello.pdf', 'wb') as f:
f.write(response.content)Code Example - Node.js
const https = require('https');
const fs = require('fs');
const querystring = require('querystring');
const query = querystring.stringify({
access_key: 'YOUR_ACCESS_KEY',
document_url: 'https://pdflayer.com/downloads/invoice.html'
});
https.get('https://api.apilayer.net/pdflayer/api/convert?' + query, response => {
response.pipe(fs.createWriteStream('invoice.pdf'));
});Help & Support
Any issues with your API integration? Contact our support team at [email protected].