This is a simple HTTP server application written in Go that converts uploaded office files into PDF files using LibreOffice in headless mode. The server listens on port 5000 and exposes a single endpoint for file conversion. It also includes automatic cleanup of temporary files older than one hour.
- Converts various office files to PDF format using LibreOffice.
- Handles file uploads via HTTP POST requests.
- Automatic cleanup of old files from the temporary directory after one hour.
- Minimal and efficient implementation using Go.
- Go: Ensure Go is installed on your system (Download Go).
- LibreOffice: LibreOffice must be installed and accessible via the
sofficecommand.
This application relies on LibreOffice for file conversion, so it supports any file format LibreOffice can handle. Below are the formats that can be converted to PDF:
.doc,.docx(Microsoft Word).odt,.ott(LibreOffice/OpenDocument Text).rtf(Rich Text Format).txt(Plain Text)
.xls,.xlsx(Microsoft Excel).ods,.ots(LibreOffice/OpenDocument Spreadsheet).csv(Comma-Separated Values)
.ppt,.pptx(Microsoft PowerPoint).odp,.otp(LibreOffice/OpenDocument Presentation)
.svg(Scalable Vector Graphics).html,.htm(HTML Files).xml(XML Files).pdf(for PDF editing and re-exporting)
-
Clone the repository:
git clone <repository_url> cd <repository_directory>
-
Install dependencies: This application does not have external dependencies, but ensure you have LibreOffice installed.
-
Build the application:
go build -o pdf-converter . -
Run the application:
./pdf-converter
The server will start listening on http://localhost:5000.
- Method:
POST - Field Name:
file - File Type: Any supported format (e.g.,
.xlsx,.docx).
curl -X POST -F "[email protected]" http://localhost:5000/convert --output output.pdf- Success: Returns the converted PDF file as a response with the
Content-Typeset toapplication/pdf. - Error: Returns an appropriate HTTP status code and error message if the conversion fails.
You can use the publicly available Docker image for this application:
docker pull wteja/pdf-converterRun the container:
docker run -p 5000:5000 wteja/pdf-converter- Temporary files are stored in the
./tmpdirectory. Ensure the application has write access to this directory. - The application automatically removes files older than one hour from the
tmpdirectory.
-
File Upload and Conversion:
- The
/convertendpoint processes file uploads, saves them to thetmpdirectory, and invokes LibreOffice in headless mode to perform the conversion.
- The
-
Temporary Directory Management:
- All uploaded and converted files are stored in the
tmpdirectory. - A background goroutine periodically checks and deletes files older than one hour.
- All uploaded and converted files are stored in the
-
Error Handling:
- Comprehensive error handling for file uploads, conversions, and temporary file management.
handleConvert: Handles the HTTP requests, manages file upload and conversion, and returns the resulting PDF.cleanupOldFiles: Periodically deletes old files from thetmpdirectory.
- Support additional file formats (e.g.,
.pptx,.odg). - Add configurable cleanup duration and temporary directory path.
- Implement better logging and monitoring.
This project is licensed under the MIT License. See the LICENSE file for details.
Developed by Weerayut Teja. Contributions and feedback are welcome!