Skip to content

Instantly share code, notes, and snippets.

@marteinn
Created January 30, 2023 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marteinn/18d519e109c3bcdc02b050faa9681a0d to your computer and use it in GitHub Desktop.
Save marteinn/18d519e109c3bcdc02b050faa9681a0d to your computer and use it in GitHub Desktop.
Read local file from jpg, pass to gotenberg libreoffice and then download result as pdf
const fs = require('fs/promises');
const { createWriteStream } = require("fs");
const util = require('util');
const stream = require('stream');
const got = require('got'); // third party
const FormData = require('form-data'); // third party
const pipeline = util.promisify(stream.pipeline);
const main = async () => {
const data = await fs.readFile('./test.jpg', { encoding: null });
let formData = new FormData();
formData.append('file', data, {
filename: 'test.jpg',
contentType: 'image/jpg',
});
const downloadStream = got.stream.post(
'https://demo.gotenberg.dev/forms/libreoffice/convert',
{
body: formData,
}
);
const fileName = "hello.pdf"
const fileWriterStream = createWriteStream(fileName);
await pipeline(downloadStream, fileWriterStream)
// Resave file example
// const myData = await fs.readFile(fileName, { encoding: null });
// fs.writeFile("./hello-2.pdf", myData)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment