Quick Start for Node.js
Both wkhtmltopdf and Puppeteer are well-supported in Node.js, though they can be complex to configure for specific needs. Our PDF generation API provides a quicker, easier way to generate PDFs from HTML, URLs, and dynamic templates. Here's how to start in Node.js.
1. Get Your API Key
- After registering, log in to the Admin Dashboard and navigate to the API Keys section to retrieve your API key.
- Ensure you keep this key secure, as it is essential for authorizing your requests to the PDFBolt API.
2. Make Your First Request
Follow these examples below to send basic requests to the PDFBolt API and generate your PDFs effortlessly.
Choose your preferred endpoint and source combination.
➡️ Endpoints:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
➡️ Sources:
- URL
- HTML
- Template
Convert any webpage into a PDF:
const fs = require('fs');
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/direct', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com',
format: 'A4',
printBackground: true
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync('webpage.pdf', Buffer.from(pdfBuffer));
console.log('PDF generated successfully');
}
generatePdf().catch(console.error);
Convert HTML content directly into a PDF (HTML must be base64 encoded):
const fs = require('fs');
async function generatePdf() {
const htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
const base64Html = Buffer.from(htmlContent).toString('base64');
const response = await fetch('https://api.pdfbolt.com/v1/direct', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
},
body: JSON.stringify({
html: base64Html,
margin: {
top: '30px',
left: '30px'
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync('document.pdf', Buffer.from(pdfBuffer));
console.log('PDF generated successfully');
}
generatePdf().catch(console.error);
Use reusable templates for dynamic PDF generation:
const fs = require('fs');
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/direct', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
templateId: 'your-template-id',
templateData: {
client_name: 'John Doe',
invoice_number: 'INV-001',
total_amount: '$299.99',
line_items: [
{ description: 'Web Development', unit_price: '$200.00' },
{ description: 'Design Services', unit_price: '$99.99' }
]
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync('invoice.pdf', Buffer.from(pdfBuffer));
console.log('PDF generated successfully');
}
generatePdf().catch(console.error);
Create your first template in the app, then use its ID in your API calls.
The Sync endpoint returns a JSON response with a download URL for the PDF.
➡️ Sources:
- URL
- HTML
- Template
Convert any webpage and get download URL:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/sync', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com',
format: 'A4',
printBackground: true
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('PDF URL:', result.documentUrl);
}
generatePdf().catch(console.error);
Convert HTML content and get download URL:
async function generatePdf() {
const htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
const base64Html = Buffer.from(htmlContent).toString('base64');
const response = await fetch('https://api.pdfbolt.com/v1/sync', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
},
body: JSON.stringify({
html: base64Html,
margin: {
top: '30px',
left: '30px'
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('PDF URL:', result.documentUrl);
}
generatePdf().catch(console.error);
Use templates and get download URL:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/sync', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
templateId: 'your-template-id',
templateData: {
client_name: 'John Doe',
invoice_number: 'INV-001',
total_amount: '$299.99',
line_items: [
{ description: 'Web Development', unit_price: '$200.00' },
{ description: 'Design Services', unit_price: '$99.99' }
]
}
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('PDF URL:', result.documentUrl);
}
generatePdf().catch(console.error);
The Async endpoint processes requests asynchronously and sends results via webhook.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage with webhook notification:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/async', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com',
format: 'A4',
printBackground: true,
webhook: 'https://your-app.com/webhook'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Request ID:', result.requestId);
console.log('PDF will be sent to webhook when ready');
}
generatePdf().catch(console.error);
Convert HTML with webhook notification:
async function generatePdf() {
const htmlContent = '<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>';
const base64Html = Buffer.from(htmlContent).toString('base64');
const response = await fetch('https://api.pdfbolt.com/v1/async', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
},
body: JSON.stringify({
html: base64Html,
margin: {
top: '30px',
left: '30px'
},
webhook: 'https://your-app.com/webhook'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Request ID:', result.requestId);
console.log('PDF will be sent to webhook when ready');
}
generatePdf().catch(console.error);
Use templates with webhook notification:
async function generatePdf() {
const response = await fetch('https://api.pdfbolt.com/v1/async', {
method: 'POST',
headers: {
'API-KEY': 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
'Content-Type': 'application/json'
},
body: JSON.stringify({
templateId: 'your-template-id',
templateData: {
client_name: 'John Doe',
invoice_number: 'INV-001',
total_amount: '$299.99',
line_items: [
{ description: 'Web Development', unit_price: '$200.00' },
{ description: 'Design Services', unit_price: '$99.99' }
]
},
webhook: 'https://your-app.com/webhook'
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`HTTP ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Request ID:', result.requestId);
console.log('PDF will be sent to webhook when ready');
}
generatePdf().catch(console.error);
3. What's next?
Continue your journey with PDFBolt by exploring its powerful features and benefits. Start by checking the available API Endpoints and dive into the Conversion Parameters to customize your integration for your unique needs.