11import stream from "stream" ;
22import * as core from "../src/core" ;
33import { FileForgeClient } from "../src" ;
4- import { generate_from_html } from "../src/Helper" ;
4+ import { generate_from_html , ResponseStream , ResponseURL } from "../src/Helper" ;
55import * as error from "../src/errors/index" ;
66import fs from "fs" ;
77import { writeFile } from "fs/promises" ;
@@ -52,18 +52,12 @@ describe("test", () => {
5252 {
5353 options : { }
5454 }
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+
6761 } , 10_000_000 ) ;
6862
6963
@@ -88,11 +82,11 @@ describe("test", () => {
8882 host : true ,
8983 }
9084 }
91- ) ;
85+ ) as ResponseURL ;
9286
9387 expect ( pdf . url ) . not . toBeNull ( ) ;
9488
95- } , 10_000_000 ) ;
89+ } , 10_000_000 ) ;
9690
9791 it ( "should fail because of invalid api key" , async ( ) => {
9892 const htmlBlob = new Blob ( [ HTML ] , {
@@ -150,18 +144,11 @@ describe("test", () => {
150144 test :false
151145 }
152146
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+
165152 } , 10_000_000 ) ;
166153
167154 it ( "should generate a PDF url from helper" , async ( ) => {
@@ -186,7 +173,7 @@ describe("test", () => {
186173 host :true ,
187174 }
188175
189- ) ;
176+ ) as ResponseURL ;
190177
191178 expect ( pdf . url ) . not . toBeNull ( ) ;
192179} , 10_000_000 ) ;
@@ -215,16 +202,39 @@ describe("test", () => {
215202 }
216203 ) ;
217204
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+ } ;
219227
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+ }
226237
227- await writeFile ( "output_merged.pdf" , bufferString ) ;
228238} , 10_000_000 ) ;
229239
230240} ) ;
0 commit comments