Skip to content

Code Examples

cURL

Basic Request

bash
curl -X POST https://app.offloadpdf.com/api/v1/pdf \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Hello World</h1><p>My first PDF!</p>"}'

Binary Download

bash
curl -X POST https://app.offloadpdf.com/api/v1/pdf \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Accept: application/pdf" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Invoice</h1><p>Total: $100</p>"}' \
  --output invoice.pdf

Complex HTML

bash
curl -X POST https://app.offloadpdf.com/api/v1/pdf \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<html><head><style>body { font-family: Arial; } h1 { color: #333; }</style></head><body><h1>Report</h1><p>Detailed content here</p></body></html>"
  }'

JavaScript

Fetch API

javascript
const response = await fetch('https://app.offloadpdf.com/api/v1/pdf', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_TOKEN',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    html: '<h1>Hello World</h1><p>Generated from JavaScript</p>'
  })
});

const data = await response.json();
const pdfBinary = atob(data.pdf_content);

Browser Binary Download

javascript
const response = await fetch('https://app.offloadpdf.com/api/v1/pdf', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_TOKEN',
    'Accept': 'application/pdf',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    html: '<h1>Invoice #12345</h1><p>Amount due: $500</p>'
  })
});

const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'invoice.pdf';
a.click();
window.URL.revokeObjectURL(url);

Node.js with Axios

javascript
const axios = require('axios');
const fs = require('fs');

async function generatePDF() {
  const response = await axios.post(
    'https://app.offloadpdf.com/api/v1/pdf',
    { html: '<h1>Report</h1><p>Generated from Node.js</p>' },
    {
      headers: {
        'Authorization': 'Bearer YOUR_API_TOKEN',
        'Content-Type': 'application/json'
      }
    }
  );

  const pdfBuffer = Buffer.from(response.data.pdf_content, 'base64');
  fs.writeFileSync('output.pdf', pdfBuffer);
}

Node.js Binary Download

javascript
const axios = require('axios');
const fs = require('fs');

async function downloadPDF() {
  const response = await axios.post(
    'https://app.offloadpdf.com/api/v1/pdf',
    { html: '<h1>Document</h1><p>Content here</p>' },
    {
      headers: {
        'Authorization': 'Bearer YOUR_API_TOKEN',
        'Accept': 'application/pdf',
        'Content-Type': 'application/json'
      },
      responseType: 'arraybuffer'
    }
  );

  fs.writeFileSync('document.pdf', response.data);
}

PHP

Guzzle

php
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://app.offloadpdf.com',
    'headers' => [
        'Authorization' => 'Bearer YOUR_API_TOKEN',
        'Content-Type' => 'application/json',
    ]
]);

$response = $client->post('/api/v1/pdf', [
    'json' => [
        'html' => '<h1>Hello from PHP</h1><p>This is a test document.</p>'
    ]
]);

$data = json_decode($response->getBody(), true);
$pdfContent = base64_decode($data['pdf_content']);
file_put_contents('output.pdf', $pdfContent);

Guzzle Binary Download

php
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://app.offloadpdf.com',
    'headers' => [
        'Authorization' => 'Bearer YOUR_API_TOKEN',
        'Accept' => 'application/pdf',
        'Content-Type' => 'application/json',
    ]
]);

$response = $client->post('/api/v1/pdf', [
    'json' => [
        'html' => '<h1>Invoice</h1><p>Total: $250</p>'
    ]
]);

file_put_contents('invoice.pdf', $response->getBody());

cURL Extension

php
<?php
$ch = curl_init('https://app.offloadpdf.com/api/v1/pdf');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer YOUR_API_TOKEN',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'html' => '<h1>Document Title</h1><p>Content here</p>'
]));

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 200) {
    $data = json_decode($response, true);
    $pdf = base64_decode($data['pdf_content']);
    file_put_contents('document.pdf', $pdf);
}

Laravel HTTP Client

php
<?php
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

$response = Http::withToken('YOUR_API_TOKEN')
    ->post('https://app.offloadpdf.com/api/v1/pdf', [
        'html' => '<h1>Laravel Example</h1><p>PDF from Laravel</p>'
    ]);

if ($response->successful()) {
    $pdfContent = base64_decode($response->json('pdf_content'));
    Storage::put('generated.pdf', $pdfContent);
    return response()->download(storage_path('app/generated.pdf'));
}