This project provides a REST API for processing PDF files, offering two main endpoints: /html2pdf and /merge. It acts as a wrapper for two powerful command-line utilities: wkhtmltopdf for converting HTML to PDF, and pdftk for merging PDF documents.
The primary goal is to offer a minimalist and straightforward implementation for specific use cases, but the codebase can be easily adapted for more generic purposes.
/html2pdf: Converts an HTML file (along with its assets like images and CSS) into a PDF./merge: Merges multiple PDF files into a single document, sorted alphabetically by filename.
Both endpoints expect a .zip file containing all the necessary input files.
Before you begin, ensure you have the following installed on your system:
- Go: Version 1.18 or higher.
- wkhtmltopdf: A command-line tool to render HTML into PDF.
- pdftk: A command-line tool for manipulating PDF documents.
Below are the instructions to get the project up and running on different platforms.
-
Install Dependencies:
- Download and install
wkhtmltopdffrom the official site. - Download and install
pdftkfrom the official site. - Ensure the installation directories for both tools are added to your system's
PATHenvironment variable.
- Download and install
-
Build the Application: Open a command prompt or PowerShell and run the following command from the project root to build the executable:
go build -o mypdfservice.exe ./cmd/webapi/main.go
-
Run the Service: Execute the compiled binary:
./mypdfservice.exe
The server will start listening on the default port
8080. To use a different port, pass it as an argument:./mypdfservice.exe 9999
-
Install Dependencies:
- For Debian/Ubuntu:
sudo apt-get update sudo apt-get install -y wkhtmltopdf pdftk
- For openSUSE:
sudo zypper refresh sudo zypper install -y wkhtmltopdf pdftk
- For Debian/Ubuntu:
-
Build the Application: Open a terminal and run the following command from the project root:
go build -o mypdfservice ./cmd/webapi/main.go
-
Run the Service: Execute the compiled binary:
./mypdfservice
The server will start on port
8080. To specify a different port:./mypdfservice 9999
A docker-compose setup is provided for easy local deployment.
-
Prerequisite: Ensure you have Docker and Docker Compose installed.
-
Build and Run: Navigate to the
cmd/dockerdirectory and run:cd cmd/docker docker-compose up --buildThis command will build the Docker image and start the service. The API will be accessible at
http://localhost:8080.
You can deploy the application as a serverless container to Google Cloud Run.
-
Prerequisites:
- Install the Google Cloud CLI.
- Authenticate and configure the CLI with your GCP project:
gcloud auth loginandgcloud config set project YOUR_PROJECT_ID. - Enable the Cloud Build and Cloud Run APIs for your project.
-
Deploy: From the project root directory, run the following command:
gcloud builds submit --config cloudbuild.yaml .This command uses Cloud Build to build the container image, push it to the Google Container Registry, and deploy it as a new service on Cloud Run.
- Method:
POST - Description: Converts an HTML file to a PDF.
- Input: A
multipart/form-datarequest containing a single.zipfile. The zip file must include:- One
.htmlfile. - All image files (
.jpg,.jpeg,.png,.gif) referenced by<img>tags in the HTML. Image paths in thesrcattribute must match the filenames in the zip archive.
- One
- Method:
POST - Description: Merges multiple PDF files into one.
- Input: A
multipart/form-datarequest containing a single.zipfile with all the.pdffiles to be merged. The files will be merged in alphabetical order of their filenames.
Here are examples of how to test the endpoints using cURL.
-
Merge PDFs:
curl -X POST http://localhost:8080/merge \ -F "files=@/path/to/your/merge-example.zip" \ -H "Content-Type: multipart/form-data" \ -o merged_output.pdf
-
Convert HTML to PDF:
curl -X POST http://localhost:8080/html2pdf \ -F "files=@/path/to/your/html-example.zip" \ -H "Content-Type: multipart/form-data" \ -o converted_output.pdf
The application maintains a detailed debug log in mypdfservice_debug.log, created in the same directory as the executable. This log is rotated automatically to manage disk space:
- The log file is limited to 10 MB.
- When the limit is reached, the file is compressed (e.g.,
mypdfservice_debug-TIMESTAMP.log.gz). - A maximum of 20 old log files are kept.
- Logs older than 90 days are automatically deleted.
You can also capture a summary log of console output by redirecting the standard output:
# Windows
./mypdfservice.exe > summary.log
# Linux
./mypdfservice > summary.log- Many thanks to the developers of the wkhtmltopdf and pdftk projects. This application would not be possible without their excellent work.
- This project was developed entirely using the Gitpod cloud development environment.
// TODO