avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Setting Up a Server to Convert Webpages into PDFs with WEB2PDF

Thu Oct 19 2023

Introduction

Converting webpages into PDFs is a common requirement for preserving the layout and content of webpages. In this blog post, we will walk you through the process of setting up a server using the WEB2PDF project which leverages Docker and Puppeteer for this purpose.

Prerequisites

  1. Docker and Docker Compose installed on your system.

Steps:

  1. Pull the WEB2PDF Docker image from Docker Hub using the command:
docker pull shinchven/web2pdf
  1. Create a docker-compose.yml file with the following content to set up the service:
version: '3.5'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    image: shinchven/web2pdf
    container_name: web2pdf
    restart: always
    ports:
      - "3040:3030"
    volumes:
      - ./output:/usr/src/app/public/output
  1. Navigate to the directory containing the docker-compose.yml file and run the command to start the service:
docker-compose up -d

Usage

  • To convert a webpage to PDF, you can use either the GET or POST HTTP methods as shown below:
curl "http://localhost:3040/generate-pdf?url=https://www.google.com&filename=google.pdf"

or

curl -X POST -F "url=https://www.google.com" -F "filename=google.pdf" http://localhost:3040/generate-pdf

This setup will allow you to easily convert webpages into PDFs by sending a simple HTTP request to the server.

Parameters

The service accepts several parameters to customize the output:

Parameter Description Default Value
url The URL of the web page to convert into a PDF. (none)
filename The name of the output PDF file. A UUID
pdfOptions Options to pass to the Puppeteer's pdf method. undefined
redirect If set to true, the service will redirect to the generated PDF file. false

Please note that the url parameter is required. If no filename is provided, a UUID will be used as the file name. The pdfOptions parameter can be used to pass any additional options to the Puppeteer's pdf method. If redirect is set to true, the service will redirect to the generated PDF file instead of returning a JSON response.