Created
January 30, 2023 13:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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