@apiverve/pdftoimage 0.0.0-stage → 1.2.2
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/LICENSE +21 -0
- package/README.md +221 -2
- package/index.d.ts +53 -0
- package/index.js +325 -0
- package/package.json +44 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 APIVerve and EvlarSoft LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,222 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PDF to Image API
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
PDF to Image renders the pages you choose from a PDF, uploaded or fetched from a URL, as PNG or JPG images and returns a download link for each, valid for 2 days, with its pixel size. The Free plan renders one page per call at 150 dpi; paid plans render up to 10 pages per call and can set the resolution from 72 to 300 dpi.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@apiverve/pdftoimage)
|
|
6
|
+
|
|
7
|
+
This is a JavaScript wrapper for the [PDF to Image API](https://apiverve.com/marketplace/pdftoimage?utm_source=npm&utm_medium=referral&utm_content=readme)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Using npm:
|
|
14
|
+
```shell
|
|
15
|
+
npm install @apiverve/pdftoimage
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Using yarn:
|
|
19
|
+
```shell
|
|
20
|
+
yarn add @apiverve/pdftoimage
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Configuration
|
|
26
|
+
|
|
27
|
+
Before using the PDF to Image API client, you have to setup your account and obtain your API Key.
|
|
28
|
+
You can get it by signing up at [https://apiverve.com](https://apiverve.com?utm_source=npm&utm_medium=referral&utm_content=readme)
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
[Get started with the Quick Start Guide](https://docs.apiverve.com/quickstart?utm_source=npm&utm_medium=referral&utm_content=readme)
|
|
35
|
+
|
|
36
|
+
The PDF to Image API documentation is found here: [https://docs.apiverve.com/ref/pdftoimage](https://docs.apiverve.com/ref/pdftoimage?utm_source=npm&utm_medium=referral&utm_content=readme).
|
|
37
|
+
You can find parameters, example responses, and status codes documented here.
|
|
38
|
+
|
|
39
|
+
### Setup
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
const pdftoimageAPI = require('@apiverve/pdftoimage');
|
|
43
|
+
const api = new pdftoimageAPI({
|
|
44
|
+
api_key: '[YOUR_API_KEY]'
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### File Upload
|
|
53
|
+
|
|
54
|
+
This API requires a file upload. Use `executeWithFile()` to upload files:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
// Upload a file (path, Buffer, or ReadStream)
|
|
58
|
+
api.executeWithFile('/path/to/file.pdf', options, callback);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Accepted file types:** .pdf
|
|
62
|
+
**Maximum file size:** 10 MB
|
|
63
|
+
|
|
64
|
+
### Available Parameters
|
|
65
|
+
|
|
66
|
+
Pass additional parameters via `options.fields`:
|
|
67
|
+
|
|
68
|
+
| Parameter | Type | Description |
|
|
69
|
+
|-----------|------|-------------|
|
|
70
|
+
| `pages` | string | Pages to render, as numbers and ranges such as 1-3,5; at most 10 per call on paid plans, 1 on the Free plan |
|
|
71
|
+
| `format` | string | Image format |
|
|
72
|
+
| `quality` | integer | JPG quality from 1 to 100; ignored for PNG |
|
|
73
|
+
| `dpi` | integer | Resolution in dots per inch, from 72 to 300 |
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
api.executeWithFile('/path/to/file', {
|
|
77
|
+
fields: {
|
|
78
|
+
pages: '1',
|
|
79
|
+
format: 'png',
|
|
80
|
+
quality: 90,
|
|
81
|
+
dpi: 150
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### Perform Request
|
|
89
|
+
|
|
90
|
+
Using the API is simple. All you have to do is make a request. The API will return a response with the data you requested.
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
// This API requires a file upload
|
|
94
|
+
// You can pass a file path, Buffer, or ReadStream
|
|
95
|
+
var filePath = '/path/to/pdf.pdf';
|
|
96
|
+
|
|
97
|
+
api.executeWithFile(filePath,
|
|
98
|
+
{
|
|
99
|
+
fields: {
|
|
100
|
+
pages: '1',
|
|
101
|
+
format: 'png',
|
|
102
|
+
quality: 90,
|
|
103
|
+
dpi: 150
|
|
104
|
+
}
|
|
105
|
+
}, function (error, data) {
|
|
106
|
+
if (error) {
|
|
107
|
+
return console.error(error);
|
|
108
|
+
} else {
|
|
109
|
+
console.log(data);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### Using Promises
|
|
117
|
+
|
|
118
|
+
You can also use promises to make requests. The API returns a promise that you can use to handle the response.
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
// This API requires a file upload
|
|
122
|
+
// You can pass a file path, Buffer, or ReadStream
|
|
123
|
+
var filePath = '/path/to/pdf.pdf';
|
|
124
|
+
|
|
125
|
+
api.executeWithFile(filePath, { fields: { /* parameters */ } })
|
|
126
|
+
.then(data => {
|
|
127
|
+
console.log(data);
|
|
128
|
+
})
|
|
129
|
+
.catch(error => {
|
|
130
|
+
console.error(error);
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
### Using Async/Await
|
|
137
|
+
|
|
138
|
+
You can also use async/await to make requests. The API returns a promise that you can use to handle the response.
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
async function makeRequest() {
|
|
142
|
+
// This API requires a file upload
|
|
143
|
+
// You can pass a file path, Buffer, or ReadStream
|
|
144
|
+
var filePath = '/path/to/pdf.pdf';
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const data = await api.executeWithFile(filePath, { fields: { /* parameters */ } });
|
|
148
|
+
console.log(data);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
console.error(error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
## Example Response
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
{
|
|
161
|
+
"status": "ok",
|
|
162
|
+
"error": null,
|
|
163
|
+
"data": {
|
|
164
|
+
"pageCount": 14,
|
|
165
|
+
"format": "png",
|
|
166
|
+
"dpi": 150,
|
|
167
|
+
"imageCount": 2,
|
|
168
|
+
"expires": 1792400000000,
|
|
169
|
+
"images": [
|
|
170
|
+
{
|
|
171
|
+
"page": 1,
|
|
172
|
+
"imageName": "3b7e9d21-4c5a-4f80-a1d6-9e2c8b7f5a34-1.png",
|
|
173
|
+
"width": 1275,
|
|
174
|
+
"height": 1650,
|
|
175
|
+
"dpi": 150,
|
|
176
|
+
"size": 214533,
|
|
177
|
+
"downloadURL": "https://storage.googleapis.com/apiverve/APIData/pdftoimage/3b7e9d21-4c5a-4f80-a1d6-9e2c8b7f5a34-1.png?GoogleAccessId=635500398038-compute%40developer.gserviceaccount.com&Expires=1792400000&Signature=SAMPLE"
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"page": 2,
|
|
181
|
+
"imageName": "3b7e9d21-4c5a-4f80-a1d6-9e2c8b7f5a34-2.png",
|
|
182
|
+
"width": 1275,
|
|
183
|
+
"height": 1650,
|
|
184
|
+
"dpi": 150,
|
|
185
|
+
"size": 198207,
|
|
186
|
+
"downloadURL": "https://storage.googleapis.com/apiverve/APIData/pdftoimage/3b7e9d21-4c5a-4f80-a1d6-9e2c8b7f5a34-2.png?GoogleAccessId=635500398038-compute%40developer.gserviceaccount.com&Expires=1792400000&Signature=SAMPLE"
|
|
187
|
+
}
|
|
188
|
+
]
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Customer Support
|
|
196
|
+
|
|
197
|
+
Need any assistance? [Get in touch with Customer Support](https://apiverve.com/contact?utm_source=npm&utm_medium=referral&utm_content=readme).
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
## Updates
|
|
202
|
+
|
|
203
|
+
Stay up to date by following [@apiverveHQ](https://twitter.com/apiverveHQ) on Twitter.
|
|
204
|
+
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
## Legal
|
|
208
|
+
|
|
209
|
+
All usage of the APIVerve website, API, and services is subject to the [APIVerve Terms of Service](https://apiverve.com/terms?utm_source=npm&utm_medium=referral&utm_content=readme) and [Privacy Policy](https://apiverve.com/privacy?utm_source=npm&utm_medium=referral&utm_content=readme).
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## License
|
|
214
|
+
Licensed under the MIT License (MIT)
|
|
215
|
+
|
|
216
|
+
Copyright (©) 2026 APIVerve, and EvlarSoft LLC
|
|
217
|
+
|
|
218
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
219
|
+
|
|
220
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
221
|
+
|
|
222
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
declare module '@apiverve/pdftoimage' {
|
|
2
|
+
export interface pdftoimageOptions {
|
|
3
|
+
api_key: string;
|
|
4
|
+
secure?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Describes fields the current plan does not unlock. Locked fields arrive as null
|
|
9
|
+
* in `data`; `locked_fields` names them, using dot paths for nested fields.
|
|
10
|
+
* Absent when the plan unlocks everything.
|
|
11
|
+
*/
|
|
12
|
+
export interface PremiumInfo {
|
|
13
|
+
message: string;
|
|
14
|
+
upgrade_url: string;
|
|
15
|
+
locked_fields: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface pdftoimageResponse {
|
|
19
|
+
status: string;
|
|
20
|
+
error: string | null;
|
|
21
|
+
data: PDFtoImageData;
|
|
22
|
+
code?: number;
|
|
23
|
+
premium?: PremiumInfo;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
interface PDFtoImageData {
|
|
28
|
+
pageCount: number | null;
|
|
29
|
+
format: null | string;
|
|
30
|
+
dpi: number | null;
|
|
31
|
+
imageCount: number | null;
|
|
32
|
+
expires: number | null;
|
|
33
|
+
images: Image[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface Image {
|
|
37
|
+
page: number | null;
|
|
38
|
+
imageName: null | string;
|
|
39
|
+
width: number | null;
|
|
40
|
+
height: number | null;
|
|
41
|
+
dpi: number | null;
|
|
42
|
+
size: number | null;
|
|
43
|
+
downloadURL: null | string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default class pdftoimageWrapper {
|
|
47
|
+
constructor(options: pdftoimageOptions);
|
|
48
|
+
|
|
49
|
+
execute(callback: (error: any, data: pdftoimageResponse | null) => void): Promise<pdftoimageResponse>;
|
|
50
|
+
execute(query: Record<string, any>, callback: (error: any, data: pdftoimageResponse | null) => void): Promise<pdftoimageResponse>;
|
|
51
|
+
execute(query?: Record<string, any>): Promise<pdftoimageResponse>;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
|
|
3
|
+
class pdftoimageWrapper {
|
|
4
|
+
|
|
5
|
+
constructor(options) {
|
|
6
|
+
if (!options || typeof options !== 'object') {
|
|
7
|
+
throw new Error('Options object must be provided. See documentation: https://docs.apiverve.com/ref/pdftoimage');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { api_key, secure = true } = options;
|
|
11
|
+
|
|
12
|
+
if (!api_key || typeof api_key !== 'string') {
|
|
13
|
+
throw new Error('API key must be provided as a non-empty string. Get your API key at: https://apiverve.com');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Validate API key format (GUID, prefixed keys like apv_xxx, or alphanumeric)
|
|
17
|
+
const apiKeyPattern = /^[a-zA-Z0-9_-]+$/;
|
|
18
|
+
if (!apiKeyPattern.test(api_key)) {
|
|
19
|
+
throw new Error('Invalid API key format. API key must be alphanumeric and may contain hyphens and underscores. Get your API key at: https://apiverve.com');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (typeof secure !== 'boolean') {
|
|
23
|
+
throw new Error('Secure parameter must be a boolean value.');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this.APIKey = api_key;
|
|
27
|
+
this.IsSecure = secure;
|
|
28
|
+
|
|
29
|
+
// secure is deprecated, all requests must be made over HTTPS
|
|
30
|
+
this.baseURL = 'https://api.apiverve.com/v1/pdftoimage';
|
|
31
|
+
|
|
32
|
+
// Validation rules for parameters (generated from schema)
|
|
33
|
+
this.validationRules = {"pdf":{"type":"string","required":false},"pages":{"type":"string","required":false,"maxLength":100,"default":"1"},"format":{"type":"string","required":false,"default":"png"},"quality":{"type":"integer","required":false,"min":1,"max":100,"default":90},"dpi":{"type":"integer","required":false,"min":72,"max":300,"default":150}};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Validate query parameters against schema rules
|
|
38
|
+
* @param {Object} query - The query parameters to validate
|
|
39
|
+
* @throws {Error} - If validation fails
|
|
40
|
+
*/
|
|
41
|
+
validateParams(query) {
|
|
42
|
+
const errors = [];
|
|
43
|
+
|
|
44
|
+
for (const [paramName, rules] of Object.entries(this.validationRules)) {
|
|
45
|
+
const value = query[paramName];
|
|
46
|
+
|
|
47
|
+
// Check required
|
|
48
|
+
if (rules.required && (value === undefined || value === null || value === '')) {
|
|
49
|
+
errors.push(`Required parameter [${paramName}] is missing.`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Skip validation if value is not provided and not required
|
|
54
|
+
if (value === undefined || value === null) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Type validation
|
|
59
|
+
if (rules.type === 'integer' || rules.type === 'number') {
|
|
60
|
+
const numValue = Number(value);
|
|
61
|
+
if (isNaN(numValue)) {
|
|
62
|
+
errors.push(`Parameter [${paramName}] must be a valid ${rules.type}.`);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (rules.type === 'integer' && !Number.isInteger(numValue)) {
|
|
67
|
+
errors.push(`Parameter [${paramName}] must be an integer.`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Min/max validation for numbers
|
|
72
|
+
if (rules.min !== undefined && numValue < rules.min) {
|
|
73
|
+
errors.push(`Parameter [${paramName}] must be at least ${rules.min}.`);
|
|
74
|
+
}
|
|
75
|
+
if (rules.max !== undefined && numValue > rules.max) {
|
|
76
|
+
errors.push(`Parameter [${paramName}] must be at most ${rules.max}.`);
|
|
77
|
+
}
|
|
78
|
+
} else if (rules.type === 'string') {
|
|
79
|
+
if (typeof value !== 'string') {
|
|
80
|
+
errors.push(`Parameter [${paramName}] must be a string.`);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Length validation for strings
|
|
85
|
+
if (rules.minLength !== undefined && value.length < rules.minLength) {
|
|
86
|
+
errors.push(`Parameter [${paramName}] must be at least ${rules.minLength} characters.`);
|
|
87
|
+
}
|
|
88
|
+
if (rules.maxLength !== undefined && value.length > rules.maxLength) {
|
|
89
|
+
errors.push(`Parameter [${paramName}] must be at most ${rules.maxLength} characters.`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Format validation
|
|
93
|
+
if (rules.format) {
|
|
94
|
+
const formatPatterns = {
|
|
95
|
+
'email': /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
96
|
+
'url': /^https?:\/\/.+/i,
|
|
97
|
+
'ip': /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
|
|
98
|
+
'date': /^\d{4}-\d{2}-\d{2}$/,
|
|
99
|
+
'hexColor': /^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (formatPatterns[rules.format] && !formatPatterns[rules.format].test(value)) {
|
|
103
|
+
errors.push(`Parameter [${paramName}] must be a valid ${rules.format}.`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} else if (rules.type === 'boolean') {
|
|
107
|
+
if (typeof value !== 'boolean' && value !== 'true' && value !== 'false') {
|
|
108
|
+
errors.push(`Parameter [${paramName}] must be a boolean.`);
|
|
109
|
+
}
|
|
110
|
+
} else if (rules.type === 'array') {
|
|
111
|
+
if (!Array.isArray(value)) {
|
|
112
|
+
errors.push(`Parameter [${paramName}] must be an array.`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Enum validation
|
|
117
|
+
if (rules.enum && Array.isArray(rules.enum)) {
|
|
118
|
+
if (!rules.enum.includes(value)) {
|
|
119
|
+
errors.push(`Parameter [${paramName}] must be one of: ${rules.enum.join(', ')}.`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (errors.length > 0) {
|
|
125
|
+
throw new Error(`Validation failed: ${errors.join(' ')} See documentation: https://docs.apiverve.com/ref/pdftoimage`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async execute(query, callback) {
|
|
130
|
+
// Handle different argument patterns
|
|
131
|
+
if(arguments.length === 0) {
|
|
132
|
+
// execute() - no args
|
|
133
|
+
query = {};
|
|
134
|
+
callback = null;
|
|
135
|
+
} else if(arguments.length === 1) {
|
|
136
|
+
if (typeof query === 'function') {
|
|
137
|
+
// execute(callback)
|
|
138
|
+
callback = query;
|
|
139
|
+
query = {};
|
|
140
|
+
} else {
|
|
141
|
+
// execute(query)
|
|
142
|
+
callback = null;
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
145
|
+
// execute(query, callback)
|
|
146
|
+
if (!query || typeof query !== 'object') {
|
|
147
|
+
throw new Error('Query parameters must be provided as an object.');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Validate parameters against schema rules
|
|
152
|
+
this.validateParams(query);
|
|
153
|
+
|
|
154
|
+
const method = 'POST';
|
|
155
|
+
const url = method === 'POST' ? this.baseURL : this.constructURL(query);
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
const response = await axios({
|
|
159
|
+
method,
|
|
160
|
+
url,
|
|
161
|
+
headers: {
|
|
162
|
+
'Content-Type': 'application/json',
|
|
163
|
+
'x-api-key': this.APIKey,
|
|
164
|
+
'auth-mode': 'npm-package'
|
|
165
|
+
},
|
|
166
|
+
data: method === 'POST' ? query : undefined
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const data = response.data;
|
|
170
|
+
if (callback) callback(null, data);
|
|
171
|
+
return data;
|
|
172
|
+
} catch (error) {
|
|
173
|
+
let apiError;
|
|
174
|
+
|
|
175
|
+
if (error.response && error.response.data) {
|
|
176
|
+
apiError = error.response.data;
|
|
177
|
+
} else if (error.message) {
|
|
178
|
+
apiError = { error: error.message, status: 'error' };
|
|
179
|
+
} else {
|
|
180
|
+
apiError = { error: 'An unknown error occurred', status: 'error' };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (callback) {
|
|
184
|
+
callback(apiError, null);
|
|
185
|
+
return; // Don't throw if callback is provided
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
throw apiError;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
constructURL(query) {
|
|
193
|
+
let url = this.baseURL;
|
|
194
|
+
|
|
195
|
+
if(query && typeof query === 'object')
|
|
196
|
+
{
|
|
197
|
+
if (Object.keys(query).length > 0) {
|
|
198
|
+
const queryString = Object.keys(query)
|
|
199
|
+
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`)
|
|
200
|
+
.join('&');
|
|
201
|
+
url += `?${queryString}`;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return url;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Execute the API request with a file upload
|
|
209
|
+
* @param {string|Buffer|ReadStream} file - File path, Buffer, or ReadStream
|
|
210
|
+
* @param {Object} options - Additional options (e.g., additional form fields)
|
|
211
|
+
* @param {Function} callback - Optional callback function
|
|
212
|
+
* @returns {Promise} - Promise with the API response
|
|
213
|
+
*/
|
|
214
|
+
async executeWithFile(file, options = {}, callback) {
|
|
215
|
+
// Handle callback as second argument
|
|
216
|
+
if (typeof options === 'function') {
|
|
217
|
+
callback = options;
|
|
218
|
+
options = {};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!file) {
|
|
222
|
+
throw new Error('File must be provided for file upload. See documentation: https://docs.apiverve.com/ref/pdftoimage');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const FormData = require('form-data');
|
|
226
|
+
const fs = require('fs');
|
|
227
|
+
const path = require('path');
|
|
228
|
+
|
|
229
|
+
const formData = new FormData();
|
|
230
|
+
|
|
231
|
+
// Handle different file input types
|
|
232
|
+
if (typeof file === 'string') {
|
|
233
|
+
// File path provided
|
|
234
|
+
if (!fs.existsSync(file)) {
|
|
235
|
+
throw new Error(`File not found: ${file}`);
|
|
236
|
+
}
|
|
237
|
+
formData.append('pdf', fs.createReadStream(file), {
|
|
238
|
+
filename: path.basename(file)
|
|
239
|
+
});
|
|
240
|
+
} else if (Buffer.isBuffer(file)) {
|
|
241
|
+
// Buffer provided
|
|
242
|
+
formData.append('pdf', file, {
|
|
243
|
+
filename: options.filename || 'upload',
|
|
244
|
+
contentType: options.contentType || 'application/octet-stream'
|
|
245
|
+
});
|
|
246
|
+
} else if (file.pipe && typeof file.pipe === 'function') {
|
|
247
|
+
// ReadStream provided
|
|
248
|
+
formData.append('pdf', file);
|
|
249
|
+
} else {
|
|
250
|
+
throw new Error('File must be a file path (string), Buffer, or ReadStream. See documentation: https://docs.apiverve.com/ref/pdftoimage');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Add any additional form fields
|
|
254
|
+
if (options.fields && typeof options.fields === 'object') {
|
|
255
|
+
for (const [key, value] of Object.entries(options.fields)) {
|
|
256
|
+
formData.append(key, value);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
const response = await axios({
|
|
262
|
+
method: 'POST',
|
|
263
|
+
url: this.baseURL,
|
|
264
|
+
headers: {
|
|
265
|
+
...formData.getHeaders(),
|
|
266
|
+
'x-api-key': this.APIKey,
|
|
267
|
+
'auth-mode': 'npm-package'
|
|
268
|
+
},
|
|
269
|
+
data: formData,
|
|
270
|
+
maxContentLength: 10 * 1024 * 1024,
|
|
271
|
+
maxBodyLength: 10 * 1024 * 1024
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const data = response.data;
|
|
275
|
+
if (callback) callback(null, data);
|
|
276
|
+
return data;
|
|
277
|
+
} catch (error) {
|
|
278
|
+
let apiError;
|
|
279
|
+
|
|
280
|
+
if (error.response && error.response.data) {
|
|
281
|
+
apiError = error.response.data;
|
|
282
|
+
} else if (error.message) {
|
|
283
|
+
apiError = { error: error.message, status: 'error' };
|
|
284
|
+
} else {
|
|
285
|
+
apiError = { error: 'An unknown error occurred', status: 'error' };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (callback) {
|
|
289
|
+
callback(apiError, null);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
throw apiError;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Execute the API request with a file URL
|
|
299
|
+
* @param {string} url - URL of the file to process
|
|
300
|
+
* @param {Object} options - Additional options (e.g., additional parameters)
|
|
301
|
+
* @param {Function} callback - Optional callback function
|
|
302
|
+
* @returns {Promise} - Promise with the API response
|
|
303
|
+
*/
|
|
304
|
+
async executeWithUrl(url, options = {}, callback) {
|
|
305
|
+
// Handle callback as second argument
|
|
306
|
+
if (typeof options === 'function') {
|
|
307
|
+
callback = options;
|
|
308
|
+
options = {};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (!url || typeof url !== 'string') {
|
|
312
|
+
throw new Error('URL must be provided as a string. See documentation: https://docs.apiverve.com/ref/pdftoimage');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Validate URL format
|
|
316
|
+
if (!/^https?:\/\/.+/i.test(url)) {
|
|
317
|
+
throw new Error('Invalid URL format. URL must start with http:// or https://');
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const query = { url, ...options };
|
|
321
|
+
return this.execute(query, callback);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
module.exports = pdftoimageWrapper;
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apiverve/pdftoimage",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.2.2",
|
|
4
|
+
"description": "PDF to Image renders the pages you choose from a PDF, uploaded or fetched from a URL, as PNG or JPG images and returns a download link for each, valid for 2 days, with its pixel size. The Free plan renders one page per call at 150 dpi; paid plans render up to 10 pages per call and can set the resolution from 72 to 300 dpi.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18.0.0"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"index.d.ts",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "mocha",
|
|
18
|
+
"example": "node examples/basic.js"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/apiverve/pdftoimage-api.git",
|
|
23
|
+
"directory": "npm"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"pdf to image api", "pdf to png api", "pdf to jpg api", "convert pdf to image", "render pdf pages api"
|
|
27
|
+
],
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "APIVerve",
|
|
30
|
+
"email": "hello@apiverve.com",
|
|
31
|
+
"url": "https://apiverve.com"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/apiverve/pdftoimage-api/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://apiverve.com/marketplace/pdftoimage?utm_source=npm&utm_medium=referral&utm_content=homepage",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"mocha": "^11.0.1",
|
|
40
|
+
"chai": "^5.1.2",
|
|
41
|
+
"dotenv": "^16.4.7"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"axios": "1.13.2",
|
|
45
|
+
"form-data": "^4.0.1"
|
|
46
|
+
}
|
|
6
47
|
}
|