A basic express API app that has a upload POST route that accepts a PDF and returns a generated .wav
file. The project uses OS specific TTS (text to speech) provided by say.js.
Very large PDFs will currently time out, this is a hobby project so I haven't worked through optimization. Currently only supports single files.
After downloading or cloning the repo, follow these steps. The project relies on Node so be sure that it is installed properly on your system.
- Create a
.env
file with thePORT
if you would like to set a designated port for the project to run on other thanlocalhost:3000
. - In your terminal run
npm install
. - Run
npm run start
to start up the server. - You can now access the API however you see fit.
GET http://your_api_endpoint_url
: This returns a generic welcome.wav
file with instructions directing you to the uploadPOST
route.POST http://your_api_endpoint_url/upload
: This allows you to submit any PDF you would like for conversion.
How to use (Documented by ChatGPT)
To submit a POST request with a PDF attached, you can use various tools or libraries. One of the most popular tools for testing API endpoints is Postman. Alternatively, you could use curl
from the command line, or use a programming language's HTTP client.
Here's how you can do it using all three methods:
- Download and install Postman.
- Open Postman and create a new request.
- Set the request method to
POST
. - Enter the API endpoint URL.
- In the
Body
tab, selectform-data
. - Enter a key (e.g., "pdf") and select the type as
File
from the dropdown. Choose your PDF by clicking the "Select Files" button. - Click "Send".
From your terminal or command prompt:
curl -X POST -F "pdf=@path_to_your_file.pdf" http://your_api_endpoint_url/upload
Make sure to replace path_to_your_file.pdf
with the path to your PDF file and http://your_api_endpoint_url/upload
with your actual API endpoint.
First, you need to install the required packages:
npm install axios form-data
Then, you can use the following code to make a POST request with the attached PDF:
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const form = new FormData();
form.append('pdf', fs.createReadStream('path_to_your_file.pdf'));
axios.post('http://your_api_endpoint_url/upload', form, {
headers: {
...form.getHeaders()
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Error uploading the file:', error);
});
Replace path_to_your_file.pdf
with the path to your PDF and http://your_api_endpoint_url/upload
with your actual API endpoint.
Choose whichever method is most convenient for you. If you're doing this for testing or debugging, tools like Postman are especially handy due to their user-friendly interface and features.