Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thib3113 opened this issue Jan 8, 2020 · 10 comments
Closed

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

thib3113 opened this issue Jan 8, 2020 · 10 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@thib3113
Copy link

thib3113 commented Jan 8, 2020

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 ...) ...

@thib3113 thib3113 changed the title using Buffer using Buffer is undocumented, and not easy to find how to use Jan 8, 2020
@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

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 :)

@yumauri yumauri added the documentation Improvements or additions to documentation label Jan 9, 2020
@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

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

@thib3113
Copy link
Author

thib3113 commented Jan 9, 2020

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 .

@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

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

@yumauri yumauri added the enhancement New feature or request label Jan 9, 2020
@thib3113
Copy link
Author

thib3113 commented Jan 9, 2020

great :)

so, thank you for the lib :) .

@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

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 😅

@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

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

@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

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

@thib3113
Copy link
Author

thib3113 commented Jan 9, 2020

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

Maybe add a link in the readme ;) .

@yumauri
Copy link
Owner

yumauri commented Jan 9, 2020

Done!

@yumauri yumauri closed this as completed Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants