1
1
import stream from "stream" ;
2
2
import * as core from "../src/core" ;
3
3
import { FileForgeClient } from "../src" ;
4
- import { generate_from_html } from "../src/Helper" ;
4
+ import { generate_from_html , ResponseStream , ResponseURL } from "../src/Helper" ;
5
5
import * as error from "../src/errors/index" ;
6
6
import fs from "fs" ;
7
7
import { writeFile } from "fs/promises" ;
@@ -52,18 +52,12 @@ describe("test", () => {
52
52
{
53
53
options : { }
54
54
}
55
- ) ;
56
-
57
- const chunks : any [ ] = [ ] ;
58
-
59
- for await ( let chunk of pdf ) {
60
- chunks . push ( chunk ) ;
61
- }
62
-
63
- const buffer : Buffer = Buffer . concat ( chunks ) ;
64
- const bufferString = buffer . toString ( )
65
-
66
- await writeFile ( "output.pdf" , bufferString ) ;
55
+ ) as ResponseStream ;
56
+
57
+ // Write the PDF stream to a file
58
+ const writeStream = fs . createWriteStream ( 'output.pdf' ) ;
59
+ pdf . file . pipe ( writeStream ) ;
60
+
67
61
} , 10_000_000 ) ;
68
62
69
63
@@ -88,11 +82,11 @@ describe("test", () => {
88
82
host : true ,
89
83
}
90
84
}
91
- ) ;
85
+ ) as ResponseURL ;
92
86
93
87
expect ( pdf . url ) . not . toBeNull ( ) ;
94
88
95
- } , 10_000_000 ) ;
89
+ } , 10_000_000 ) ;
96
90
97
91
it ( "should fail because of invalid api key" , async ( ) => {
98
92
const htmlBlob = new Blob ( [ HTML ] , {
@@ -150,18 +144,11 @@ describe("test", () => {
150
144
test :false
151
145
}
152
146
153
- ) ;
154
- const chunks : any [ ] = [ ] ;
155
-
156
- for await ( let chunk of pdf ) {
157
- chunks . push ( chunk ) ;
158
- }
159
-
160
- const buffer : Buffer = Buffer . concat ( chunks ) ;
161
- const bufferString = buffer . toString ( )
162
-
163
-
164
- await writeFile ( "output_helper.pdf" , bufferString ) ;
147
+ ) as ResponseStream ;
148
+ // Write the PDF stream to a file
149
+ const writeStream = fs . createWriteStream ( 'output_helper.pdf' ) ;
150
+ pdf . file . pipe ( writeStream ) ;
151
+
165
152
} , 10_000_000 ) ;
166
153
167
154
it ( "should generate a PDF url from helper" , async ( ) => {
@@ -186,7 +173,7 @@ describe("test", () => {
186
173
host :true ,
187
174
}
188
175
189
- ) ;
176
+ ) as ResponseURL ;
190
177
191
178
expect ( pdf . url ) . not . toBeNull ( ) ;
192
179
} , 10_000_000 ) ;
@@ -215,16 +202,39 @@ describe("test", () => {
215
202
}
216
203
) ;
217
204
218
- const chunks : any [ ] = [ ] ;
205
+ // Write the PDF stream to a file
206
+ const writeStream = fs . createWriteStream ( 'output_merged.pdf' ) ;
207
+ pdf . file . pipe ( writeStream ) ;
208
+
209
+ } , 10_000_000 ) ;
210
+
211
+ it ( "should generate from html snippet" , async ( ) => {
212
+ try {
213
+
214
+ const client = new FileForgeClient ( {
215
+ apiKey : FILEFORGE_API_KEY
216
+ } ) ;
217
+ const documentInput = {
218
+ html : HTML ,
219
+ fileName : 'example' ,
220
+ test : false ,
221
+ host : false ,
222
+ expiresAt : new Date ( Date . now ( ) + 48 * 60 * 60 * 1000 ) ,
223
+ files : [
224
+ { path : '/style.css' , content : CSS } ,
225
+ ] ,
226
+ } ;
219
227
220
- for await ( let chunk of pdf ) {
221
- chunks . push ( chunk ) ;
222
- }
223
-
224
- const buffer : Buffer = Buffer . concat ( chunks ) ;
225
- const bufferString = buffer . toString ( )
228
+ const response = await generate_from_html ( client , documentInput ) as ResponseStream ;
229
+
230
+ // Write the PDF stream to a file
231
+ const writeStream = fs . createWriteStream ( 'outputSnippet.pdf' ) ;
232
+ response . file . pipe ( writeStream ) ;
233
+ console . log ( 'PDF generated successfully.' ) ;
234
+ } catch ( error ) {
235
+ console . error ( 'Error generating PDF:' , error ) ;
236
+ }
226
237
227
- await writeFile ( "output_merged.pdf" , bufferString ) ;
228
238
} , 10_000_000 ) ;
229
239
230
240
} ) ;
0 commit comments