diff --git a/output.pdf b/output.pdf index 0cebb36..d08774e 100644 Binary files a/output.pdf and b/output.pdf differ diff --git a/outputSnippet.pdf b/outputSnippet.pdf new file mode 100644 index 0000000..f374fa9 Binary files /dev/null and b/outputSnippet.pdf differ diff --git a/output_helper.pdf b/output_helper.pdf index e881dac..275d039 100644 Binary files a/output_helper.pdf and b/output_helper.pdf differ diff --git a/output_merged.pdf b/output_merged.pdf index 1dff900..e54fd68 100644 Binary files a/output_merged.pdf and b/output_merged.pdf differ diff --git a/src/Client.ts b/src/Client.ts index fd79300..3e6133d 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -109,7 +109,7 @@ export class FileForgeClient { files: File[] | fs.ReadStream[], request: FileForge.MergeRequest, requestOptions?: FileForgeClient.RequestOptions - ): Promise { + ): Promise { const _request = core.newFormData(); const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, { unrecognizedObjectKeys: "passthrough", diff --git a/src/core/fetcher/Fetcher.ts b/src/core/fetcher/Fetcher.ts index 64d75e6..8e17594 100644 --- a/src/core/fetcher/Fetcher.ts +++ b/src/core/fetcher/Fetcher.ts @@ -148,25 +148,21 @@ async function fetcherImpl(args: Fetcher.Args): Promise{ +):Promise{ const files: AssetOrPathBuffer[] = document.files ?? []; files.push({ path: "/index.html", content: document.html }); diff --git a/tests/custom.test.ts b/tests/custom.test.ts index ac48ea8..4f6e732 100644 --- a/tests/custom.test.ts +++ b/tests/custom.test.ts @@ -1,7 +1,7 @@ import stream from "stream"; import * as core from "../src/core"; import { FileForgeClient } from "../src"; -import { generate_from_html } from "../src/Helper"; +import { generate_from_html, ResponseStream, ResponseURL } from "../src/Helper"; import * as error from "../src/errors/index"; import fs from "fs"; import { writeFile } from "fs/promises"; @@ -52,18 +52,12 @@ describe("test", () => { { options: {} } - ); - - const chunks: any[] = []; - - for await (let chunk of pdf) { - chunks.push(chunk); - } - - const buffer: Buffer = Buffer.concat(chunks); - const bufferString = buffer.toString() - - await writeFile("output.pdf", bufferString); + ) as ResponseStream; + + // Write the PDF stream to a file + const writeStream = fs.createWriteStream('output.pdf'); + pdf.file.pipe(writeStream); + }, 10_000_000); @@ -88,11 +82,11 @@ describe("test", () => { host: true, } } - ); + ) as ResponseURL; expect(pdf.url).not.toBeNull(); - }, 10_000_000); + }, 10_000_000); it("should fail because of invalid api key", async () => { const htmlBlob = new Blob([HTML], { @@ -150,18 +144,11 @@ describe("test", () => { test:false } - ); - const chunks: any[] = []; - - for await (let chunk of pdf) { - chunks.push(chunk); - } - - const buffer: Buffer = Buffer.concat(chunks); - const bufferString = buffer.toString() - - - await writeFile("output_helper.pdf", bufferString); + ) as ResponseStream; + // Write the PDF stream to a file + const writeStream = fs.createWriteStream('output_helper.pdf'); + pdf.file.pipe(writeStream); + }, 10_000_000); it("should generate a PDF url from helper", async () => { @@ -186,7 +173,7 @@ describe("test", () => { host:true, } - ); + ) as ResponseURL; expect(pdf.url).not.toBeNull(); }, 10_000_000); @@ -215,16 +202,39 @@ describe("test", () => { } ); - const chunks: any[] = []; + // Write the PDF stream to a file + const writeStream = fs.createWriteStream('output_merged.pdf'); + pdf.file.pipe(writeStream); + +}, 10_000_000); + +it("should generate from html snippet", async () => { + try { + + const client = new FileForgeClient({ + apiKey: FILEFORGE_API_KEY + }); + const documentInput = { + html: HTML, + fileName: 'example', + test: false, + host: false, + expiresAt: new Date(Date.now() + 48 * 60 * 60 * 1000), + files: [ + { path: '/style.css', content: CSS }, + ], + }; - for await (let chunk of pdf) { - chunks.push(chunk); - } - - const buffer: Buffer = Buffer.concat(chunks); - const bufferString = buffer.toString() + const response = await generate_from_html(client, documentInput) as ResponseStream; + + // Write the PDF stream to a file + const writeStream = fs.createWriteStream('outputSnippet.pdf'); + response.file.pipe(writeStream); + console.log('PDF generated successfully.'); + } catch (error) { + console.error('Error generating PDF:', error); + } - await writeFile("output_merged.pdf", bufferString); }, 10_000_000); });