Integra el procesamiento de PDF en tus aplicaciones. Comprime, une, divide PDFs y extrae texto con nuestra potente API REST.
✓ Incluido en planes Premium y BusinessTodas las solicitudes API requieren autenticación mediante un token Bearer. Puedes obtener tus claves API desde tu panel de control.
Incluye tu clave secreta en la cabecera Authorization:
Authorization: Bearer sk_live_your_secret_key_here
Reduce el tamaño del archivo PDF manteniendo la calidad. Elige entre niveles de compresión bajo, medio o alto.
| Parámetro | Tipo | Descripción |
|---|---|---|
| pdf_base64 * | string | Archivo PDF codificado en Base64 (o usar pdf_url) |
| pdf_url | string | URL para descargar el PDF |
| level | string | Nivel de compresión: low, medium (predeterminado), high |
| webhook_url optional | string | URL para recibir el resultado cuando el procesamiento termine |
curl -X POST https://pdf-ninja.io/api/v1/pdf/compress \
-H "Authorization: Bearer sk_live_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"pdf_base64": "JVBERi0xLjQK...",
"level": "medium"
}'
<?php
$apiKey = 'sk_live_your_secret_key';
$pdfContent = base64_encode(file_get_contents('document.pdf'));
$ch = curl_init('https://pdf-ninja.io/api/v1/pdf/compress');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode([
'pdf_base64' => $pdfContent,
'level' => 'medium'
])
]);
$response = curl_exec($ch);
$result = json_decode($response, true);
if ($result['success']) {
file_put_contents('compressed.pdf', base64_decode($result['data']['pdf_base64']));
echo "Compressed! Saved " . $result['data']['savings_percent'] . "%";
}
?>
const apiKey = 'sk_live_your_secret_key';
// Read file and convert to base64
const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = async function(e) {
const base64 = e.target.result.split(',')[1];
const response = await fetch('https://pdf-ninja.io/api/v1/pdf/compress', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
pdf_base64: base64,
level: 'medium'
})
});
const result = await response.json();
if (result.success) {
// Download compressed PDF
const link = document.createElement('a');
link.href = `data:application/pdf;base64,${result.data.pdf_base64}`;
link.download = 'compressed.pdf';
link.click();
console.log(`Saved ${result.data.savings_percent}%`);
}
};
reader.readAsDataURL(file);
import requests
import base64
api_key = 'sk_live_your_secret_key'
# Read PDF and encode to base64
with open('document.pdf', 'rb') as f:
pdf_base64 = base64.b64encode(f.read()).decode('utf-8')
response = requests.post(
'https://pdf-ninja.io/api/v1/pdf/compress',
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
},
json={
'pdf_base64': pdf_base64,
'level': 'medium'
}
)
result = response.json()
if result['success']:
# Save compressed PDF
compressed_data = base64.b64decode(result['data']['pdf_base64'])
with open('compressed.pdf', 'wb') as f:
f.write(compressed_data)
print(f"Saved {result['data']['savings_percent']}%")
const fs = require('fs');
const axios = require('axios');
const apiKey = 'sk_live_your_secret_key';
// Read PDF and encode to base64
const pdfBuffer = fs.readFileSync('document.pdf');
const pdfBase64 = pdfBuffer.toString('base64');
async function compressPdf() {
const response = await axios.post(
'https://pdf-ninja.io/api/v1/pdf/compress',
{
pdf_base64: pdfBase64,
level: 'medium'
},
{
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
}
);
if (response.data.success) {
// Save compressed PDF
const compressedBuffer = Buffer.from(
response.data.data.pdf_base64,
'base64'
);
fs.writeFileSync('compressed.pdf', compressedBuffer);
console.log(`Saved ${response.data.data.savings_percent}%`);
}
}
compressPdf();
{
"success": true,
"data": {
"pdf_base64": "JVBERi0xLjQK...",
"original_size": 5242880,
"compressed_size": 2621440,
"savings_percent": 50.0,
"method": "ghostscript"
},
"request_id": "abc123-def456"
}
Combina múltiples archivos PDF en un solo documento. Soporta hasta 20 archivos.
| Parámetro | Tipo | Descripción |
|---|---|---|
| files * | array | Array de objetos PDF con pdf_base64 o pdf_url |
| page_size | string | Tamaño de página de salida: original (predeterminado), A4, A3, Letter |
curl -X POST https://pdf-ninja.io/api/v1/pdf/merge \
-H "Authorization: Bearer sk_live_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"files": [
{"pdf_base64": "JVBERi0xLjQK..."},
{"pdf_url": "https://example.com/document.pdf"},
{"pdf_base64": "JVBERi0xLjQK..."}
],
"page_size": "A4"
}'
<?php
$apiKey = 'sk_live_your_secret_key';
$files = [
['pdf_base64' => base64_encode(file_get_contents('doc1.pdf'))],
['pdf_base64' => base64_encode(file_get_contents('doc2.pdf'))],
['pdf_url' => 'https://example.com/doc3.pdf']
];
$ch = curl_init('https://pdf-ninja.io/api/v1/pdf/merge');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode([
'files' => $files,
'page_size' => 'A4'
])
]);
$response = curl_exec($ch);
$result = json_decode($response, true);
if ($result['success']) {
file_put_contents('merged.pdf', base64_decode($result['data']['pdf_base64']));
echo "Merged {$result['data']['files_count']} files, {$result['data']['total_pages']} pages";
}
?>
import requests
import base64
api_key = 'sk_live_your_secret_key'
files = []
for filename in ['doc1.pdf', 'doc2.pdf', 'doc3.pdf']:
with open(filename, 'rb') as f:
files.append({
'pdf_base64': base64.b64encode(f.read()).decode('utf-8')
})
response = requests.post(
'https://pdf-ninja.io/api/v1/pdf/merge',
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
},
json={
'files': files,
'page_size': 'A4'
}
)
result = response.json()
if result['success']:
merged_data = base64.b64decode(result['data']['pdf_base64'])
with open('merged.pdf', 'wb') as f:
f.write(merged_data)
print(f"Merged {result['data']['files_count']} files")
{
"success": true,
"data": {
"pdf_base64": "JVBERi0xLjQK...",
"files_count": 3,
"total_pages": 25,
"size": 2048000,
"method": "ghostscript"
}
}
Divide un PDF en múltiples archivos por páginas, rangos o partes iguales.
| Parámetro | Tipo | Descripción |
|---|---|---|
| pdf_base64 * | string | Archivo PDF codificado en Base64 (o usar pdf_url) |
| mode | string | Modo de división: pages (1 por página), range (personalizado), fixed (N páginas cada uno), equal (N partes iguales) |
| options | object | Opciones específicas del modo (ver ejemplos abajo) |
# Split by custom page ranges
curl -X POST https://pdf-ninja.io/api/v1/pdf/split \
-H "Authorization: Bearer sk_live_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"pdf_base64": "JVBERi0xLjQK...",
"mode": "range",
"options": {
"pages": [[1, 2, 3], [4, 5], [6, 7, 8, 9, 10]]
}
}'
# Split into 3 equal parts
curl -X POST https://pdf-ninja.io/api/v1/pdf/split \
-H "Authorization: Bearer sk_live_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"pdf_base64": "JVBERi0xLjQK...",
"mode": "equal",
"options": {"num_files": 3}
}'
import requests
import base64
api_key = 'sk_live_your_secret_key'
with open('document.pdf', 'rb') as f:
pdf_base64 = base64.b64encode(f.read()).decode('utf-8')
# Split into fixed-size chunks (5 pages each)
response = requests.post(
'https://pdf-ninja.io/api/v1/pdf/split',
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
},
json={
'pdf_base64': pdf_base64,
'mode': 'fixed',
'options': {'pages_per_file': 5}
}
)
result = response.json()
if result['success']:
for i, file in enumerate(result['data']['files']):
pdf_data = base64.b64decode(file['pdf_base64'])
with open(f'split_{i+1}.pdf', 'wb') as f:
f.write(pdf_data)
print(f"Created split_{i+1}.pdf with pages {file['pages']}")
{
"success": true,
"data": {
"original_pages": 15,
"files_count": 3,
"files": [
{
"pdf_base64": "JVBERi0...",
"pages": [1, 2, 3],
"page_count": 3,
"size": 512000,
"name": "part_1"
},
...
]
}
}
Extrae texto de PDFs e imágenes usando reconocimiento óptico de caracteres. Soporta más de 30 idiomas.
| Parámetro | Tipo | Descripción |
|---|---|---|
| file_base64 * | string | PDF o imagen codificado en Base64 (o usar file_url) |
| language | string | Código de idioma: eng (predeterminado), spa, fra, deu, ita, por, chi_sim, jpn, kor, ara... |
| output_format | string | Formato de salida: text (predeterminado) o json (con desglose por página) |
curl -X POST https://pdf-ninja.io/api/v1/pdf/ocr \
-H "Authorization: Bearer sk_live_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"file_base64": "JVBERi0xLjQK...",
"language": "eng",
"output_format": "json"
}'
import requests
import base64
api_key = 'sk_live_your_secret_key'
# OCR a scanned PDF in Spanish
with open('scanned_document.pdf', 'rb') as f:
file_base64 = base64.b64encode(f.read()).decode('utf-8')
response = requests.post(
'https://pdf-ninja.io/api/v1/pdf/ocr',
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
},
json={
'file_base64': file_base64,
'language': 'spa',
'output_format': 'json'
}
)
result = response.json()
if result['success']:
print(f"Extracted {result['data']['word_count']} words")
print(result['data']['text'])
{
"success": true,
"data": {
"text": "This is the extracted text from the document...",
"word_count": 1234,
"char_count": 5678,
"method": "python",
"pages": [
{"page": 1, "text": "Page 1 content..."},
{"page": 2, "text": "Page 2 content..."}
]
}
}
Consulta el estado y recupera los resultados de trabajos asíncronos.
# Get specific job status
curl https://pdf-ninja.io/api/v1/pdf/jobs/job_abc123 \
-H "Authorization: Bearer sk_live_your_secret_key"
# List all jobs
curl "https://pdf-ninja.io/api/v1/pdf/jobs?status=completed&page=1" \
-H "Authorization: Bearer sk_live_your_secret_key"
{
"success": true,
"data": {
"job_id": "job_abc123",
"operation": "compress",
"status": "completed",
"created_at": "2025-01-05T12:00:00Z",
"completed_at": "2025-01-05T12:00:05Z",
"data": {
"pdf_base64": "JVBERi0...",
"savings_percent": 45.2
}
}
}
Send documents for electronic signature. Create signature requests, track status, send reminders, and download signed documents with certificates.
Your API key needs the following permissions for e-signature operations:
signature:create signature:read signature:cancel signature:remind
Send a document to one or more signers for electronic signature.
| Name | Type | Description |
|---|---|---|
| title * | string | Document title shown to signers |
| signers * | array | Array of signers with name, email, and optional fields |
| pdf_base64 * | string | Base64-encoded PDF document (or use pdf_url) |
| pdf_url | string | URL to download PDF (alternative to pdf_base64) |
| message optional | string | Custom message to include in email to signers |
| expires_in_days optional | integer | Days until expiration (1-30, default: 7) |
| send_emails optional | boolean | Send email invitations automatically (default: true) |
| external_id optional | string | Your custom ID to reference this request |
| metadata optional | object | Custom metadata to store with the request |
| webhook_tag optional | string | Tag to filter which webhooks receive events for this request. Only webhooks with matching tag will be notified. Max 50 chars, alphanumeric with hyphens/underscores. |
| Name | Type | Description |
|---|---|---|
| name * | string | Signer's full name |
| email * | string | Signer's email address |
| order optional | integer | Signing order (for sequential signing) |
| fields optional | array | Signature fields (if empty, signer can place signature freely) |
| signature | Signature field (drawn or typed) |
| initials | Initials field |
| date | Date field (auto-filled) |
| name | Full name field (auto-filled) |
| Email field (auto-filled) | |
| text | Free text input |
| checkbox | Checkbox field |
curl -X POST https://pdf-ninja.io/api/v1/signature-requests \
-H "Authorization: Bearer sk_live_your_secret_key" \
-H "Content-Type: application/json" \
-d '{
"title": "Service Agreement",
"message": "Please sign this agreement at your earliest convenience.",
"signers": [
{
"name": "John Doe",
"email": "[email protected]"
},
{
"name": "Jane Smith",
"email": "[email protected]",
"fields": [
{
"type": "signature",
"page": 1,
"x": 100,
"y": 500,
"width": 200,
"height": 60
}
]
}
],
"pdf_base64": "JVBERi0xLjQK...",
"external_id": "contract-2024-001",
"expires_in_days": 14
}'
<?php
$apiKey = 'sk_live_your_secret_key';
$pdfContent = base64_encode(file_get_contents('contract.pdf'));
$data = [
'title' => 'Service Agreement',
'message' => 'Please sign this agreement.',
'signers' => [
[
'name' => 'John Doe',
'email' => '[email protected]'
],
[
'name' => 'Jane Smith',
'email' => '[email protected]'
]
],
'pdf_base64' => $pdfContent,
'external_id' => 'contract-2024-001',
'expires_in_days' => 14
];
$ch = curl_init('https://pdf-ninja.io/api/v1/signature-requests');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode($data)
]);
$response = curl_exec($ch);
$result = json_decode($response, true);
if ($result['success']) {
echo "Signature request created: " . $result['data']['id'] . "\n";
// Get signing URLs for each signer
foreach ($result['data']['signers'] as $signer) {
echo $signer['name'] . ": " . $signer['sign_url'] . "\n";
}
}
?>
import requests
import base64
api_key = 'sk_live_your_secret_key'
# Read PDF and encode to base64
with open('contract.pdf', 'rb') as f:
pdf_base64 = base64.b64encode(f.read()).decode('utf-8')
data = {
'title': 'Service Agreement',
'message': 'Please sign this agreement.',
'signers': [
{'name': 'John Doe', 'email': '[email protected]'},
{'name': 'Jane Smith', 'email': '[email protected]'}
],
'pdf_base64': pdf_base64,
'external_id': 'contract-2024-001',
'expires_in_days': 14
}
response = requests.post(
'https://pdf-ninja.io/api/v1/signature-requests',
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
},
json=data
)
result = response.json()
if result.get('success'):
print(f"Created: {result['data']['id']}")
# Get signing URLs
for signer in result['data']['signers']:
print(f"{signer['name']}: {signer['sign_url']}")
const apiKey = 'sk_live_your_secret_key';
// Convert file to base64
async function fileToBase64(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result.split(',')[1]);
reader.readAsDataURL(file);
});
}
async function createSignatureRequest(file) {
const pdfBase64 = await fileToBase64(file);
const response = await fetch('https://pdf-ninja.io/api/v1/signature-requests', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: 'Service Agreement',
message: 'Please sign this agreement.',
signers: [
{ name: 'John Doe', email: '[email protected]' },
{ name: 'Jane Smith', email: '[email protected]' }
],
pdf_base64: pdfBase64,
external_id: 'contract-2024-001',
expires_in_days: 14
})
});
const result = await response.json();
if (result.success) {
console.log('Created:', result.data.id);
// Signing URLs for embedding or redirecting
result.data.signers.forEach(signer => {
console.log(`${signer.name}: ${signer.sign_url}`);
});
}
return result;
}
{
"success": true,
"data": {
"id": "sr_a1b2c3d4e5f6g7h8i9j0k1l2",
"status": "pending",
"title": "Service Agreement",
"signers": [
{
"name": "John Doe",
"email": "[email protected]",
"order": 1,
"status": "pending",
"sign_url": "https://pdf-ninja.io/pdf_esignature_sign.php?token=abc123..."
},
{
"name": "Jane Smith",
"email": "[email protected]",
"order": 2,
"status": "pending",
"sign_url": "https://pdf-ninja.io/pdf_esignature_sign.php?token=def456..."
}
],
"external_id": "contract-2024-001",
"created_at": "2024-01-15T10:30:00Z",
"expires_at": "2024-01-29T10:30:00Z"
},
"request_id": "req_xyz789"
}
Retrieve a paginated list of your signature requests with optional filters.
| page | integer | Page number (default: 1) |
| per_page | integer | Items per page (1-100, default: 20) |
| status | string | Filter by status: pending, in_progress, completed, cancelled, expired |
| external_id | string | Filter by your external ID |
# List all signature requests
curl "https://pdf-ninja.io/api/v1/signature-requests?page=1&per_page=20" \
-H "Authorization: Bearer sk_live_your_secret_key"
# Filter by status
curl "https://pdf-ninja.io/api/v1/signature-requests?status=completed" \
-H "Authorization: Bearer sk_live_your_secret_key"
# Find by external ID
curl "https://pdf-ninja.io/api/v1/signature-requests?external_id=contract-2024-001" \
-H "Authorization: Bearer sk_live_your_secret_key"
Retrieve detailed information about a specific signature request including signer status.
curl https://pdf-ninja.io/api/v1/signature-requests/sr_a1b2c3d4e5f6g7h8i9j0k1l2 \
-H "Authorization: Bearer sk_live_your_secret_key"
{
"success": true,
"data": {
"id": "sr_a1b2c3d4e5f6g7h8i9j0k1l2",
"status": "completed",
"title": "Service Agreement",
"message": "Please sign this agreement.",
"signers": [
{
"name": "John Doe",
"email": "[email protected]",
"order": 1,
"status": "signed",
"signed_at": "2024-01-16T14:22:00Z",
"signature_hash": "a1b2c3d4e5f6..."
},
{
"name": "Jane Smith",
"email": "[email protected]",
"order": 2,
"status": "signed",
"signed_at": "2024-01-17T09:15:00Z",
"signature_hash": "f6e5d4c3b2a1..."
}
],
"external_id": "contract-2024-001",
"metadata": {"department": "sales"},
"created_at": "2024-01-15T10:30:00Z",
"expires_at": "2024-01-29T10:30:00Z",
"completed_at": "2024-01-17T09:15:00Z",
"download_url": "/api/v1/signature-requests/sr_a1b2c3.../download",
"certificate_url": "/api/v1/signature-requests/sr_a1b2c3.../certificate"
}
}
Download the signed PDF document (only available for completed requests).
curl https://pdf-ninja.io/api/v1/signature-requests/sr_a1b2c3.../download \
-H "Authorization: Bearer sk_live_your_secret_key" \
-o signed_document.pdf
<?php
$apiKey = 'sk_live_your_secret_key';
$requestId = 'sr_a1b2c3d4e5f6g7h8i9j0k1l2';
$ch = curl_init("https://pdf-ninja.io/api/v1/signature-requests/{$requestId}/download");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey
]
]);
$pdfContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
file_put_contents('signed_contract.pdf', $pdfContent);
echo "Downloaded signed document!";
}
?>
Download the signature certificate (HTML) with audit trail and signature hashes.
curl https://pdf-ninja.io/api/v1/signature-requests/sr_a1b2c3.../certificate \
-H "Authorization: Bearer sk_live_your_secret_key" \
-o signature_certificate.html
Cancel a pending or in-progress signature request. Cannot cancel completed requests.
curl -X POST https://pdf-ninja.io/api/v1/signature-requests/sr_a1b2c3.../cancel \
-H "Authorization: Bearer sk_live_your_secret_key"
{
"success": true,
"data": {
"id": "sr_a1b2c3d4e5f6g7h8i9j0k1l2",
"status": "cancelled",
"cancelled_at": "2024-01-18T11:00:00Z"
}
}
Send email reminders to all signers who haven't signed yet.
curl -X POST https://pdf-ninja.io/api/v1/signature-requests/sr_a1b2c3.../remind \
-H "Authorization: Bearer sk_live_your_secret_key"
{
"success": true,
"data": {
"id": "sr_a1b2c3d4e5f6g7h8i9j0k1l2",
"reminders_sent": 1,
"reminded_emails": ["[email protected]"]
}
}
Receive real-time notifications when signature events occur.
signature_request.created |
Signature request was created |
signature_request.signed |
A signer completed their signature |
signature_request.completed |
All signers have signed |
signature_request.cancelled |
Request was cancelled |
signature_request.expired |
Request expired without completion |
POST /your-webhook-endpoint HTTP/1.1
Content-Type: application/json
X-PDF-Ninja-Event: signature_request.completed
X-PDF-Ninja-Signature: sha256=abc123...
{
"event": "signature_request.completed",
"signature_request_id": "sr_a1b2c3d4e5f6g7h8i9j0k1l2",
"data": {
"id": "sr_a1b2c3d4e5f6g7h8i9j0k1l2",
"title": "Service Agreement",
"status": "completed",
"external_id": "contract-2024-001",
"metadata": {"department": "sales"},
"completed_at": "2024-01-17T09:15:00Z"
},
"timestamp": "2024-01-17T09:15:00Z"
}
Recibe notificaciones cuando los trabajos asíncronos se completen. Añade webhook_url a cualquier solicitud para habilitar el modo asíncrono.
Use tags to route events to specific webhooks. This is useful when you have multiple workflows (e.g., contracts, offers, HR documents) and want different endpoints to handle different types of signature requests.
webhook_tag: "contracts" to your signature request// Creating a webhook with tag
POST /api/v1/webhooks
{
"url": "https://your-server.com/contracts-webhook",
"events": ["signature_request.completed"],
"tag": "contracts"
}
// Creating a signature request that targets this webhook
POST /api/v1/signature-requests
{
"title": "Service Agreement",
"webhook_tag": "contracts", // Only webhooks with tag="contracts" will receive events
"signers": [...]
}
Cuando un trabajo se completa, enviamos una solicitud POST a tu URL de webhook con el siguiente payload:
POST /your-webhook-endpoint HTTP/1.1
Host: your-server.com
Content-Type: application/json
X-PDF-Ninja-Event: pdf.compress.completed
X-PDF-Ninja-Job-ID: job_abc123
X-PDF-Ninja-Signature: sha256=abc123...
{
"event": "pdf.compress.completed",
"job_id": "job_abc123",
"data": {
"pdf_base64": "JVBERi0...",
"original_size": 5000000,
"compressed_size": 2500000,
"savings_percent": 50.0
},
"timestamp": "2025-01-05T12:00:00Z"
}
Si proporcionas un webhook_secret, firmamos el payload con HMAC-SHA256. Verifica la firma para asegurar autenticidad:
<?php
$webhookSecret = 'your_webhook_secret';
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_PDF_NINJA_SIGNATURE'] ?? '';
// Extract hash from "sha256=xxx" format
$expectedSignature = 'sha256=' . hash_hmac('sha256', $payload, $webhookSecret);
if (hash_equals($expectedSignature, $signature)) {
$data = json_decode($payload, true);
// Process the webhook
echo "Valid webhook received!";
} else {
http_response_code(401);
echo "Invalid signature";
}
?>
import hmac
import hashlib
from flask import Flask, request
app = Flask(__name__)
WEBHOOK_SECRET = 'your_webhook_secret'
@app.route('/webhook', methods=['POST'])
def handle_webhook():
payload = request.data
signature = request.headers.get('X-PDF-Ninja-Signature', '')
expected = 'sha256=' + hmac.new(
WEBHOOK_SECRET.encode(),
payload,
hashlib.sha256
).hexdigest()
if hmac.compare_digest(expected, signature):
data = request.json
print(f"Received: {data['event']}")
return 'OK', 200
else:
return 'Invalid signature', 401