The goal: Have a Node.js server which will take a pdf from a PUT call, and return each page converted to a .jpg. Ultimately, I don't care how it gets done, of course.
I've successfully created a Node.js server which uses imagemagick to convert a pdf to an image. It's an express server, and it uses this imagemagick npm package, which wraps the imagemagick CLI.
Here's the code for the route, which works very well running locally
app.put('/v1/convert-pdf', (req, res) => {
res.status(202).send({ message: "we're working on it, alright?!" })
console.log("beginning image conversion")
im.convert(["-density", "144", "./content/pdf/foo.pdf", "./content/img/bar.jpg"], (err, stdout) => {
if (err) {
console.error(err)
} else {
console.log(stdout)
console.log("finished converting pdfs");
}
})
})
The route above outputs a bunch of .jpg files named foo-0.jpg, foo-1.jpg, etc... Now what I want to do is take a file, which is put to the route, convert it via imagemagick and then give it back to the user (eventually I'll put it in an S3 bucket, but baby steps).
If it does require saving the file to a folder in Heroku, how do I do that? I've attempted to read/write from Heroku, and I don't get an error (unless one of the files/directories doesn't exist), but I don't see the file either.
Summarising our conversation in comments here in case it's helpful to anyone:
The reason you won't be able to see the files that were created and saved by the application when you run heroku run bash
is because that actually spins up a new dyno and doesn't have access to the file system of the dyno running the app.
As mentioned, the best way to do this is to upload the resultant images to something like S3 as soon as the conversion is complete, and to serve any incoming requests to retrieve those files through S3.
If running a single web dyno this wouldn't be a problem right now, but if you're running multiple then the converted files will only be available on the dyno that received and transformed the PDF, so other dynos won't have access to them.
Also, each deployment creates new dynos, so unless you store those files in something like S3, they'll be lost as soon as you push up.