docstron 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +2 -0
- package/LICENSE +21 -0
- package/README.md +789 -0
- package/check-connection.js +107 -0
- package/examples/basic-usage.js +91 -0
- package/examples/complete-workflow.js +160 -0
- package/examples/generate-pdf.js +325 -0
- package/examples/invoice-template.js +187 -0
- package/examples/manage-applications.js +114 -0
- package/examples/quick-generate.js +101 -0
- package/index.js +13 -0
- package/lib/Docstron.js +55 -0
- package/lib/constants.js +7 -0
- package/lib/resources/Applications.js +110 -0
- package/lib/resources/Documents.js +173 -0
- package/lib/resources/Templates.js +97 -0
- package/lib/utils/errors.js +38 -0
- package/lib/utils/httpClient.js +68 -0
- package/package.json +42 -0
- package/test/test.js +213 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Quick check - create check-connection.js
|
|
2
|
+
require('dotenv').config();
|
|
3
|
+
const axios = require('axios');
|
|
4
|
+
|
|
5
|
+
const API_KEY = process.env.DOCSTRON_API_KEY;
|
|
6
|
+
const BASE_URL = 'https://api.docstron.com';
|
|
7
|
+
|
|
8
|
+
console.log('Testing connection to Docstron API...');
|
|
9
|
+
console.log('Base URL:', BASE_URL);
|
|
10
|
+
console.log('API Key:', API_KEY ? API_KEY.substring(0, 10) + '...' : 'NOT SET');
|
|
11
|
+
|
|
12
|
+
axios.get(BASE_URL + '/v1/templates', {
|
|
13
|
+
headers: {
|
|
14
|
+
'Authorization': `Bearer ${API_KEY}`,
|
|
15
|
+
'Content-Type': 'applicaiton/json'
|
|
16
|
+
},
|
|
17
|
+
timeout: 10000
|
|
18
|
+
})
|
|
19
|
+
.then(response => {
|
|
20
|
+
console.log('โ
Connection successful!');
|
|
21
|
+
console.log('Status:', response.status);
|
|
22
|
+
})
|
|
23
|
+
.catch(error => {
|
|
24
|
+
console.error('โ Connection failed!');
|
|
25
|
+
if (error.response) {
|
|
26
|
+
console.error('Status:', error.response.status);
|
|
27
|
+
console.error('Message:', error.response.data);
|
|
28
|
+
} else if (error.request) {
|
|
29
|
+
console.error('No response received');
|
|
30
|
+
console.error('Error:', error.message);
|
|
31
|
+
} else {
|
|
32
|
+
console.error('Error:', error.message);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
require('dotenv').config();
|
|
39
|
+
const axios = require('axios');
|
|
40
|
+
|
|
41
|
+
const KEY = process.env.DOCSTRON_API_KEY;
|
|
42
|
+
|
|
43
|
+
const urls = [
|
|
44
|
+
'https://api.docstron.com',
|
|
45
|
+
'https://docstron.com/api',
|
|
46
|
+
'https://backend.docstron.com',
|
|
47
|
+
'https://docstron.com',
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
(async () => {
|
|
51
|
+
for (const url of urls) {
|
|
52
|
+
console.log(`\n๐ Testing: ${url}/templates`);
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const res = await axios.get(url + '/templates', {
|
|
56
|
+
headers: { 'x-api-key': KEY },
|
|
57
|
+
timeout: 5000,
|
|
58
|
+
});
|
|
59
|
+
console.log('โ๏ธ SUCCESS', res.status);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.log('โ FAILED');
|
|
62
|
+
console.log(err.code || err.message);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
})();
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
// require('dotenv').config();
|
|
69
|
+
// const axios = require('axios');
|
|
70
|
+
// const HttpsProxyAgent = require('https-proxy-agent');
|
|
71
|
+
|
|
72
|
+
// const API_KEY = process.env.DOCSTRON_API_KEY;
|
|
73
|
+
|
|
74
|
+
// // corporate proxy
|
|
75
|
+
// const proxyUrl = 'http://10.0.1.222:8080';
|
|
76
|
+
// const httpsAgent = new HttpsProxyAgent(proxyUrl);
|
|
77
|
+
|
|
78
|
+
// // Correct Docstron API base
|
|
79
|
+
// const BASE_URL = 'https://api.docstron.com';
|
|
80
|
+
|
|
81
|
+
// (async () => {
|
|
82
|
+
// try {
|
|
83
|
+
// const res = await axios.get(`${BASE_URL}/v1/templates`, {
|
|
84
|
+
// headers: {
|
|
85
|
+
// 'x-api-key': API_KEY, // โ
correct
|
|
86
|
+
// 'Accept': 'application/json'
|
|
87
|
+
// },
|
|
88
|
+
// httpsAgent,
|
|
89
|
+
// proxy: false, // IMPORTANT
|
|
90
|
+
// timeout: 15000
|
|
91
|
+
// });
|
|
92
|
+
|
|
93
|
+
// console.log('SUCCESS:', res.status);
|
|
94
|
+
// console.log(res.data);
|
|
95
|
+
|
|
96
|
+
// } catch (err) {
|
|
97
|
+
// console.error('FAILED');
|
|
98
|
+
|
|
99
|
+
// if (err.response) {
|
|
100
|
+
// console.log('Status:', err.response.status);
|
|
101
|
+
// console.log('Data:', err.response.data);
|
|
102
|
+
// } else {
|
|
103
|
+
// console.log(err.message);
|
|
104
|
+
// }
|
|
105
|
+
// }
|
|
106
|
+
// })();
|
|
107
|
+
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require('dotenv').config();
|
|
2
|
+
const Docstron = require('../index');
|
|
3
|
+
|
|
4
|
+
async function basicExample() {
|
|
5
|
+
const apiKey = process.env.DOCSTRON_API_KEY;
|
|
6
|
+
const appId = process.env.DOCSTRON_APP_ID;
|
|
7
|
+
|
|
8
|
+
// Validate credentials
|
|
9
|
+
if (!apiKey) {
|
|
10
|
+
console.error('โ Error: DOCSTRON_API_KEY not found!');
|
|
11
|
+
console.error('\n๐ก Please create a .env file with:');
|
|
12
|
+
console.error(' DOCSTRON_API_KEY=your-api-key-here');
|
|
13
|
+
console.error(' DOCSTRON_APP_ID=app-your-application-id-here\n');
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!appId) {
|
|
18
|
+
console.error('โ Error: DOCSTRON_APP_ID not found!');
|
|
19
|
+
console.error('\n๐ก Please add to your .env file:');
|
|
20
|
+
console.error(' DOCSTRON_APP_ID=app-your-application-id-here\n');
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const client = new Docstron(apiKey);
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
console.log('Docstron SDK v' + Docstron.getVersion());
|
|
28
|
+
console.log('API Key:', apiKey.substring(0, 10) + '...');
|
|
29
|
+
console.log('App ID:', appId);
|
|
30
|
+
console.log('Creating a template...\n');
|
|
31
|
+
|
|
32
|
+
// Create a simple template
|
|
33
|
+
const template = await client.templates.create({
|
|
34
|
+
application_id: appId,
|
|
35
|
+
name: 'My First Template - ' + Date.now(),
|
|
36
|
+
content: '<h1>Hello {{name}}!</h1><p>Welcome to {{company}}</p>',
|
|
37
|
+
is_active: true
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log('โ
Template created successfully!');
|
|
41
|
+
console.log('Template ID:', template.template_id);
|
|
42
|
+
console.log('Template Name:', template.name);
|
|
43
|
+
console.log('Created At:', template.created_at);
|
|
44
|
+
console.log('\n');
|
|
45
|
+
|
|
46
|
+
// Get the template
|
|
47
|
+
console.log('Retrieving the template...');
|
|
48
|
+
const retrieved = await client.templates.get(template.template_id);
|
|
49
|
+
console.log('โ
Template retrieved:', retrieved.name);
|
|
50
|
+
console.log('\n');
|
|
51
|
+
|
|
52
|
+
// Update the template
|
|
53
|
+
console.log('Updating the template...');
|
|
54
|
+
const updated = await client.templates.update(template.template_id, {
|
|
55
|
+
name: 'Updated Template Name - ' + Date.now()
|
|
56
|
+
});
|
|
57
|
+
console.log('โ
Template updated:', updated.name);
|
|
58
|
+
console.log('\n');
|
|
59
|
+
|
|
60
|
+
// Clean up - delete the test template
|
|
61
|
+
console.log('Cleaning up...');
|
|
62
|
+
await client.templates.delete(template.template_id);
|
|
63
|
+
console.log('โ
Test template deleted');
|
|
64
|
+
console.log('\n๐ All operations completed successfully!');
|
|
65
|
+
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error('โ Error:', error.message);
|
|
68
|
+
|
|
69
|
+
if (error.name === 'ValidationError') {
|
|
70
|
+
console.error('\nValidation errors:');
|
|
71
|
+
error.errors.forEach(err => {
|
|
72
|
+
console.error(` - ${err.field}: ${err.message}`);
|
|
73
|
+
});
|
|
74
|
+
} else if (error.name === 'AuthenticationError') {
|
|
75
|
+
console.error('\n๐ก Your API key appears to be invalid.');
|
|
76
|
+
console.error(' Please check your .env file and verify the key at:');
|
|
77
|
+
console.error(' https://docstron.com/dashboard');
|
|
78
|
+
} else if (error.name === 'NotFoundError') {
|
|
79
|
+
console.error('\n๐ก Application not found.');
|
|
80
|
+
console.error(' Please check your DOCSTRON_APP_ID in .env file');
|
|
81
|
+
} else if (error.statusCode === 0) {
|
|
82
|
+
console.error('\n๐ก Cannot connect to Docstron API.');
|
|
83
|
+
console.error(' Possible issues:');
|
|
84
|
+
console.error(' - Check your internet connection');
|
|
85
|
+
console.error(' - Verify the API endpoint is correct');
|
|
86
|
+
console.error(' - API might be temporarily unavailable');
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
basicExample();
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
require('dotenv').config();
|
|
2
|
+
const Docstron = require('../index');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Complete workflow: Application โ Template โ Document
|
|
8
|
+
* This demonstrates the full Docstron workflow from start to finish
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
async function completeWorkflow() {
|
|
12
|
+
const apiKey = process.env.DOCSTRON_API_KEY;
|
|
13
|
+
|
|
14
|
+
if (!apiKey) {
|
|
15
|
+
console.error('โ Missing DOCSTRON_API_KEY');
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const client = new Docstron(apiKey);
|
|
20
|
+
|
|
21
|
+
console.log('๐ Complete Docstron Workflow\n');
|
|
22
|
+
console.log('='.repeat(50));
|
|
23
|
+
console.log('SDK Version:', Docstron.getVersion());
|
|
24
|
+
console.log('Features:', JSON.stringify(Docstron.getFeatures(), null, 2));
|
|
25
|
+
console.log('');
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
/*
|
|
29
|
+
// Step 1: Create Application
|
|
30
|
+
console.log('Step 1: Creating application...\n');
|
|
31
|
+
const app = await client.applications.create({
|
|
32
|
+
name: 'Invoice System - ' + Date.now(),
|
|
33
|
+
description: 'Complete workflow demo application',
|
|
34
|
+
is_active: true
|
|
35
|
+
});
|
|
36
|
+
console.log('โ
Application created!');
|
|
37
|
+
console.log(' App ID:', app.app_id);
|
|
38
|
+
console.log('');
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
// Step 2: Create Template
|
|
43
|
+
console.log('Step 2: Creating template...\n');
|
|
44
|
+
const template = await client.templates.create({
|
|
45
|
+
application_id: 'app-c673a1f1-ff8e-4367-98c2-063e57061a55', //app.app_id, //app-c673a1f1-ff8e-4367-98c2-063e57061a55
|
|
46
|
+
name: 'Professional Invoice',
|
|
47
|
+
content: `
|
|
48
|
+
<!DOCTYPE html>
|
|
49
|
+
<html>
|
|
50
|
+
<head>
|
|
51
|
+
<style>
|
|
52
|
+
body { font-family: Arial; padding: 40px; }
|
|
53
|
+
.header { text-align: center; border-bottom: 2px solid #333; padding-bottom: 20px; }
|
|
54
|
+
.details { margin: 30px 0; }
|
|
55
|
+
.total { font-size: 24px; font-weight: bold; color: #e74c3c; text-align: right; }
|
|
56
|
+
</style>
|
|
57
|
+
</head>
|
|
58
|
+
<body>
|
|
59
|
+
<div class="header">
|
|
60
|
+
<h1>INVOICE</h1>
|
|
61
|
+
<p>Invoice #{{invoice_number}}</p>
|
|
62
|
+
</div>
|
|
63
|
+
<div class="details">
|
|
64
|
+
<p><strong>Customer:</strong> {{customer_name}}</p>
|
|
65
|
+
<p><strong>Date:</strong> {{date}}</p>
|
|
66
|
+
<p><strong>Service:</strong> {{service}}</p>
|
|
67
|
+
</div>
|
|
68
|
+
<p class="total">Total: {{total}}</p>
|
|
69
|
+
</body>
|
|
70
|
+
</html>
|
|
71
|
+
`,
|
|
72
|
+
is_active: true
|
|
73
|
+
});
|
|
74
|
+
console.log('โ
Template created!');
|
|
75
|
+
console.log(' Template ID:', template.template_id);
|
|
76
|
+
console.log('');
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
/*
|
|
80
|
+
// Step 3: Generate Document
|
|
81
|
+
console.log('Step 3: Generating PDF...\n');
|
|
82
|
+
const doc = await client.documents.generate(template.template_id, {
|
|
83
|
+
data: {
|
|
84
|
+
invoice_number: 'INV-DEMO-001',
|
|
85
|
+
customer_name: 'Demo Client',
|
|
86
|
+
date: new Date().toLocaleDateString(),
|
|
87
|
+
service: 'SDK Demo Services',
|
|
88
|
+
total: '$999.00'
|
|
89
|
+
},
|
|
90
|
+
response_type: 'document_id'
|
|
91
|
+
});
|
|
92
|
+
console.log('โ
PDF generated!');
|
|
93
|
+
console.log(' Document ID:', doc.document_id);
|
|
94
|
+
console.log(' Download URL:', doc.download_url);
|
|
95
|
+
console.log('');
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
// Step 4: Download PDF
|
|
100
|
+
console.log('Step 4: Downloading PDF...\n');
|
|
101
|
+
const outputDir = path.join(__dirname, '../output');
|
|
102
|
+
if (!fs.existsSync(outputDir)) {
|
|
103
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
104
|
+
}
|
|
105
|
+
const outputPath = path.join(outputDir, 'complete-workflow-demo.pdf');
|
|
106
|
+
await client.documents.download(doc.document_id, outputPath); //document-815b139b-12dd-46de-ab42-0e832bf6f7b0
|
|
107
|
+
console.log('โ
PDF downloaded!');
|
|
108
|
+
console.log(' Saved to:', outputPath);
|
|
109
|
+
console.log('');
|
|
110
|
+
*/
|
|
111
|
+
|
|
112
|
+
/*
|
|
113
|
+
// Step 5: Get Statistics
|
|
114
|
+
console.log('Step 5: Getting statistics...\n');
|
|
115
|
+
const appDetails = await client.applications.get(app.app_id);
|
|
116
|
+
const templates = await client.templates.list(app.templateId);
|
|
117
|
+
const documents = await client.documents.list(app.documentId);
|
|
118
|
+
|
|
119
|
+
console.log('โ
Statistics:');
|
|
120
|
+
console.log(` Application: ${appDetails.name}`);
|
|
121
|
+
console.log(` Templates created: ${templates.length}`);
|
|
122
|
+
console.log(` Total documents: ${documents.length}`);
|
|
123
|
+
console.log('');
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
/*
|
|
127
|
+
// Step 6: Cleanup
|
|
128
|
+
console.log('Step 6: Cleaning up...\n');
|
|
129
|
+
await client.documents.delete(doc.document_id);
|
|
130
|
+
console.log('โ
Document deleted');
|
|
131
|
+
await client.templates.delete(template.template_id);
|
|
132
|
+
console.log('โ
Template deleted');
|
|
133
|
+
await client.applications.delete(app.app_id);
|
|
134
|
+
console.log('โ
Application deleted');
|
|
135
|
+
console.log('');
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
console.log('='.repeat(50));
|
|
139
|
+
console.log('๐ Complete workflow finished successfully!\n');
|
|
140
|
+
console.log('Summary:');
|
|
141
|
+
console.log(' โ
Created application');
|
|
142
|
+
console.log(' โ
Created template');
|
|
143
|
+
console.log(' โ
Generated PDF');
|
|
144
|
+
console.log(' โ
Downloaded PDF');
|
|
145
|
+
console.log(' โ
Cleaned up resources');
|
|
146
|
+
console.log('');
|
|
147
|
+
console.log(`PDF saved at: ${outputPath}`);
|
|
148
|
+
console.log('');
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error('\nโ Workflow failed:', error.message);
|
|
154
|
+
if (error.statusCode) {
|
|
155
|
+
console.error('Status Code:', error.statusCode);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
completeWorkflow();
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
require('dotenv').config();
|
|
2
|
+
const Docstron = require('../index');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
async function generatePdfExample() {
|
|
7
|
+
const apiKey = process.env.DOCSTRON_API_KEY;
|
|
8
|
+
const appId = process.env.DOCSTRON_APP_ID;
|
|
9
|
+
const templateId = process.env.DOCSTRON_TEMPLATE_ID;
|
|
10
|
+
|
|
11
|
+
if (!apiKey || !appId) {
|
|
12
|
+
console.error('โ Missing credentials. Please check your .env file for DOCSTRON_API_KEY and DOCSTRON_APP_ID');
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (templateId === 'template-your-id-here') {
|
|
17
|
+
console.error('โ Please set DOCSTRON_TEMPLATE_ID in your .env file');
|
|
18
|
+
console.error(' Or replace "template-your-id-here" with your actual template ID in the code');
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const client = new Docstron(apiKey);
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
console.log('๐ฏ Docstron SDK v' + Docstron.getVersion());
|
|
26
|
+
console.log('๐ PDF Generation Examples\n');
|
|
27
|
+
console.log('='.repeat(50));
|
|
28
|
+
|
|
29
|
+
/*
|
|
30
|
+
// STEP 1: Creating a template first
|
|
31
|
+
console.log('\n ๐ Step 1: Creating a template...');
|
|
32
|
+
const template = await client.templates.create({
|
|
33
|
+
application_id: appId,
|
|
34
|
+
name: 'Invoice Template - ' + Date.now(),
|
|
35
|
+
content:
|
|
36
|
+
`<!DOCTYPE html>
|
|
37
|
+
<html>
|
|
38
|
+
<head>
|
|
39
|
+
<meta charset="UTF-8">
|
|
40
|
+
<style>
|
|
41
|
+
body {
|
|
42
|
+
font-family: 'Arial', sans-serif;
|
|
43
|
+
max-width: 800px;
|
|
44
|
+
margin: 0 auto;
|
|
45
|
+
padding: 40px;
|
|
46
|
+
color: #333;
|
|
47
|
+
}
|
|
48
|
+
.header {
|
|
49
|
+
text-align: center;
|
|
50
|
+
border-bottom: 3px solid #4a90e2;
|
|
51
|
+
padding-bottom: 20px;
|
|
52
|
+
margin-bottom: 30px;
|
|
53
|
+
}
|
|
54
|
+
.header h1 {
|
|
55
|
+
color: #4a90e2;
|
|
56
|
+
margin: 0;
|
|
57
|
+
}
|
|
58
|
+
.info-section {
|
|
59
|
+
display: flex;
|
|
60
|
+
justify-content: space-between;
|
|
61
|
+
margin-bottom: 30px;
|
|
62
|
+
}
|
|
63
|
+
.info-box {
|
|
64
|
+
flex: 1;
|
|
65
|
+
}
|
|
66
|
+
.info-box h3 {
|
|
67
|
+
color: #4a90e2;
|
|
68
|
+
border-bottom: 2px solid #e0e0e0;
|
|
69
|
+
padding-bottom: 5px;
|
|
70
|
+
}
|
|
71
|
+
table {
|
|
72
|
+
width: 100%;
|
|
73
|
+
border-collapse: collapse;
|
|
74
|
+
margin: 20px 0;
|
|
75
|
+
}
|
|
76
|
+
th {
|
|
77
|
+
background: #4a90e2;
|
|
78
|
+
color: white;
|
|
79
|
+
padding: 12px;
|
|
80
|
+
text-align: left;
|
|
81
|
+
}
|
|
82
|
+
td {
|
|
83
|
+
padding: 10px;
|
|
84
|
+
border-bottom: 1px solid #e0e0e0;
|
|
85
|
+
}
|
|
86
|
+
.total-section {
|
|
87
|
+
text-align: right;
|
|
88
|
+
margin-top: 20px;
|
|
89
|
+
}
|
|
90
|
+
.total {
|
|
91
|
+
font-size: 24px;
|
|
92
|
+
font-weight: bold;
|
|
93
|
+
color: #4a90e2;
|
|
94
|
+
}
|
|
95
|
+
.footer {
|
|
96
|
+
margin-top: 40px;
|
|
97
|
+
text-align: center;
|
|
98
|
+
color: #666;
|
|
99
|
+
font-size: 12px;
|
|
100
|
+
border-top: 1px solid #e0e0e0;
|
|
101
|
+
padding-top: 20px;
|
|
102
|
+
}
|
|
103
|
+
</style>
|
|
104
|
+
</head>
|
|
105
|
+
<body>
|
|
106
|
+
<div class="header">
|
|
107
|
+
<h1>INVOICE</h1>
|
|
108
|
+
<p>Invoice #{{invoice_number}}</p>
|
|
109
|
+
<p>Date: {{invoice_date}}</p>
|
|
110
|
+
</div>
|
|
111
|
+
|
|
112
|
+
<div class="info-section">
|
|
113
|
+
<div class="info-box">
|
|
114
|
+
<h3>From:</h3>
|
|
115
|
+
<p><strong>{{company_name}}</strong></p>
|
|
116
|
+
<p>{{company_address}}</p>
|
|
117
|
+
<p>{{company_email}}</p>
|
|
118
|
+
</div>
|
|
119
|
+
<div class="info-box">
|
|
120
|
+
<h3>Bill To:</h3>
|
|
121
|
+
<p><strong>{{customer_name}}</strong></p>
|
|
122
|
+
<p>{{customer_address}}</p>
|
|
123
|
+
<p>{{customer_email}}</p>
|
|
124
|
+
</div>
|
|
125
|
+
</div>
|
|
126
|
+
|
|
127
|
+
<table>
|
|
128
|
+
<thead>
|
|
129
|
+
<tr>
|
|
130
|
+
<th>Description</th>
|
|
131
|
+
<th>Quantity</th>
|
|
132
|
+
<th>Rate</th>
|
|
133
|
+
<th>Amount</th>
|
|
134
|
+
</tr>
|
|
135
|
+
</thead>
|
|
136
|
+
<tbody>
|
|
137
|
+
<tr>
|
|
138
|
+
<td>{{item_description}}</td>
|
|
139
|
+
<td>{{item_quantity}}</td>
|
|
140
|
+
<td>{{item_rate}}</td>
|
|
141
|
+
<td>{{item_amount}}</td>
|
|
142
|
+
</tr>
|
|
143
|
+
</tbody>
|
|
144
|
+
</table>
|
|
145
|
+
|
|
146
|
+
<div class="total-section">
|
|
147
|
+
<p><strong>Subtotal:</strong> {{subtotal}}</p>
|
|
148
|
+
<p><strong>Tax ({{tax_rate}}%):</strong> {{tax_amount}}</p>
|
|
149
|
+
<p class="total">Total: {{total_amount}}</p>
|
|
150
|
+
</div>
|
|
151
|
+
|
|
152
|
+
<div class="footer">
|
|
153
|
+
<p>{{footer_text}}</p>
|
|
154
|
+
<p>Payment due within 30 days</p>
|
|
155
|
+
</div>
|
|
156
|
+
</body>
|
|
157
|
+
</html>
|
|
158
|
+
`
|
|
159
|
+
});
|
|
160
|
+
console.log('โ
Template created: ', template.template_id);
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
// STEP 2: Generate PDF with document_id response
|
|
165
|
+
/*
|
|
166
|
+
console.log('๐ Step 2: Generating PDF (document_id response)...');
|
|
167
|
+
const doc1 = await client.documents.generate('template-940dedbf-30af-422f-8aff-7e8b05f0ad70', {
|
|
168
|
+
data: {
|
|
169
|
+
invoice_number: 'INV-2025-001',
|
|
170
|
+
date: new Date().toLocaleDateString(),
|
|
171
|
+
customer_name: 'John Doe',
|
|
172
|
+
customer_email: 'john.doe@example.com',
|
|
173
|
+
item_name: 'Product A',
|
|
174
|
+
quantity: 2,
|
|
175
|
+
price: '$50',
|
|
176
|
+
total: '$100'
|
|
177
|
+
},
|
|
178
|
+
response_type: 'document_id'
|
|
179
|
+
});
|
|
180
|
+
console.log('โ
PDF generated!');
|
|
181
|
+
console.log(' Document ID: ', doc1.document_id);
|
|
182
|
+
console.log(' Document URL: ', doc1.download_url);
|
|
183
|
+
*/
|
|
184
|
+
|
|
185
|
+
/*
|
|
186
|
+
console.log('๐ Step 2: Generating PDF (document_id response)...');
|
|
187
|
+
const doc1 = await client.documents.generate('template-5f7ef2fd-3d83-47f8-9fc9-e6820ad52adb', {
|
|
188
|
+
data: {
|
|
189
|
+
invoice_number: 'INV-2025-001',
|
|
190
|
+
invoice_date: new Date().toLocaleDateString(),
|
|
191
|
+
customer_name: 'Acme Corporation',
|
|
192
|
+
customer_email: 'billing@acme.com',
|
|
193
|
+
customer_address: '123 Business St, New York, NY 10001',
|
|
194
|
+
item_description: 'Web Development Services',
|
|
195
|
+
item_quantity: '40 hours',
|
|
196
|
+
item_rate: '$100/hour',
|
|
197
|
+
item_amount: '$4,000.00',
|
|
198
|
+
subtotal: '$4,000.00',
|
|
199
|
+
tax_rate: '10',
|
|
200
|
+
tax_amount: '$400.00',
|
|
201
|
+
total_amount: '$4,400.00',
|
|
202
|
+
notes: 'Payment due within 30 days',
|
|
203
|
+
company_name: 'Your Company Inc.',
|
|
204
|
+
company_address: '456 Company Ave, San Francisco, CA 94102',
|
|
205
|
+
company_email: 'info@yourcompany.com',
|
|
206
|
+
company_phone: '+1 (555) 123-4567'
|
|
207
|
+
},
|
|
208
|
+
response_type: 'document_id'
|
|
209
|
+
});
|
|
210
|
+
console.log('โ
PDF generated!');
|
|
211
|
+
console.log(' Document ID: ', doc1.document_id);
|
|
212
|
+
console.log(' Document URL: ', doc1.download_url);
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
/*
|
|
216
|
+
// Step 3: Generate PDF with base64 response
|
|
217
|
+
console.log('\n๐ Step 3: Generating PDF (base64 response)...');
|
|
218
|
+
const doc2 = await client.documents.generate(template.template_id, {
|
|
219
|
+
data: {
|
|
220
|
+
invoice_number: 'INV-2025-002',
|
|
221
|
+
date: new Date().toLocaleDateString(),
|
|
222
|
+
customer_name: 'Jane Smith',
|
|
223
|
+
customer_email: 'jane@example.com',
|
|
224
|
+
item_name: 'Consulting Services',
|
|
225
|
+
quantity: '20 hours',
|
|
226
|
+
price: '$150/hr',
|
|
227
|
+
total: '$3,000.00'
|
|
228
|
+
},
|
|
229
|
+
response_type: 'json_with_base64'
|
|
230
|
+
});
|
|
231
|
+
console.log('โ
PDF generated!');
|
|
232
|
+
console.log(' Document ID:', doc2.document_id);
|
|
233
|
+
console.log(' Filename:', doc2.filename);
|
|
234
|
+
console.log(' Size:', doc2.size_bytes, 'bytes');
|
|
235
|
+
console.log(' Base64 length:', doc2.pdf_content.length, 'characters');
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
// Step 4: Download the PDF
|
|
239
|
+
|
|
240
|
+
console.log('\n ๐ฅ Step 4: Downloading PDF...');
|
|
241
|
+
const outputPath = path.join(__dirname, '../output', `invoice-${'document-004e98bf-76a7-4548-934e-d95912f7ead9'}.pdf`);
|
|
242
|
+
|
|
243
|
+
// Create output directory if it doesn't exist
|
|
244
|
+
const outputDir = path.dirname(outputPath);
|
|
245
|
+
if (!fs.existsSync(outputDir)) {
|
|
246
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const pdfBuffer = await client.documents.download('document-004e98bf-76a7-4548-934e-d95912f7ead9', outputPath);
|
|
250
|
+
console.log('โ
PDF downloaded!');
|
|
251
|
+
console.log(' Saved to:', outputPath);
|
|
252
|
+
console.log(' Size:', pdfBuffer.length, 'bytes');
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
// Step 5: Get document details
|
|
256
|
+
/*
|
|
257
|
+
console.log('\n๐ Step 5: Getting document details...');
|
|
258
|
+
const docDetails = await client.documents.get(doc1.document_id);
|
|
259
|
+
console.log('โ
Document details retrieved!');
|
|
260
|
+
console.log(' Template ID:', docDetails.template_id);
|
|
261
|
+
console.log(' Created:', docDetails.created_at);
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
// Step 6: List all documents
|
|
265
|
+
/*
|
|
266
|
+
console.log('\n๐ Step 6: Listing all documents...');
|
|
267
|
+
const allDocs = await client.documents.list();
|
|
268
|
+
console.log('โ
Found', allDocs.length, 'documents');
|
|
269
|
+
*/
|
|
270
|
+
|
|
271
|
+
// Clean up
|
|
272
|
+
/*
|
|
273
|
+
console.log('\n๐งน Cleaning up...');
|
|
274
|
+
await client.documents.delete(doc1.document_id);
|
|
275
|
+
await client.documents.delete(doc2.document_id);
|
|
276
|
+
await client.templates.delete(template.template_id);
|
|
277
|
+
console.log('โ
Test data cleaned up');
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
// Example 1: Simple PDF generation with document_id response
|
|
281
|
+
/*
|
|
282
|
+
console.log('๐ Example 1: Generate PDF (get download URL)...\n');
|
|
283
|
+
const invoice1 = await client.documents.generate(templateId, {
|
|
284
|
+
data: {
|
|
285
|
+
invoice_number: 'INV-2025-001',
|
|
286
|
+
invoice_date: new Date().toLocaleDateString(),
|
|
287
|
+
customer_name: 'Acme Corporation',
|
|
288
|
+
customer_email: 'billing@acme.com',
|
|
289
|
+
customer_address: '123 Business St, New York, NY 10001',
|
|
290
|
+
item_description: 'Web Development Services',
|
|
291
|
+
item_quantity: '40 hours',
|
|
292
|
+
item_rate: '$100/hour',
|
|
293
|
+
item_amount: '$4,000.00',
|
|
294
|
+
subtotal: '$4,000.00',
|
|
295
|
+
tax_rate: '10',
|
|
296
|
+
tax_amount: '$400.00',
|
|
297
|
+
total_amount: '$4,400.00',
|
|
298
|
+
notes: 'Payment due within 30 days',
|
|
299
|
+
company_name: 'Your Company Inc.',
|
|
300
|
+
company_address: '456 Company Ave, San Francisco, CA 94102',
|
|
301
|
+
company_email: 'info@yourcompany.com',
|
|
302
|
+
company_phone: '+1 (555) 123-4567'
|
|
303
|
+
},
|
|
304
|
+
response_type: 'document_id'
|
|
305
|
+
});
|
|
306
|
+
console.log('โ
PDF Generated!');
|
|
307
|
+
const outputPath2 = path.join(__dirname, '../output', `invoice-${invoice1.document_id}.pdf`);
|
|
308
|
+
const pdfBuffer2 = await client.documents.download(invoice1.document_id, outputPath2);
|
|
309
|
+
console.log('โ
PDF downloaded!');
|
|
310
|
+
console.log(' Saved to:', outputPath2);
|
|
311
|
+
console.log(' Size:', pdfBuffer2.length, 'bytes');
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
console.log('\n' + '='.repeat(50));
|
|
315
|
+
console.log('๐ All PDF generation examples completed successfully!\n');
|
|
316
|
+
|
|
317
|
+
} catch (error) {
|
|
318
|
+
console.error('\nโ Error:', error.message);
|
|
319
|
+
if (error.statusCode) {
|
|
320
|
+
console.error('Status Code:', error.statusCode);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
generatePdfExample();
|