Closed 1226zxc closed 2 years ago
Hello! Yes, it is absolutely normal!
It is IncomingMessage
instance because actually PDF is got by HTTP/S protocol using Node.js native modules http
or https
(depending on Gotenberg URL).
IncomingMessage
class extends Node.js readable stream, so, it is actually a stream of bytes.
http.IncomingMessage
->stream.Readable
And you can pipe that stream of bytes into any destination you want. For example, write it into the file:
pdf.pipe(
fs.createWriteStream(
filename(ossFileName.substring(0, ossFileName.lastIndexOf('.')) + '.pdf')
)
)
(you can omit resultFilename
parameter actually in toPDF
description, it is kinda useless in case you write result in file)
If you want to convert it to Buffer, you can write convert function like this (this is just an example, you can find other code snippets for that task):
async function stream2buffer(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on("data", (chunk) => chunks.push(chunk));
stream.on("end", () => resolve(Buffer.concat(chunks)));
stream.on("error", reject);
});
}
const pdfAsBuffer = await stream2buffer(pdf)
Hello! Yes, it is absolutely normal!
It is
IncomingMessage
instance because actually PDF is got by HTTP/S protocol using Node.js native moduleshttp
orhttps
(depending on Gotenberg URL).
IncomingMessage
class extends Node.js readable stream, so, it is actually a stream of bytes.
http.IncomingMessage
->stream.Readable
And you can pipe that stream of bytes into any destination you want. For example, write it into the file:
pdf.pipe( fs.createWriteStream( filename(ossFileName.substring(0, ossFileName.lastIndexOf('.')) + '.pdf') ) )
(you can omit
resultFilename
parameter actually intoPDF
description, it is kinda useless in case you write result in file)If you want to convert it to Buffer, you can write convert function like this (this is just an example, you can find other code snippets for that task):
async function stream2buffer(stream) { return new Promise((resolve, reject) => { const chunks = []; stream.on("data", (chunk) => chunks.push(chunk)); stream.on("end", () => resolve(Buffer.concat(chunks))); stream.on("error", reject); }); } const pdfAsBuffer = await stream2buffer(pdf)
Thx for your so clear answer. That does help me out of the problem! Thanks again!
Hi~ Firstly, I'm new to Node.js. I got a problem when I'd like to convert some docs to pdfs. I just followed the documentation described and when I debug the code below, I found it's actually an IncomingMessage instance. Is that normal? I've browsed the issues page. It's seam like no one had the same problem. Doesn't the response return the pdf buffer directly if it would work? How can I get the response body from it? Or is it something I did wrong?
const pdf = await toPDF([docName, docBuffer])
where thedocName
anddocBuffer
are variables. I am using 6.x gotenberg and 0.7.2 of gotenberg-js-client. In addition, no errors were found in Gotenberg logs, just INFO logs. Thx!Addtional code segment: