yumauri/gotenberg-js-client

using Buffer is undocumented, and not easy to find how to use

thib3113 opened this issue ยท 10 comments

It's seems this :

const myBuffer = fs.readFileSync('myDoc.docx');
const pdf = await toPDF(myBuffer);

Doesn't work, because the file name send to gotenberg is 'index.html' ...

After some debug, I found a solution :

const myBuffer = fs.readFileSync('myDoc.docx');
const pdf = await toPDF(["myDoc.dox", myBuffer]);

But it's either undocumented, and maybe need to throw an exception if the fileName is not retrievable with the argument (Buffer, stream ...) ...

Hi, @thib3113!
Thank you for using my library :)

There is no way to know filename from Buffer (or string, readFileSync could return it also). If library cannot determine filename of an asset โ€” index.html is used by default.
This is done for convenience, if you want to convert just HTML string, like this:

const pdf = await toPDF('<html>...</html>')

(I presume, that converting HTMLs is more frequent task, than converting DOCs)

There are few ways to define filename, they are mentioned here in example, but you are right, description is not very, well, specific...

In your case you can use any of this:

// like tuple, as you already know
const pdf = await toPDF(['myDoc.docx', myBuffer])

// like object, where keys are filenames
const pdf = await toPDF({ 'myDoc.docx': myBuffer })

// using file link, fs.ReadStream will be used internally
// in that case you don't need variable myBuffer
const pdf = await toPDF({ 'myDoc.docx': 'file://myDoc.docx' })

// using file link without specifying filename
// fs.ReadStream will be used internally, and will get filename from file link
// in that case you don't need variable myBuffer also
const pdf = await toPDF('file://myDoc.docx')

// using fs.ReadStream directly
// filename will be retrieved from file stream
const pdf = await toPDF(fs.createReadStream('myDoc.docx'))

Library will try to figure out, what kind of format you are using :)

I will think, maybe, I'll add some heuristic, and if filename is not defined (and cannot be determined), and source doesn't look like HTML โ€” throw an exception, as you suggested

In my case, the buffer come from an http request ... So, can't use file:// ( or need to store the file, to re-read it ๐Ÿค” ), and using a variable in object name, is not so easy (in fact it's possible, but need to know the syntax).

About the index.html, you need to pass a function in the pipe, to set if you want to convert HTML to PDF, or OFFICE to PDF, you can maybe use it ?

If function to convert to office is used, and filename can't be retrieve, throw an exception .

You are right, no need for heuristic, because I already know, what I want to convert :)

great :)

so, thank you for the lib :) .

Published version 0.4.2 just now, added some sources validations:

If you'll try to convert Office document with default filename (index.html), library will throw exception

Default filename "index.html" is not allowed for Office conversion, looks like you didn't set filename for document

If you will try to convert HTML (or Markdown) without index.html, it will throw exception

File "index.html" is required for HTML (Markdown) conversion

If you will add two assets with same name, it will throw exception

There are duplicates in file names: ...

Also, to native client I've added passing messages from Gotenberg itself, in case of error, for example, if you will pass asset doc.bla to Office conversion, it will throw exception from Gotenberg

Error: 400 Bad Request (no resource file found for extensions '[.txt .rtf .fodt .doc .docx .odt .xls .xlsx .ods .ppt .pptx .odp]')

By the way, you can implement your own client, for example, based on request library, but this is also undocumented, though ๐Ÿ˜…

I think I'll leave this issue opened until I add more lengthy description for sources in documentation

@thib3113 can you check this out, please? Does this look good enough?
https://github.com/yumauri/gotenberg-js-client/wiki/Source

yes, seems great :) . We can easily understand how to "name" the buffer, or string, or else .

Maybe add a link in the readme ;) .