Contoh Penggunaan API
Implementasi langsung dalam berbagai bahasa
HTML to PDF
Konversi konten HTML (string) menjadi file PDF
JavaScript (Fetch API)
async function convertHtmlToPdf() {
const apiKey = 'YOUR_API_KEY_HERE';
const apiUrl = 'https://api.pdfautoran.com/api/v1/html-to-pdf';
// HTML yang akan di-convert ke PDF
const htmlContent = `
<html>
<head>
<title>Contoh PDF</title>
</head>
<body>
<h1 style="color:#2563eb;">Hello PDF AUTORAN</h1>
<p>Ini contoh dokumen yang dibuat dari HTML string.</p>
</body>
</html>
`;
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: htmlContent,
fileName: 'contoh-html-to-pdf'
})
});
if (!response.ok) {
let errorBody = null;
try {
errorBody = await response.json();
} catch (_) {
errorBody = { message: 'Unknown error' };
}
console.error('Error:', errorBody.message || 'Gagal generate PDF');
return;
}
// Response sukses = file PDF (binary)
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
// Download otomatis
const a = document.createElement('a');
a.href = url;
a.download = 'contoh-html-to-pdf.pdf';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
Stamp PNG to PDF
Tempelkan tanda tangan / logo PNG ke posisi tertentu di PDF
JavaScript (Fetch API)
async function stampPdf() {
const apiKey = 'YOUR_API_KEY_HERE';
const apiUrl = 'https://api.pdfautoran.com/api/v1/stamp';
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
pdfUrl: 'https://example.com/document.pdf',
imageUrl: 'https://example.com/stamp.png',
page: 1, // halaman ke-1
x: 60, // posisi X dari kiri (point)
y: 300, // posisi Y dari bawah (point)
width: 80, // lebar stamp
height: 30, // tinggi stamp
fileName: 'hasil-stamp'
}),
});
if (!response.ok) {
let errorBody = null;
try {
errorBody = await response.json();
} catch (_) {
errorBody = { message: 'Unknown error' };
}
console.error('Error:', errorBody.message || 'Gagal stamp PDF');
return;
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'hasil-stamp.pdf';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
Merge PDF
Gabungkan beberapa PDF menjadi satu file (urutan mengikuti array)
JavaScript (Fetch API)
async function mergePdfs() {
const apiKey = 'YOUR_API_KEY_HERE';
const apiUrl = 'https://api.pdfautoran.com/api/v1/merge';
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
urls: [
'https://example.com/doc1.pdf',
'https://example.com/doc2.pdf',
'https://example.com/doc3.pdf',
],
fileName: 'hasil-merge'
}),
});
if (!response.ok) {
let errorBody = null;
try {
errorBody = await response.json();
} catch (_) {
errorBody = { message: 'Unknown error' };
}
console.error('Error:', errorBody.message || 'Gagal merge PDF');
return;
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'hasil-merge.pdf';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
Compress PDF
Perkecil ukuran file PDF tanpa mengubah jumlah halaman (low / medium / high).
JavaScript (Fetch API)
async function compressPdf() {
const apiKey = 'YOUR_API_KEY_HERE';
const apiUrl = 'https://api.pdfautoran.com/api/v1/compress';
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
pdfUrl: 'https://example.com/source.pdf',
quality: 'medium', // optional: low | medium | high
fileName: 'hasil-compress' // optional
}),
});
if (!response.ok) {
let errorBody = null;
try {
errorBody = await response.json();
} catch (_) {
errorBody = { message: 'Unknown error' };
}
console.error('Error:', errorBody.message || 'Gagal compress PDF');
return;
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'hasil-compress.pdf';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
💡 Tips & Best Practices
Jaga Kerahasiaan API Key
Jangan pernah commit API Key ke repository publik. Simpan di environment variables (.env).
Timeout Handling
Tambahkan timeout pada HTTP client Anda. Proses generate / compress PDF bisa butuh beberapa detik tergantung ukuran file.
Simpan PDF di Storage Anda
API mengembalikan file PDF langsung di response. Simpan ke storage/server Anda sendiri untuk penggunaan jangka panjang.
Monitoring Credits
Pantau penggunaan credits di dashboard. Setiap pemanggilan endpoint (HTML_TO_PDF, MERGE, STAMP, PDF_COMPRESS) akan mengurangi credits sesuai jenis fitur.