There is a project in the work that needs to convert HTML into PDF. At that time, it was used
dompdf/dompdf。 Later, there is a need to convert HTML into image. Then I went to find it
wkhtmltopdfBoth PDF and HTML can be generated. Immediately tossed with joy.
wkhtmltoimageMay not be the best choice, after all, Google’s
PuppeteerMuch better than it. But as a cli software, it can quickly take us to achieve what we want.
In addition, you may ask why you don’t use third-party packages. Because the project at work is deployed on docker and the basic image is alpine, this may bring unpredictable problems to our subsequent operations. In addition, multiple projects may use this service together in the future, so it is used at present
goWrap it into an HTTP service for all projects to access and call.
OverNaive/Html2X, the following is actually a readme document.
Html2x is an out of the box HTTP service based on wkhtmltox, which helps the server quickly generate PDF / image.
1. Replace the third-party package with HTTP service, which is decoupled from the business system and can be updated independently;
2. Package the installation of wkhtmltox in docker to update the version quickly;
3. You can quickly complete the deployment by directly pulling the image, which is really out of the box.
How to use
Please install docker yourself first
1. Get image
- Build image locally:
docker build -t overnaive/html2x
- Remote pull image:
docker pull overnaive/html2x
2. Run image
docker run --name html2x -p 8080:8888 -it -d overnaive/html2x, you can run a container.
At this point, an HTTP service is running.
Please go to API for html2x to check.
- Docker learning materials: docker — from introduction to practice
- Wkhtmltopdf documentation
- Wkhtmltoimage documentation
It is planned to use gin to realize complex logic such as parameter verification, authentication and current limiting.
If you don’t know about docker, but want to use it quickly; Or you just want to experience it.
Here is a deployed free service. Please check the free html2x service.